LCOV - differential code coverage report
Current view: top level - src/backend/parser - parse_coerce.c (source / functions) Coverage Total Hit UBC CBC
Current: a2387c32f2f8a1643c7d71b951587e6bcb2d4744 vs 371a302eecdc82274b0ae2967d18fd726a0aa6a1 Lines: 91.7 % 1041 955 86 955
Current Date: 2025-10-26 12:31:50 -0700 Functions: 100.0 % 31 31 31
Baseline: lcov-20251027-010456-baseline Branches: 76.0 % 1106 841 265 841
Baseline Date: 2025-10-26 11:01:32 +1300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 11 11 11
(360..) days: 91.7 % 1030 944 86 944
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 30 30 30
Branch coverage date bins:
(30,360] days: 75.0 % 4 3 1 3
(360..) days: 76.0 % 1102 838 264 838

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * parse_coerce.c
                                  4                 :                :  *      handle type coercions/conversions for parser
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/parser/parse_coerce.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include "access/htup_details.h"
                                 18                 :                : #include "catalog/pg_cast.h"
                                 19                 :                : #include "catalog/pg_class.h"
                                 20                 :                : #include "catalog/pg_inherits.h"
                                 21                 :                : #include "catalog/pg_proc.h"
                                 22                 :                : #include "catalog/pg_type.h"
                                 23                 :                : #include "nodes/makefuncs.h"
                                 24                 :                : #include "nodes/nodeFuncs.h"
                                 25                 :                : #include "parser/parse_coerce.h"
                                 26                 :                : #include "parser/parse_relation.h"
                                 27                 :                : #include "parser/parse_type.h"
                                 28                 :                : #include "utils/builtins.h"
                                 29                 :                : #include "utils/datum.h"      /* needed for datumIsEqual() */
                                 30                 :                : #include "utils/fmgroids.h"
                                 31                 :                : #include "utils/lsyscache.h"
                                 32                 :                : #include "utils/syscache.h"
                                 33                 :                : #include "utils/typcache.h"
                                 34                 :                : 
                                 35                 :                : 
                                 36                 :                : static Node *coerce_type_typmod(Node *node,
                                 37                 :                :                                 Oid targetTypeId, int32 targetTypMod,
                                 38                 :                :                                 CoercionContext ccontext, CoercionForm cformat,
                                 39                 :                :                                 int location,
                                 40                 :                :                                 bool hideInputCoercion);
                                 41                 :                : static void hide_coercion_node(Node *node);
                                 42                 :                : static Node *build_coercion_expression(Node *node,
                                 43                 :                :                                        CoercionPathType pathtype,
                                 44                 :                :                                        Oid funcId,
                                 45                 :                :                                        Oid targetTypeId, int32 targetTypMod,
                                 46                 :                :                                        CoercionContext ccontext, CoercionForm cformat,
                                 47                 :                :                                        int location);
                                 48                 :                : static Node *coerce_record_to_complex(ParseState *pstate, Node *node,
                                 49                 :                :                                       Oid targetTypeId,
                                 50                 :                :                                       CoercionContext ccontext,
                                 51                 :                :                                       CoercionForm cformat,
                                 52                 :                :                                       int location);
                                 53                 :                : static bool is_complex_array(Oid typid);
                                 54                 :                : static bool typeIsOfTypedTable(Oid reltypeId, Oid reloftypeId);
                                 55                 :                : 
                                 56                 :                : 
                                 57                 :                : /*
                                 58                 :                :  * coerce_to_target_type()
                                 59                 :                :  *      Convert an expression to a target type and typmod.
                                 60                 :                :  *
                                 61                 :                :  * This is the general-purpose entry point for arbitrary type coercion
                                 62                 :                :  * operations.  Direct use of the component operations can_coerce_type,
                                 63                 :                :  * coerce_type, and coerce_type_typmod should be restricted to special
                                 64                 :                :  * cases (eg, when the conversion is expected to succeed).
                                 65                 :                :  *
                                 66                 :                :  * Returns the possibly-transformed expression tree, or NULL if the type
                                 67                 :                :  * conversion is not possible.  (We do this, rather than ereport'ing directly,
                                 68                 :                :  * so that callers can generate custom error messages indicating context.)
                                 69                 :                :  *
                                 70                 :                :  * pstate - parse state (can be NULL, see coerce_type)
                                 71                 :                :  * expr - input expression tree (already transformed by transformExpr)
                                 72                 :                :  * exprtype - result type of expr
                                 73                 :                :  * targettype - desired result type
                                 74                 :                :  * targettypmod - desired result typmod
                                 75                 :                :  * ccontext, cformat - context indicators to control coercions
                                 76                 :                :  * location - parse location of the coercion request, or -1 if unknown/implicit
                                 77                 :                :  */
                                 78                 :                : Node *
 8217 tgl@sss.pgh.pa.us          79                 :CBC      375589 : coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
                                 80                 :                :                       Oid targettype, int32 targettypmod,
                                 81                 :                :                       CoercionContext ccontext,
                                 82                 :                :                       CoercionForm cformat,
                                 83                 :                :                       int location)
                                 84                 :                : {
                                 85                 :                :     Node       *result;
                                 86                 :                :     Node       *origexpr;
                                 87                 :                : 
 7803                            88         [ +  + ]:         375589 :     if (!can_coerce_type(1, &exprtype, &targettype, ccontext))
                                 89                 :            217 :         return NULL;
                                 90                 :                : 
                                 91                 :                :     /*
                                 92                 :                :      * If the input has a CollateExpr at the top, strip it off, perform the
                                 93                 :                :      * coercion, and put a new one back on.  This is annoying since it
                                 94                 :                :      * duplicates logic in coerce_type, but if we don't do this then it's too
                                 95                 :                :      * hard to tell whether coerce_type actually changed anything, and we
                                 96                 :                :      * *must* know that to avoid possibly calling hide_coercion_node on
                                 97                 :                :      * something that wasn't generated by coerce_type.  Note that if there are
                                 98                 :                :      * multiple stacked CollateExprs, we just discard all but the topmost.
                                 99                 :                :      * Also, if the target type isn't collatable, we discard the CollateExpr.
                                100                 :                :      */
 5047                           101                 :         375372 :     origexpr = expr;
                                102   [ +  -  +  + ]:         375402 :     while (expr && IsA(expr, CollateExpr))
                                103                 :             30 :         expr = (Node *) ((CollateExpr *) expr)->arg;
                                104                 :                : 
 7803                           105                 :         375372 :     result = coerce_type(pstate, expr, exprtype,
                                106                 :                :                          targettype, targettypmod,
                                107                 :                :                          ccontext, cformat, location);
                                108                 :                : 
                                109                 :                :     /*
                                110                 :                :      * If the target is a fixed-length type, it may need a length coercion as
                                111                 :                :      * well as a type coercion.  If we find ourselves adding both, force the
                                112                 :                :      * inner coercion node to implicit display form.
                                113                 :                :      */
                                114                 :         373182 :     result = coerce_type_typmod(result,
                                115                 :                :                                 targettype, targettypmod,
                                116                 :                :                                 ccontext, cformat, location,
                                117   [ +  +  +  + ]:         373182 :                                 (result != expr && !IsA(result, Const)));
                                118                 :                : 
 1659                           119   [ +  +  +  + ]:         373182 :     if (expr != origexpr && type_is_collatable(targettype))
                                120                 :                :     {
                                121                 :                :         /* Reinstall top CollateExpr */
 5047                           122                 :             24 :         CollateExpr *coll = (CollateExpr *) origexpr;
                                123                 :             24 :         CollateExpr *newcoll = makeNode(CollateExpr);
                                124                 :                : 
                                125                 :             24 :         newcoll->arg = (Expr *) result;
                                126                 :             24 :         newcoll->collOid = coll->collOid;
                                127                 :             24 :         newcoll->location = coll->location;
                                128                 :             24 :         result = (Node *) newcoll;
                                129                 :                :     }
                                130                 :                : 
 7803                           131                 :         373182 :     return result;
                                132                 :                : }
                                133                 :                : 
                                134                 :                : 
                                135                 :                : /*
                                136                 :                :  * coerce_type()
                                137                 :                :  *      Convert an expression to a different type.
                                138                 :                :  *
                                139                 :                :  * The caller should already have determined that the coercion is possible;
                                140                 :                :  * see can_coerce_type.
                                141                 :                :  *
                                142                 :                :  * Normally, no coercion to a typmod (length) is performed here.  The caller
                                143                 :                :  * must call coerce_type_typmod as well, if a typmod constraint is wanted.
                                144                 :                :  * (But if the target type is a domain, it may internally contain a
                                145                 :                :  * typmod constraint, which will be applied inside coerce_to_domain.)
                                146                 :                :  * In some cases pg_cast specifies a type coercion function that also
                                147                 :                :  * applies length conversion, and in those cases only, the result will
                                148                 :                :  * already be properly coerced to the specified typmod.
                                149                 :                :  *
                                150                 :                :  * pstate is only used in the case that we are able to resolve the type of
                                151                 :                :  * a previously UNKNOWN Param.  It is okay to pass pstate = NULL if the
                                152                 :                :  * caller does not want type information updated for Params.
                                153                 :                :  *
                                154                 :                :  * Note: this function must not modify the given expression tree, only add
                                155                 :                :  * decoration on top of it.  See transformSetOperationTree, for example.
                                156                 :                :  */
                                157                 :                : Node *
 8217                           158                 :         759156 : coerce_type(ParseState *pstate, Node *node,
                                159                 :                :             Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod,
                                160                 :                :             CoercionContext ccontext, CoercionForm cformat, int location)
                                161                 :                : {
                                162                 :                :     Node       *result;
                                163                 :                :     CoercionPathType pathtype;
                                164                 :                :     Oid         funcId;
                                165                 :                : 
 9349                           166   [ +  +  -  + ]:         759156 :     if (targetTypeId == inputTypeId ||
                                167                 :                :         node == NULL)
                                168                 :                :     {
                                169                 :                :         /* no conversion needed */
 8217                           170                 :         155033 :         return node;
                                171                 :                :     }
 7985                           172   [ +  +  +  + ]:         604123 :     if (targetTypeId == ANYOID ||
 6956                           173         [ +  + ]:         565489 :         targetTypeId == ANYELEMENTOID ||
 2048                           174         [ +  - ]:         558290 :         targetTypeId == ANYNONARRAYOID ||
                                175         [ -  + ]:         558290 :         targetTypeId == ANYCOMPATIBLEOID ||
                                176                 :                :         targetTypeId == ANYCOMPATIBLENONARRAYOID)
                                177                 :                :     {
                                178                 :                :         /*
                                179                 :                :          * Assume can_coerce_type verified that implicit coercion is okay.
                                180                 :                :          *
                                181                 :                :          * Note: by returning the unmodified node here, we are saying that
                                182                 :                :          * it's OK to treat an UNKNOWN constant as a valid input for a
                                183                 :                :          * function accepting one of these pseudotypes.  This should be all
                                184                 :                :          * right, since an UNKNOWN value is still a perfectly valid Datum.
                                185                 :                :          *
                                186                 :                :          * NB: we do NOT want a RelabelType here: the exposed type of the
                                187                 :                :          * function argument must be its actual type, not the polymorphic
                                188                 :                :          * pseudotype.
                                189                 :                :          */
 7985                           190                 :          45833 :         return node;
                                191                 :                :     }
 5255                           192   [ +  +  +  + ]:         558290 :     if (targetTypeId == ANYARRAYOID ||
 5107 heikki.linnakangas@i      193         [ +  + ]:         542317 :         targetTypeId == ANYENUMOID ||
 2048 tgl@sss.pgh.pa.us         194         [ +  + ]:         537898 :         targetTypeId == ANYRANGEOID ||
 1772 akorotkov@postgresql      195         [ +  - ]:         535347 :         targetTypeId == ANYMULTIRANGEOID ||
 2048 tgl@sss.pgh.pa.us         196         [ +  - ]:         535347 :         targetTypeId == ANYCOMPATIBLEARRAYOID ||
 1772 akorotkov@postgresql      197         [ -  + ]:         535347 :         targetTypeId == ANYCOMPATIBLERANGEOID ||
                                198                 :                :         targetTypeId == ANYCOMPATIBLEMULTIRANGEOID)
                                199                 :                :     {
                                200                 :                :         /*
                                201                 :                :          * Assume can_coerce_type verified that implicit coercion is okay.
                                202                 :                :          *
                                203                 :                :          * These cases are unlike the ones above because the exposed type of
                                204                 :                :          * the argument must be an actual array, enum, range, or multirange
                                205                 :                :          * type.  In particular the argument must *not* be an UNKNOWN
                                206                 :                :          * constant.  If it is, we just fall through; below, we'll call the
                                207                 :                :          * pseudotype's input function, which will produce an error.  Also, if
                                208                 :                :          * what we have is a domain over array, enum, range, or multirange, we
                                209                 :                :          * have to relabel it to its base type.
                                210                 :                :          *
                                211                 :                :          * Note: currently, we can't actually see a domain-over-enum here,
                                212                 :                :          * since the other functions in this file will not match such a
                                213                 :                :          * parameter to ANYENUM.  But that should get changed eventually.
                                214                 :                :          */
 5255 tgl@sss.pgh.pa.us         215         [ +  + ]:          22943 :         if (inputTypeId != UNKNOWNOID)
                                216                 :                :         {
                                217                 :          22371 :             Oid         baseTypeId = getBaseType(inputTypeId);
                                218                 :                : 
                                219         [ +  + ]:          22371 :             if (baseTypeId != inputTypeId)
                                220                 :                :             {
                                221                 :             27 :                 RelabelType *r = makeRelabelType((Expr *) node,
                                222                 :                :                                                  baseTypeId, -1,
                                223                 :                :                                                  InvalidOid,
                                224                 :                :                                                  cformat);
                                225                 :                : 
                                226                 :             27 :                 r->location = location;
                                227                 :             27 :                 return (Node *) r;
                                228                 :                :             }
                                229                 :                :             /* Not a domain type, so return it as-is */
                                230                 :          22344 :             return node;
                                231                 :                :         }
                                232                 :                :     }
 8217                           233   [ +  +  +  + ]:         535919 :     if (inputTypeId == UNKNOWNOID && IsA(node, Const))
                                234                 :                :     {
                                235                 :                :         /*
                                236                 :                :          * Input is a string constant with previously undetermined type. Apply
                                237                 :                :          * the target type's typinput function to it to produce a constant of
                                238                 :                :          * the target type.
                                239                 :                :          *
                                240                 :                :          * NOTE: this case cannot be folded together with the other
                                241                 :                :          * constant-input case, since the typinput function does not
                                242                 :                :          * necessarily behave the same as a type conversion function. For
                                243                 :                :          * example, int4's typinput function will reject "1.2", whereas
                                244                 :                :          * float-to-int type conversion will round to integer.
                                245                 :                :          *
                                246                 :                :          * XXX if the typinput function is not immutable, we really ought to
                                247                 :                :          * postpone evaluation of the function call until runtime. But there
                                248                 :                :          * is no way to represent a typinput function call as an expression
                                249                 :                :          * tree, because C-string values are not Datums. (XXX This *is*
                                250                 :                :          * possible as of 7.3, do we want to do it?)
                                251                 :                :          */
 9580                           252                 :         375938 :         Const      *con = (Const *) node;
 9561                           253                 :         375938 :         Const      *newcon = makeNode(Const);
                                254                 :                :         Oid         baseTypeId;
                                255                 :                :         int32       baseTypeMod;
                                256                 :                :         int32       inputTypeMod;
                                257                 :                :         Type        baseType;
                                258                 :                :         ParseCallbackState pcbstate;
                                259                 :                : 
                                260                 :                :         /*
                                261                 :                :          * If the target type is a domain, we want to call its base type's
                                262                 :                :          * input routine, not domain_in().  This is to avoid premature failure
                                263                 :                :          * when the domain applies a typmod: existing input routines follow
                                264                 :                :          * implicit-coercion semantics for length checks, which is not always
                                265                 :                :          * what we want here.  The needed check will be applied properly
                                266                 :                :          * inside coerce_to_domain().
                                267                 :                :          */
 6256                           268                 :         375938 :         baseTypeMod = targetTypeMod;
 7145                           269                 :         375938 :         baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
                                270                 :                : 
                                271                 :                :         /*
                                272                 :                :          * For most types we pass typmod -1 to the input routine, because
                                273                 :                :          * existing input routines follow implicit-coercion semantics for
                                274                 :                :          * length checks, which is not always what we want here.  Any length
                                275                 :                :          * constraint will be applied later by our caller.  An exception
                                276                 :                :          * however is the INTERVAL type, for which we *must* pass the typmod
                                277                 :                :          * or it won't be able to obey the bizarre SQL-spec input rules. (Ugly
                                278                 :                :          * as sin, but so is this part of the spec...)
                                279                 :                :          */
 6256                           280         [ +  + ]:         375938 :         if (baseTypeId == INTERVALOID)
                                281                 :           2568 :             inputTypeMod = baseTypeMod;
                                282                 :                :         else
                                283                 :         373370 :             inputTypeMod = -1;
                                284                 :                : 
 3831 noah@leadboat.com         285                 :         375938 :         baseType = typeidType(baseTypeId);
                                286                 :                : 
 7145 tgl@sss.pgh.pa.us         287                 :         375938 :         newcon->consttype = baseTypeId;
 6256                           288                 :         375938 :         newcon->consttypmod = inputTypeMod;
 3831 noah@leadboat.com         289                 :         375938 :         newcon->constcollid = typeTypeCollation(baseType);
                                290                 :         375938 :         newcon->constlen = typeLen(baseType);
                                291                 :         375938 :         newcon->constbyval = typeByVal(baseType);
 9561 tgl@sss.pgh.pa.us         292                 :         375938 :         newcon->constisnull = con->constisnull;
                                293                 :                : 
                                294                 :                :         /*
                                295                 :                :          * We use the original literal's location regardless of the position
                                296                 :                :          * of the coercion.  This is a change from pre-9.2 behavior, meant to
                                297                 :                :          * simplify life for pg_stat_statements.
                                298                 :                :          */
 4962                           299                 :         375938 :         newcon->location = con->location;
                                300                 :                : 
                                301                 :                :         /*
                                302                 :                :          * Set up to point at the constant's text if the input routine throws
                                303                 :                :          * an error.
                                304                 :                :          */
 6265                           305                 :         375938 :         setup_parser_errposition_callback(&pcbstate, pstate, con->location);
                                306                 :                : 
                                307                 :                :         /*
                                308                 :                :          * We assume here that UNKNOWN's internal representation is the same
                                309                 :                :          * as CSTRING.
                                310                 :                :          */
 9329 bruce@momjian.us          311         [ +  + ]:         375938 :         if (!con->constisnull)
 3831 noah@leadboat.com         312                 :         337867 :             newcon->constvalue = stringTypeDatum(baseType,
                                313                 :                :                                                  DatumGetCString(con->constvalue),
                                314                 :                :                                                  inputTypeMod);
                                315                 :                :         else
                                316                 :          38071 :             newcon->constvalue = stringTypeDatum(baseType,
                                317                 :                :                                                  NULL,
                                318                 :                :                                                  inputTypeMod);
                                319                 :                : 
                                320                 :                :         /*
                                321                 :                :          * If it's a varlena value, force it to be in non-expanded
                                322                 :                :          * (non-toasted) format; this avoids any possible dependency on
                                323                 :                :          * external values and improves consistency of representation.
                                324                 :                :          */
 3567 tgl@sss.pgh.pa.us         325   [ +  +  +  + ]:         373626 :         if (!con->constisnull && newcon->constlen == -1)
                                326                 :         161424 :             newcon->constvalue =
                                327                 :         161424 :                 PointerGetDatum(PG_DETOAST_DATUM(newcon->constvalue));
                                328                 :                : 
                                329                 :                : #ifdef RANDOMIZE_ALLOCATED_MEMORY
                                330                 :                : 
                                331                 :                :         /*
                                332                 :                :          * For pass-by-reference data types, repeat the conversion to see if
                                333                 :                :          * the input function leaves any uninitialized bytes in the result. We
                                334                 :                :          * can only detect that reliably if RANDOMIZE_ALLOCATED_MEMORY is
                                335                 :                :          * enabled, so we don't bother testing otherwise.  The reason we don't
                                336                 :                :          * want any instability in the input function is that comparison of
                                337                 :                :          * Const nodes relies on bytewise comparison of the datums, so if the
                                338                 :                :          * input function leaves garbage then subexpressions that should be
                                339                 :                :          * identical may not get recognized as such.  See pgsql-hackers
                                340                 :                :          * discussion of 2008-04-04.
                                341                 :                :          */
                                342                 :                :         if (!con->constisnull && !newcon->constbyval)
                                343                 :                :         {
                                344                 :                :             Datum       val2;
                                345                 :                : 
                                346                 :                :             val2 = stringTypeDatum(baseType,
                                347                 :                :                                    DatumGetCString(con->constvalue),
                                348                 :                :                                    inputTypeMod);
                                349                 :                :             if (newcon->constlen == -1)
                                350                 :                :                 val2 = PointerGetDatum(PG_DETOAST_DATUM(val2));
                                351                 :                :             if (!datumIsEqual(newcon->constvalue, val2, false, newcon->constlen))
                                352                 :                :                 elog(WARNING, "type %s has unstable input conversion for \"%s\"",
                                353                 :                :                      typeTypeName(baseType), DatumGetCString(con->constvalue));
                                354                 :                :         }
                                355                 :                : #endif
                                356                 :                : 
 6265                           357                 :         373626 :         cancel_parser_errposition_callback(&pcbstate);
                                358                 :                : 
 9561                           359                 :         373626 :         result = (Node *) newcon;
                                360                 :                : 
                                361                 :                :         /* If target is a domain, apply constraints. */
 7145                           362         [ +  + ]:         373626 :         if (baseTypeId != targetTypeId)
                                363                 :          16960 :             result = coerce_to_domain(result,
                                364                 :                :                                       baseTypeId, baseTypeMod,
                                365                 :                :                                       targetTypeId,
                                366                 :                :                                       ccontext, cformat, location,
                                367                 :                :                                       false);
                                368                 :                : 
 3831 noah@leadboat.com         369                 :         373626 :         ReleaseSysCache(baseType);
                                370                 :                : 
 8217 tgl@sss.pgh.pa.us         371                 :         373626 :         return result;
                                372                 :                :     }
 5840                           373   [ +  +  +  + ]:         159981 :     if (IsA(node, Param) &&
                                374         [ +  + ]:           8546 :         pstate != NULL && pstate->p_coerce_param_hook != NULL)
                                375                 :                :     {
                                376                 :                :         /*
                                377                 :                :          * Allow the CoerceParamHook to decide what happens.  It can return a
                                378                 :                :          * transformed node (very possibly the same Param node), or return
                                379                 :                :          * NULL to indicate we should proceed with normal coercion.
                                380                 :                :          */
 2972 peter_e@gmx.net           381                 :           4689 :         result = pstate->p_coerce_param_hook(pstate,
                                382                 :                :                                              (Param *) node,
                                383                 :                :                                              targetTypeId,
                                384                 :                :                                              targetTypeMod,
                                385                 :                :                                              location);
 5840 tgl@sss.pgh.pa.us         386         [ +  + ]:           4689 :         if (result)
                                387                 :           4647 :             return result;
                                388                 :                :     }
 5344                           389         [ +  + ]:         155334 :     if (IsA(node, CollateExpr))
                                390                 :                :     {
                                391                 :                :         /*
                                392                 :                :          * If we have a COLLATE clause, we have to push the coercion
                                393                 :                :          * underneath the COLLATE; or discard the COLLATE if the target type
                                394                 :                :          * isn't collatable.  This is really ugly, but there is little choice
                                395                 :                :          * because the above hacks on Consts and Params wouldn't happen
                                396                 :                :          * otherwise.  This kluge has consequences in coerce_to_target_type.
                                397                 :                :          */
 5336                           398                 :           3823 :         CollateExpr *coll = (CollateExpr *) node;
                                399                 :                : 
 1659                           400                 :           3823 :         result = coerce_type(pstate, (Node *) coll->arg,
                                401                 :                :                              inputTypeId, targetTypeId, targetTypeMod,
                                402                 :                :                              ccontext, cformat, location);
                                403         [ +  - ]:           3823 :         if (type_is_collatable(targetTypeId))
                                404                 :                :         {
                                405                 :           3823 :             CollateExpr *newcoll = makeNode(CollateExpr);
                                406                 :                : 
                                407                 :           3823 :             newcoll->arg = (Expr *) result;
                                408                 :           3823 :             newcoll->collOid = coll->collOid;
                                409                 :           3823 :             newcoll->location = coll->location;
                                410                 :           3823 :             result = (Node *) newcoll;
                                411                 :                :         }
                                412                 :           3823 :         return result;
                                413                 :                :     }
 6719                           414                 :         151511 :     pathtype = find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
                                415                 :                :                                      &funcId);
                                416         [ +  + ]:         151511 :     if (pathtype != COERCION_PATH_NONE)
                                417                 :                :     {
                                418                 :                :         Oid         baseTypeId;
                                419                 :                :         int32       baseTypeMod;
                                420                 :                : 
  271                           421                 :         149860 :         baseTypeMod = targetTypeMod;
                                422                 :         149860 :         baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
                                423                 :                : 
 6719                           424         [ +  + ]:         149860 :         if (pathtype != COERCION_PATH_RELABELTYPE)
                                425                 :                :         {
                                426                 :                :             /*
                                427                 :                :              * Generate an expression tree representing run-time application
                                428                 :                :              * of the conversion function.  If we are dealing with a domain
                                429                 :                :              * target type, the conversion function will yield the base type,
                                430                 :                :              * and we need to extract the correct typmod to use from the
                                431                 :                :              * domain's typtypmod.
                                432                 :                :              */
                                433                 :          47234 :             result = build_coercion_expression(node, pathtype, funcId,
                                434                 :                :                                                baseTypeId, baseTypeMod,
                                435                 :                :                                                ccontext, cformat, location);
                                436                 :                : 
                                437                 :                :             /*
                                438                 :                :              * If domain, coerce to the domain type and relabel with domain
                                439                 :                :              * type ID, hiding the previous coercion node.
                                440                 :                :              */
 8457                           441         [ +  + ]:          47234 :             if (targetTypeId != baseTypeId)
 7145                           442                 :           1292 :                 result = coerce_to_domain(result, baseTypeId, baseTypeMod,
                                443                 :                :                                           targetTypeId,
                                444                 :                :                                           ccontext, cformat, location,
                                445                 :                :                                           true);
                                446                 :                :         }
                                447                 :                :         else
                                448                 :                :         {
                                449                 :                :             /*
                                450                 :                :              * We don't need to do a physical conversion, but we do need to
                                451                 :                :              * attach a RelabelType node so that the expression will be seen
                                452                 :                :              * to have the intended type when inspected by higher-level code.
                                453                 :                :              *
                                454                 :                :              * Also, domains may have value restrictions beyond the base type
                                455                 :                :              * that must be accounted for.  If the destination is a domain
                                456                 :                :              * then we won't need a RelabelType node.
                                457                 :                :              */
  271                           458                 :         102626 :             result = coerce_to_domain(node, baseTypeId, baseTypeMod,
                                459                 :                :                                       targetTypeId,
                                460                 :                :                                       ccontext, cformat, location,
                                461                 :                :                                       false);
 8302                           462         [ +  + ]:         102626 :             if (result == node)
                                463                 :                :             {
                                464                 :                :                 /*
                                465                 :                :                  * XXX could we label result with exprTypmod(node) instead of
                                466                 :                :                  * default -1 typmod, to save a possible length-coercion
                                467                 :                :                  * later? Would work if both types have same interpretation of
                                468                 :                :                  * typmod, which is likely but not certain.
                                469                 :                :                  */
 6269                           470                 :          79244 :                 RelabelType *r = makeRelabelType((Expr *) result,
                                471                 :                :                                                  targetTypeId, -1,
                                472                 :                :                                                  InvalidOid,
                                473                 :                :                                                  cformat);
                                474                 :                : 
                                475                 :          79244 :                 r->location = location;
                                476                 :          79244 :                 result = (Node *) r;
                                477                 :                :             }
                                478                 :                :         }
 8217                           479                 :         149860 :         return result;
                                480                 :                :     }
 7840                           481   [ +  +  +  - ]:           2553 :     if (inputTypeId == RECORDOID &&
                                482                 :            902 :         ISCOMPLEX(targetTypeId))
                                483                 :                :     {
                                484                 :                :         /* Coerce a RECORD to a specific complex type */
                                485                 :            902 :         return coerce_record_to_complex(pstate, node, targetTypeId,
                                486                 :                :                                         ccontext, cformat, location);
                                487                 :                :     }
 7480                           488   [ +  +  +  - ]:           1457 :     if (targetTypeId == RECORDOID &&
                                489                 :            708 :         ISCOMPLEX(inputTypeId))
                                490                 :                :     {
                                491                 :                :         /* Coerce a specific complex type to RECORD */
                                492                 :                :         /* NB: we do NOT want a RelabelType here */
                                493                 :            708 :         return node;
                                494                 :                :     }
                                495                 :                : #ifdef NOT_USED
                                496                 :                :     if (inputTypeId == RECORDARRAYOID &&
                                497                 :                :         is_complex_array(targetTypeId))
                                498                 :                :     {
                                499                 :                :         /* Coerce record[] to a specific complex array type */
                                500                 :                :         /* not implemented yet ... */
                                501                 :                :     }
                                502                 :                : #endif
 6223                           503   [ +  +  +  - ]:             49 :     if (targetTypeId == RECORDARRAYOID &&
                                504                 :              8 :         is_complex_array(inputTypeId))
                                505                 :                :     {
                                506                 :                :         /* Coerce a specific complex array type to record[] */
                                507                 :                :         /* NB: we do NOT want a RelabelType here */
                                508                 :              8 :         return node;
                                509                 :                :     }
 5413 peter_e@gmx.net           510         [ +  + ]:             33 :     if (typeInheritsFrom(inputTypeId, targetTypeId)
                                511         [ +  - ]:              3 :         || typeIsOfTypedTable(inputTypeId, targetTypeId))
                                512                 :                :     {
                                513                 :                :         /*
                                514                 :                :          * Input class type is a subclass of target, so generate an
                                515                 :                :          * appropriate runtime conversion (removing unneeded columns and
                                516                 :                :          * possibly rearranging the ones that are wanted).
                                517                 :                :          *
                                518                 :                :          * We will also get here when the input is a domain over a subclass of
                                519                 :                :          * the target type.  To keep life simple for the executor, we define
                                520                 :                :          * ConvertRowtypeExpr as only working between regular composite types;
                                521                 :                :          * therefore, in such cases insert a RelabelType to smash the input
                                522                 :                :          * expression down to its base type.
                                523                 :                :          */
 2923 tgl@sss.pgh.pa.us         524                 :             33 :         Oid         baseTypeId = getBaseType(inputTypeId);
 7625                           525                 :             33 :         ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
                                526                 :                : 
 2923                           527         [ -  + ]:             33 :         if (baseTypeId != inputTypeId)
                                528                 :                :         {
 2923 tgl@sss.pgh.pa.us         529                 :UBC           0 :             RelabelType *rt = makeRelabelType((Expr *) node,
                                530                 :                :                                               baseTypeId, -1,
                                531                 :                :                                               InvalidOid,
                                532                 :                :                                               COERCE_IMPLICIT_CAST);
                                533                 :                : 
                                534                 :              0 :             rt->location = location;
                                535                 :              0 :             node = (Node *) rt;
                                536                 :                :         }
 7625 tgl@sss.pgh.pa.us         537                 :CBC          33 :         r->arg = (Expr *) node;
                                538                 :             33 :         r->resulttype = targetTypeId;
                                539                 :             33 :         r->convertformat = cformat;
 6269                           540                 :             33 :         r->location = location;
 7625                           541                 :             33 :         return (Node *) r;
                                542                 :                :     }
                                543                 :                :     /* If we get here, caller blew it */
 8136 tgl@sss.pgh.pa.us         544         [ #  # ]:UBC           0 :     elog(ERROR, "failed to find conversion function from %s to %s",
                                545                 :                :          format_type_be(inputTypeId), format_type_be(targetTypeId));
                                546                 :                :     return NULL;                /* keep compiler quiet */
                                547                 :                : }
                                548                 :                : 
                                549                 :                : 
                                550                 :                : /*
                                551                 :                :  * can_coerce_type()
                                552                 :                :  *      Can input_typeids be coerced to target_typeids?
                                553                 :                :  *
                                554                 :                :  * We must be told the context (CAST construct, assignment, implicit coercion)
                                555                 :                :  * as this determines the set of available casts.
                                556                 :                :  */
                                557                 :                : bool
 2561 peter_e@gmx.net           558                 :CBC     1044670 : can_coerce_type(int nargs, const Oid *input_typeids, const Oid *target_typeids,
                                559                 :                :                 CoercionContext ccontext)
                                560                 :                : {
 8238 tgl@sss.pgh.pa.us         561                 :        1044670 :     bool        have_generics = false;
                                562                 :                :     int         i;
                                563                 :                : 
                                564                 :                :     /* run through argument list... */
10033 lockhart@fourpalms.o      565         [ +  + ]:        1862196 :     for (i = 0; i < nargs; i++)
                                566                 :                :     {
 9329 bruce@momjian.us          567                 :        1216224 :         Oid         inputTypeId = input_typeids[i];
 8440 tgl@sss.pgh.pa.us         568                 :        1216224 :         Oid         targetTypeId = target_typeids[i];
                                569                 :                :         CoercionPathType pathtype;
                                570                 :                :         Oid         funcId;
                                571                 :                : 
                                572                 :                :         /* no problem if same type */
 9356                           573         [ +  + ]:        1216224 :         if (inputTypeId == targetTypeId)
                                574                 :         817526 :             continue;
                                575                 :                : 
                                576                 :                :         /* accept if target is ANY */
 8467                           577         [ +  + ]:         992294 :         if (targetTypeId == ANYOID)
                                578                 :          35804 :             continue;
                                579                 :                : 
                                580                 :                :         /* accept if target is polymorphic, for now */
 6783                           581   [ +  +  +  +  :         956490 :         if (IsPolymorphicType(targetTypeId))
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                           +  +  + ]
                                582                 :                :         {
 3050                           583                 :          98680 :             have_generics = true;   /* do more checking later */
 8238                           584                 :          98680 :             continue;
                                585                 :                :         }
                                586                 :                : 
                                587                 :                :         /*
                                588                 :                :          * If input is an untyped string constant, assume we can convert it to
                                589                 :                :          * anything.
                                590                 :                :          */
 7985                           591         [ +  + ]:         857810 :         if (inputTypeId == UNKNOWNOID)
                                592                 :         328432 :             continue;
                                593                 :                : 
                                594                 :                :         /*
                                595                 :                :          * If pg_cast shows that we can coerce, accept.  This test now covers
                                596                 :                :          * both binary-compatible and coercion-function cases.
                                597                 :                :          */
 6719                           598                 :         529378 :         pathtype = find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
                                599                 :                :                                          &funcId);
                                600         [ +  + ]:         529378 :         if (pathtype != COERCION_PATH_NONE)
 8467                           601                 :         129198 :             continue;
                                602                 :                : 
                                603                 :                :         /*
                                604                 :                :          * If input is RECORD and target is a composite type, assume we can
                                605                 :                :          * coerce (may need tighter checking here)
                                606                 :                :          */
 7840                           607   [ +  +  +  + ]:         401151 :         if (inputTypeId == RECORDOID &&
                                608                 :            971 :             ISCOMPLEX(targetTypeId))
                                609                 :            902 :             continue;
                                610                 :                : 
                                611                 :                :         /*
                                612                 :                :          * If input is a composite type and target is RECORD, accept
                                613                 :                :          */
 7480                           614   [ +  +  +  + ]:         405911 :         if (targetTypeId == RECORDOID &&
                                615                 :           6633 :             ISCOMPLEX(inputTypeId))
                                616                 :            547 :             continue;
                                617                 :                : 
                                618                 :                : #ifdef NOT_USED                 /* not implemented yet */
                                619                 :                : 
                                620                 :                :         /*
                                621                 :                :          * If input is record[] and target is a composite array type, assume
                                622                 :                :          * we can coerce (may need tighter checking here)
                                623                 :                :          */
                                624                 :                :         if (inputTypeId == RECORDARRAYOID &&
                                625                 :                :             is_complex_array(targetTypeId))
                                626                 :                :             continue;
                                627                 :                : #endif
                                628                 :                : 
                                629                 :                :         /*
                                630                 :                :          * If input is a composite array type and target is record[], accept
                                631                 :                :          */
 6223                           632   [ +  +  -  + ]:         398737 :         if (targetTypeId == RECORDARRAYOID &&
                                633                 :              6 :             is_complex_array(inputTypeId))
 6223 tgl@sss.pgh.pa.us         634                 :UBC           0 :             continue;
                                635                 :                : 
                                636                 :                :         /*
                                637                 :                :          * If input is a class type that inherits from target, accept
                                638                 :                :          */
 5413 peter_e@gmx.net           639         [ +  + ]:CBC      398731 :         if (typeInheritsFrom(inputTypeId, targetTypeId)
                                640         [ +  + ]:         398701 :             || typeIsOfTypedTable(inputTypeId, targetTypeId))
 9356 tgl@sss.pgh.pa.us         641                 :             33 :             continue;
                                642                 :                : 
                                643                 :                :         /*
                                644                 :                :          * Else, cannot coerce at this argument position
                                645                 :                :          */
 8457                           646                 :         398698 :         return false;
                                647                 :                :     }
                                648                 :                : 
                                649                 :                :     /* If we found any generic argument types, cross-check them */
 8238                           650         [ +  + ]:         645972 :     if (have_generics)
                                651                 :                :     {
                                652         [ +  + ]:          66236 :         if (!check_generic_type_consistency(input_typeids, target_typeids,
                                653                 :                :                                             nargs))
                                654                 :          38873 :             return false;
                                655                 :                :     }
                                656                 :                : 
10033 lockhart@fourpalms.o      657                 :         607099 :     return true;
                                658                 :                : }
                                659                 :                : 
                                660                 :                : 
                                661                 :                : /*
                                662                 :                :  * Create an expression tree to represent coercion to a domain type.
                                663                 :                :  *
                                664                 :                :  * 'arg': input expression
                                665                 :                :  * 'baseTypeId': base type of domain
                                666                 :                :  * 'baseTypeMod': base type typmod of domain
                                667                 :                :  * 'typeId': target type to coerce to
                                668                 :                :  * 'ccontext': context indicator to control coercions
                                669                 :                :  * 'cformat': coercion display format
                                670                 :                :  * 'location': coercion request location
                                671                 :                :  * 'hideInputCoercion': if true, hide the input coercion under this one.
                                672                 :                :  *
                                673                 :                :  * If the target type isn't a domain, the given 'arg' is returned as-is.
                                674                 :                :  */
                                675                 :                : Node *
 7145 tgl@sss.pgh.pa.us         676                 :         121204 : coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId,
                                677                 :                :                  CoercionContext ccontext, CoercionForm cformat, int location,
                                678                 :                :                  bool hideInputCoercion)
                                679                 :                : {
                                680                 :                :     CoerceToDomain *result;
                                681                 :                : 
                                682                 :                :     /* We now require the caller to supply correct baseTypeId/baseTypeMod */
  271                           683         [ -  + ]:         121204 :     Assert(OidIsValid(baseTypeId));
                                684                 :                : 
                                685                 :                :     /* If it isn't a domain, return the node as it was passed in */
 8302                           686         [ +  + ]:         121204 :     if (baseTypeId == typeId)
                                687                 :          79244 :         return arg;
                                688                 :                : 
                                689                 :                :     /* Suppress display of nested coercion steps */
 7803                           690         [ +  + ]:          41960 :     if (hideInputCoercion)
                                691                 :           1292 :         hide_coercion_node(arg);
                                692                 :                : 
                                693                 :                :     /*
                                694                 :                :      * If the domain applies a typmod to its base type, build the appropriate
                                695                 :                :      * coercion step.  Mark it implicit for display purposes, because we don't
                                696                 :                :      * want it shown separately by ruleutils.c; but the isExplicit flag passed
                                697                 :                :      * to the conversion function depends on the manner in which the domain
                                698                 :                :      * coercion is invoked, so that the semantics of implicit and explicit
                                699                 :                :      * coercion differ.  (Is that really the behavior we want?)
                                700                 :                :      *
                                701                 :                :      * NOTE: because we apply this as part of the fixed expression structure,
                                702                 :                :      * ALTER DOMAIN cannot alter the typtypmod.  But it's unclear that that
                                703                 :                :      * would be safe to do anyway, without lots of knowledge about what the
                                704                 :                :      * base type thinks the typmod means.
                                705                 :                :      */
 2949                           706                 :          41960 :     arg = coerce_type_typmod(arg, baseTypeId, baseTypeMod,
                                707                 :                :                              ccontext, COERCE_IMPLICIT_CAST, location,
                                708                 :                :                              false);
                                709                 :                : 
                                710                 :                :     /*
                                711                 :                :      * Now build the domain coercion node.  This represents run-time checking
                                712                 :                :      * of any constraints currently attached to the domain.  This also ensures
                                713                 :                :      * that the expression is properly labeled as to result type.
                                714                 :                :      */
 8302                           715                 :          41960 :     result = makeNode(CoerceToDomain);
                                716                 :          41960 :     result->arg = (Expr *) arg;
                                717                 :          41960 :     result->resulttype = typeId;
                                718                 :          41960 :     result->resulttypmod = -1;   /* currently, always -1 for domains */
                                719                 :                :     /* resultcollid will be set by parse_collate.c */
                                720                 :          41960 :     result->coercionformat = cformat;
 6269                           721                 :          41960 :     result->location = location;
                                722                 :                : 
 8302                           723                 :          41960 :     return (Node *) result;
                                724                 :                : }
                                725                 :                : 
                                726                 :                : 
                                727                 :                : /*
                                728                 :                :  * coerce_type_typmod()
                                729                 :                :  *      Force a value to a particular typmod, if meaningful and possible.
                                730                 :                :  *
                                731                 :                :  * This is applied to values that are going to be stored in a relation
                                732                 :                :  * (where we have an atttypmod for the column) as well as values being
                                733                 :                :  * explicitly CASTed (where the typmod comes from the target type spec).
                                734                 :                :  *
                                735                 :                :  * The caller must have already ensured that the value is of the correct
                                736                 :                :  * type, typically by applying coerce_type.
                                737                 :                :  *
                                738                 :                :  * ccontext may affect semantics, depending on whether the length coercion
                                739                 :                :  * function pays attention to the isExplicit flag it's passed.
                                740                 :                :  *
                                741                 :                :  * cformat determines the display properties of the generated node (if any).
                                742                 :                :  *
                                743                 :                :  * If hideInputCoercion is true *and* we generate a node, the input node is
                                744                 :                :  * forced to IMPLICIT display form, so that only the typmod coercion node will
                                745                 :                :  * be visible when displaying the expression.
                                746                 :                :  *
                                747                 :                :  * NOTE: this does not need to work on domain types, because any typmod
                                748                 :                :  * coercion for a domain is considered to be part of the type coercion
                                749                 :                :  * needed to produce the domain value in the first place.  So, no getBaseType.
                                750                 :                :  */
                                751                 :                : static Node *
 8440                           752                 :         415142 : coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod,
                                753                 :                :                    CoercionContext ccontext, CoercionForm cformat,
                                754                 :                :                    int location,
                                755                 :                :                    bool hideInputCoercion)
                                756                 :                : {
                                757                 :                :     CoercionPathType pathtype;
                                758                 :                :     Oid         funcId;
                                759                 :                : 
                                760                 :                :     /* Skip coercion if already done */
 1543                           761         [ +  + ]:         415142 :     if (targetTypMod == exprTypmod(node))
 9415                           762                 :         405649 :         return node;
                                763                 :                : 
                                764                 :                :     /* Suppress display of nested coercion steps */
 1543                           765         [ +  + ]:           9493 :     if (hideInputCoercion)
                                766                 :            488 :         hide_coercion_node(node);
                                767                 :                : 
                                768                 :                :     /*
                                769                 :                :      * A negative typmod means that no actual coercion is needed, but we still
                                770                 :                :      * want a RelabelType to ensure that the expression exposes the intended
                                771                 :                :      * typmod.
                                772                 :                :      */
 1411                           773         [ +  + ]:           9493 :     if (targetTypMod < 0)
                                774                 :             15 :         pathtype = COERCION_PATH_NONE;
                                775                 :                :     else
                                776                 :           9478 :         pathtype = find_typmod_coercion_function(targetTypeId, &funcId);
                                777                 :                : 
 6719                           778         [ +  + ]:           9493 :     if (pathtype != COERCION_PATH_NONE)
                                779                 :                :     {
                                780                 :           9472 :         node = build_coercion_expression(node, pathtype, funcId,
                                781                 :                :                                          targetTypeId, targetTypMod,
                                782                 :                :                                          ccontext, cformat, location);
                                783                 :                :     }
                                784                 :                :     else
                                785                 :                :     {
                                786                 :                :         /*
                                787                 :                :          * We don't need to perform any actual coercion step, but we should
                                788                 :                :          * apply a RelabelType to ensure that the expression exposes the
                                789                 :                :          * intended typmod.
                                790                 :                :          */
 1543                           791                 :             21 :         node = applyRelabelType(node, targetTypeId, targetTypMod,
                                792                 :                :                                 exprCollation(node),
                                793                 :                :                                 cformat, location, false);
                                794                 :                :     }
                                795                 :                : 
 7803                           796                 :           9493 :     return node;
                                797                 :                : }
                                798                 :                : 
                                799                 :                : /*
                                800                 :                :  * Mark a coercion node as IMPLICIT so it will never be displayed by
                                801                 :                :  * ruleutils.c.  We use this when we generate a nest of coercion nodes
                                802                 :                :  * to implement what is logically one conversion; the inner nodes are
                                803                 :                :  * forced to IMPLICIT_CAST format.  This does not change their semantics,
                                804                 :                :  * only display behavior.
                                805                 :                :  *
                                806                 :                :  * It is caller error to call this on something that doesn't have a
                                807                 :                :  * CoercionForm field.
                                808                 :                :  */
                                809                 :                : static void
                                810                 :           1780 : hide_coercion_node(Node *node)
                                811                 :                : {
                                812         [ +  + ]:           1780 :     if (IsA(node, FuncExpr))
                                813                 :            826 :         ((FuncExpr *) node)->funcformat = COERCE_IMPLICIT_CAST;
                                814         [ +  + ]:            954 :     else if (IsA(node, RelabelType))
                                815                 :            139 :         ((RelabelType *) node)->relabelformat = COERCE_IMPLICIT_CAST;
 6719                           816         [ +  + ]:            815 :     else if (IsA(node, CoerceViaIO))
                                817                 :            809 :         ((CoerceViaIO *) node)->coerceformat = COERCE_IMPLICIT_CAST;
 6789                           818         [ +  - ]:              6 :     else if (IsA(node, ArrayCoerceExpr))
                                819                 :              6 :         ((ArrayCoerceExpr *) node)->coerceformat = COERCE_IMPLICIT_CAST;
 7625 tgl@sss.pgh.pa.us         820         [ #  # ]:UBC           0 :     else if (IsA(node, ConvertRowtypeExpr))
                                821                 :              0 :         ((ConvertRowtypeExpr *) node)->convertformat = COERCE_IMPLICIT_CAST;
 7803                           822         [ #  # ]:              0 :     else if (IsA(node, RowExpr))
                                823                 :              0 :         ((RowExpr *) node)->row_format = COERCE_IMPLICIT_CAST;
                                824         [ #  # ]:              0 :     else if (IsA(node, CoerceToDomain))
                                825                 :              0 :         ((CoerceToDomain *) node)->coercionformat = COERCE_IMPLICIT_CAST;
                                826                 :                :     else
                                827         [ #  # ]:              0 :         elog(ERROR, "unsupported node type: %d", (int) nodeTag(node));
 7803 tgl@sss.pgh.pa.us         828                 :CBC        1780 : }
                                829                 :                : 
                                830                 :                : /*
                                831                 :                :  * build_coercion_expression()
                                832                 :                :  *      Construct an expression tree for applying a pg_cast entry.
                                833                 :                :  *
                                834                 :                :  * This is used for both type-coercion and length-coercion operations,
                                835                 :                :  * since there is no difference in terms of the calling convention.
                                836                 :                :  */
                                837                 :                : static Node *
 6789                           838                 :          56706 : build_coercion_expression(Node *node,
                                839                 :                :                           CoercionPathType pathtype,
                                840                 :                :                           Oid funcId,
                                841                 :                :                           Oid targetTypeId, int32 targetTypMod,
                                842                 :                :                           CoercionContext ccontext, CoercionForm cformat,
                                843                 :                :                           int location)
                                844                 :                : {
                                845                 :          56706 :     int         nargs = 0;
                                846                 :                : 
                                847         [ +  + ]:          56706 :     if (OidIsValid(funcId))
                                848                 :                :     {
                                849                 :                :         HeapTuple   tp;
                                850                 :                :         Form_pg_proc procstruct;
                                851                 :                : 
 5734 rhaas@postgresql.org      852                 :          42310 :         tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcId));
 6789 tgl@sss.pgh.pa.us         853         [ -  + ]:          42310 :         if (!HeapTupleIsValid(tp))
 6789 tgl@sss.pgh.pa.us         854         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for function %u", funcId);
 6789 tgl@sss.pgh.pa.us         855                 :CBC       42310 :         procstruct = (Form_pg_proc) GETSTRUCT(tp);
                                856                 :                : 
                                857                 :                :         /*
                                858                 :                :          * These Asserts essentially check that function is a legal coercion
                                859                 :                :          * function.  We can't make the seemingly obvious tests on prorettype
                                860                 :                :          * and proargtypes[0], even in the COERCION_PATH_FUNC case, because of
                                861                 :                :          * various binary-compatibility cases.
                                862                 :                :          */
                                863                 :                :         /* Assert(targetTypeId == procstruct->prorettype); */
                                864         [ -  + ]:          42310 :         Assert(!procstruct->proretset);
 2796 peter_e@gmx.net           865         [ -  + ]:          42310 :         Assert(procstruct->prokind == PROKIND_FUNCTION);
 6789 tgl@sss.pgh.pa.us         866                 :          42310 :         nargs = procstruct->pronargs;
                                867   [ +  -  -  + ]:          42310 :         Assert(nargs >= 1 && nargs <= 3);
                                868                 :                :         /* Assert(procstruct->proargtypes.values[0] == exprType(node)); */
                                869   [ +  +  -  + ]:          42310 :         Assert(nargs < 2 || procstruct->proargtypes.values[1] == INT4OID);
                                870   [ +  +  -  + ]:          42310 :         Assert(nargs < 3 || procstruct->proargtypes.values[2] == BOOLOID);
                                871                 :                : 
                                872                 :          42310 :         ReleaseSysCache(tp);
                                873                 :                :     }
                                874                 :                : 
 6719                           875         [ +  + ]:          56706 :     if (pathtype == COERCION_PATH_FUNC)
                                876                 :                :     {
                                877                 :                :         /* We build an ordinary FuncExpr with special arguments */
                                878                 :                :         FuncExpr   *fexpr;
                                879                 :                :         List       *args;
                                880                 :                :         Const      *cons;
                                881                 :                : 
 6789                           882         [ -  + ]:          42271 :         Assert(OidIsValid(funcId));
                                883                 :                : 
                                884                 :          42271 :         args = list_make1(node);
                                885                 :                : 
                                886         [ +  + ]:          42271 :         if (nargs >= 2)
                                887                 :                :         {
                                888                 :                :             /* Pass target typmod as an int4 constant */
                                889                 :          10069 :             cons = makeConst(INT4OID,
                                890                 :                :                              -1,
                                891                 :                :                              InvalidOid,
                                892                 :                :                              sizeof(int32),
                                893                 :                :                              Int32GetDatum(targetTypMod),
                                894                 :                :                              false,
                                895                 :                :                              true);
                                896                 :                : 
                                897                 :          10069 :             args = lappend(args, cons);
                                898                 :                :         }
                                899                 :                : 
                                900         [ +  + ]:          42271 :         if (nargs == 3)
                                901                 :                :         {
                                902                 :                :             /* Pass it a boolean isExplicit parameter, too */
                                903                 :           6216 :             cons = makeConst(BOOLOID,
                                904                 :                :                              -1,
                                905                 :                :                              InvalidOid,
                                906                 :                :                              sizeof(bool),
                                907                 :                :                              BoolGetDatum(ccontext == COERCION_EXPLICIT),
                                908                 :                :                              false,
                                909                 :                :                              true);
                                910                 :                : 
                                911                 :           6216 :             args = lappend(args, cons);
                                912                 :                :         }
                                913                 :                : 
 5336                           914                 :          42271 :         fexpr = makeFuncExpr(funcId, targetTypeId, args,
                                915                 :                :                              InvalidOid, InvalidOid, cformat);
 6269                           916                 :          42271 :         fexpr->location = location;
                                917                 :          42271 :         return (Node *) fexpr;
                                918                 :                :     }
 6719                           919         [ +  + ]:          14435 :     else if (pathtype == COERCION_PATH_ARRAYCOERCE)
                                920                 :                :     {
                                921                 :                :         /* We need to build an ArrayCoerceExpr */
                                922                 :           2820 :         ArrayCoerceExpr *acoerce = makeNode(ArrayCoerceExpr);
 2949                           923                 :           2820 :         CaseTestExpr *ctest = makeNode(CaseTestExpr);
                                924                 :                :         Oid         sourceBaseTypeId;
                                925                 :                :         int32       sourceBaseTypeMod;
                                926                 :                :         Oid         targetElementType;
                                927                 :                :         Node       *elemexpr;
                                928                 :                : 
                                929                 :                :         /*
                                930                 :                :          * Look through any domain over the source array type.  Note we don't
                                931                 :                :          * expect that the target type is a domain; it must be a plain array.
                                932                 :                :          * (To get to a domain target type, we'll do coerce_to_domain later.)
                                933                 :                :          */
                                934                 :           2820 :         sourceBaseTypeMod = exprTypmod(node);
                                935                 :           2820 :         sourceBaseTypeId = getBaseTypeAndTypmod(exprType(node),
                                936                 :                :                                                 &sourceBaseTypeMod);
                                937                 :                : 
                                938                 :                :         /*
                                939                 :                :          * Set up a CaseTestExpr representing one element of the source array.
                                940                 :                :          * This is an abuse of CaseTestExpr, but it's OK as long as there
                                941                 :                :          * can't be any CaseExpr or ArrayCoerceExpr within the completed
                                942                 :                :          * elemexpr.
                                943                 :                :          */
                                944                 :           2820 :         ctest->typeId = get_element_type(sourceBaseTypeId);
                                945         [ -  + ]:           2820 :         Assert(OidIsValid(ctest->typeId));
                                946                 :           2820 :         ctest->typeMod = sourceBaseTypeMod;
                                947                 :           2820 :         ctest->collation = InvalidOid;   /* Assume coercions don't care */
                                948                 :                : 
                                949                 :                :         /* And coerce it to the target element type */
                                950                 :           2820 :         targetElementType = get_element_type(targetTypeId);
                                951         [ -  + ]:           2820 :         Assert(OidIsValid(targetElementType));
                                952                 :                : 
                                953                 :           2820 :         elemexpr = coerce_to_target_type(NULL,
                                954                 :                :                                          (Node *) ctest,
                                955                 :                :                                          ctest->typeId,
                                956                 :                :                                          targetElementType,
                                957                 :                :                                          targetTypMod,
                                958                 :                :                                          ccontext,
                                959                 :                :                                          cformat,
                                960                 :                :                                          location);
                                961         [ -  + ]:           2820 :         if (elemexpr == NULL)   /* shouldn't happen */
 2949 tgl@sss.pgh.pa.us         962         [ #  # ]:UBC           0 :             elog(ERROR, "failed to coerce array element type as expected");
                                963                 :                : 
 6719 tgl@sss.pgh.pa.us         964                 :CBC        2820 :         acoerce->arg = (Expr *) node;
 2949                           965                 :           2820 :         acoerce->elemexpr = (Expr *) elemexpr;
 6719                           966                 :           2820 :         acoerce->resulttype = targetTypeId;
                                967                 :                : 
                                968                 :                :         /*
                                969                 :                :          * Label the output as having a particular element typmod only if we
                                970                 :                :          * ended up with a per-element expression that is labeled that way.
                                971                 :                :          */
 2949                           972                 :           2820 :         acoerce->resulttypmod = exprTypmod(elemexpr);
                                973                 :                :         /* resultcollid will be set by parse_collate.c */
 6719                           974                 :           2820 :         acoerce->coerceformat = cformat;
 6269                           975                 :           2820 :         acoerce->location = location;
                                976                 :                : 
 6719                           977                 :           2820 :         return (Node *) acoerce;
                                978                 :                :     }
                                979         [ +  - ]:          11615 :     else if (pathtype == COERCION_PATH_COERCEVIAIO)
                                980                 :                :     {
                                981                 :                :         /* We need to build a CoerceViaIO node */
                                982                 :          11615 :         CoerceViaIO *iocoerce = makeNode(CoerceViaIO);
                                983                 :                : 
                                984         [ -  + ]:          11615 :         Assert(!OidIsValid(funcId));
                                985                 :                : 
                                986                 :          11615 :         iocoerce->arg = (Expr *) node;
                                987                 :          11615 :         iocoerce->resulttype = targetTypeId;
                                988                 :                :         /* resultcollid will be set by parse_collate.c */
                                989                 :          11615 :         iocoerce->coerceformat = cformat;
 6269                           990                 :          11615 :         iocoerce->location = location;
                                991                 :                : 
 6719                           992                 :          11615 :         return (Node *) iocoerce;
                                993                 :                :     }
                                994                 :                :     else
                                995                 :                :     {
 6719 tgl@sss.pgh.pa.us         996         [ #  # ]:UBC           0 :         elog(ERROR, "unsupported pathtype %d in build_coercion_expression",
                                997                 :                :              (int) pathtype);
                                998                 :                :         return NULL;            /* keep compiler quiet */
                                999                 :                :     }
                               1000                 :                : }
                               1001                 :                : 
                               1002                 :                : 
                               1003                 :                : /*
                               1004                 :                :  * coerce_record_to_complex
                               1005                 :                :  *      Coerce a RECORD to a specific composite type.
                               1006                 :                :  *
                               1007                 :                :  * Currently we only support this for inputs that are RowExprs or whole-row
                               1008                 :                :  * Vars.
                               1009                 :                :  */
                               1010                 :                : static Node *
 7840 tgl@sss.pgh.pa.us        1011                 :CBC         902 : coerce_record_to_complex(ParseState *pstate, Node *node,
                               1012                 :                :                          Oid targetTypeId,
                               1013                 :                :                          CoercionContext ccontext,
                               1014                 :                :                          CoercionForm cformat,
                               1015                 :                :                          int location)
                               1016                 :                : {
                               1017                 :                :     RowExpr    *rowexpr;
                               1018                 :                :     Oid         baseTypeId;
 2923                          1019                 :            902 :     int32       baseTypeMod = -1;
                               1020                 :                :     TupleDesc   tupdesc;
 7840                          1021                 :            902 :     List       *args = NIL;
                               1022                 :                :     List       *newargs;
                               1023                 :                :     int         i;
                               1024                 :                :     int         ucolno;
                               1025                 :                :     ListCell   *arg;
                               1026                 :                : 
                               1027   [ +  -  +  - ]:            902 :     if (node && IsA(node, RowExpr))
                               1028                 :                :     {
                               1029                 :                :         /*
                               1030                 :                :          * Since the RowExpr must be of type RECORD, we needn't worry about it
                               1031                 :                :          * containing any dropped columns.
                               1032                 :                :          */
                               1033                 :            902 :         args = ((RowExpr *) node)->args;
                               1034                 :                :     }
 7840 tgl@sss.pgh.pa.us        1035   [ #  #  #  # ]:UBC           0 :     else if (node && IsA(node, Var) &&
                               1036         [ #  # ]:              0 :              ((Var *) node)->varattno == InvalidAttrNumber)
                               1037                 :              0 :     {
 7729 bruce@momjian.us         1038                 :              0 :         int         rtindex = ((Var *) node)->varno;
                               1039                 :              0 :         int         sublevels_up = ((Var *) node)->varlevelsup;
 6265 tgl@sss.pgh.pa.us        1040                 :              0 :         int         vlocation = ((Var *) node)->location;
                               1041                 :                :         ParseNamespaceItem *nsitem;
                               1042                 :                : 
 2125                          1043                 :              0 :         nsitem = GetNSItemByRangeTablePosn(pstate, rtindex, sublevels_up);
 1001                          1044                 :              0 :         args = expandNSItemVars(pstate, nsitem, sublevels_up, vlocation, NULL);
                               1045                 :                :     }
                               1046                 :                :     else
 7840                          1047         [ #  # ]:              0 :         ereport(ERROR,
                               1048                 :                :                 (errcode(ERRCODE_CANNOT_COERCE),
                               1049                 :                :                  errmsg("cannot cast type %s to %s",
                               1050                 :                :                         format_type_be(RECORDOID),
                               1051                 :                :                         format_type_be(targetTypeId)),
                               1052                 :                :                  parser_coercion_errposition(pstate, location, node)));
                               1053                 :                : 
                               1054                 :                :     /*
                               1055                 :                :      * Look up the composite type, accounting for possibility that what we are
                               1056                 :                :      * given is a domain over composite.
                               1057                 :                :      */
 2923 tgl@sss.pgh.pa.us        1058                 :CBC         902 :     baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
                               1059                 :            902 :     tupdesc = lookup_rowtype_tupdesc(baseTypeId, baseTypeMod);
                               1060                 :                : 
                               1061                 :                :     /* Process the fields */
 7840                          1062                 :            902 :     newargs = NIL;
 7741                          1063                 :            902 :     ucolno = 1;
                               1064                 :            902 :     arg = list_head(args);
                               1065         [ +  + ]:           2958 :     for (i = 0; i < tupdesc->natts; i++)
                               1066                 :                :     {
                               1067                 :                :         Node       *expr;
                               1068                 :                :         Node       *cexpr;
                               1069                 :                :         Oid         exprtype;
 2990 andres@anarazel.de       1070                 :           2056 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
                               1071                 :                : 
                               1072                 :                :         /* Fill in NULLs for dropped columns in rowtype */
                               1073         [ +  + ]:           2056 :         if (attr->attisdropped)
                               1074                 :                :         {
                               1075                 :                :             /*
                               1076                 :                :              * can't use atttypid here, but it doesn't really matter what type
                               1077                 :                :              * the Const claims to be.
                               1078                 :                :              */
 5330 tgl@sss.pgh.pa.us        1079                 :              3 :             newargs = lappend(newargs,
                               1080                 :              3 :                               makeNullConst(INT4OID, -1, InvalidOid));
 7741                          1081                 :              3 :             continue;
                               1082                 :                :         }
                               1083                 :                : 
                               1084         [ -  + ]:           2053 :         if (arg == NULL)
 7741 tgl@sss.pgh.pa.us        1085         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1086                 :                :                     (errcode(ERRCODE_CANNOT_COERCE),
                               1087                 :                :                      errmsg("cannot cast type %s to %s",
                               1088                 :                :                             format_type_be(RECORDOID),
                               1089                 :                :                             format_type_be(targetTypeId)),
                               1090                 :                :                      errdetail("Input has too few columns."),
                               1091                 :                :                      parser_coercion_errposition(pstate, location, node)));
 7741 tgl@sss.pgh.pa.us        1092                 :CBC        2053 :         expr = (Node *) lfirst(arg);
                               1093                 :           2053 :         exprtype = exprType(expr);
                               1094                 :                : 
 6269                          1095                 :           2053 :         cexpr = coerce_to_target_type(pstate,
                               1096                 :                :                                       expr, exprtype,
                               1097                 :                :                                       attr->atttypid,
                               1098                 :                :                                       attr->atttypmod,
                               1099                 :                :                                       ccontext,
                               1100                 :                :                                       COERCE_IMPLICIT_CAST,
                               1101                 :                :                                       -1);
                               1102         [ -  + ]:           2053 :         if (cexpr == NULL)
 7840 tgl@sss.pgh.pa.us        1103         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1104                 :                :                     (errcode(ERRCODE_CANNOT_COERCE),
                               1105                 :                :                      errmsg("cannot cast type %s to %s",
                               1106                 :                :                             format_type_be(RECORDOID),
                               1107                 :                :                             format_type_be(targetTypeId)),
                               1108                 :                :                      errdetail("Cannot cast type %s to %s in column %d.",
                               1109                 :                :                                format_type_be(exprtype),
                               1110                 :                :                                format_type_be(attr->atttypid),
                               1111                 :                :                                ucolno),
                               1112                 :                :                      parser_coercion_errposition(pstate, location, expr)));
 6269 tgl@sss.pgh.pa.us        1113                 :CBC        2053 :         newargs = lappend(newargs, cexpr);
 7741                          1114                 :           2053 :         ucolno++;
 2296                          1115                 :           2053 :         arg = lnext(args, arg);
                               1116                 :                :     }
 7741                          1117         [ -  + ]:            902 :     if (arg != NULL)
 7741 tgl@sss.pgh.pa.us        1118         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1119                 :                :                 (errcode(ERRCODE_CANNOT_COERCE),
                               1120                 :                :                  errmsg("cannot cast type %s to %s",
                               1121                 :                :                         format_type_be(RECORDOID),
                               1122                 :                :                         format_type_be(targetTypeId)),
                               1123                 :                :                  errdetail("Input has too many columns."),
                               1124                 :                :                  parser_coercion_errposition(pstate, location, node)));
                               1125                 :                : 
 7073 tgl@sss.pgh.pa.us        1126         [ +  - ]:CBC         902 :     ReleaseTupleDesc(tupdesc);
                               1127                 :                : 
 7840                          1128                 :            902 :     rowexpr = makeNode(RowExpr);
                               1129                 :            902 :     rowexpr->args = newargs;
 2923                          1130                 :            902 :     rowexpr->row_typeid = baseTypeId;
 7840                          1131                 :            902 :     rowexpr->row_format = cformat;
 6230                          1132                 :            902 :     rowexpr->colnames = NIL; /* not needed for named target type */
 6269                          1133                 :            902 :     rowexpr->location = location;
                               1134                 :                : 
                               1135                 :                :     /* If target is a domain, apply constraints */
 2923                          1136         [ +  + ]:            902 :     if (baseTypeId != targetTypeId)
                               1137                 :                :     {
                               1138                 :             77 :         rowexpr->row_format = COERCE_IMPLICIT_CAST;
                               1139                 :             77 :         return coerce_to_domain((Node *) rowexpr,
                               1140                 :                :                                 baseTypeId, baseTypeMod,
                               1141                 :                :                                 targetTypeId,
                               1142                 :                :                                 ccontext, cformat, location,
                               1143                 :                :                                 false);
                               1144                 :                :     }
                               1145                 :                : 
 7840                          1146                 :            825 :     return (Node *) rowexpr;
                               1147                 :                : }
                               1148                 :                : 
                               1149                 :                : /*
                               1150                 :                :  * coerce_to_boolean()
                               1151                 :                :  *      Coerce an argument of a construct that requires boolean input
                               1152                 :                :  *      (AND, OR, NOT, etc).  Also check that input is not a set.
                               1153                 :                :  *
                               1154                 :                :  * Returns the possibly-transformed node tree.
                               1155                 :                :  *
                               1156                 :                :  * As with coerce_type, pstate may be NULL if no special unknown-Param
                               1157                 :                :  * processing is wanted.
                               1158                 :                :  */
                               1159                 :                : Node *
 8217                          1160                 :         397602 : coerce_to_boolean(ParseState *pstate, Node *node,
                               1161                 :                :                   const char *constructName)
                               1162                 :                : {
 8569                          1163                 :         397602 :     Oid         inputTypeId = exprType(node);
                               1164                 :                : 
                               1165         [ +  + ]:         397602 :     if (inputTypeId != BOOLOID)
                               1166                 :                :     {
                               1167                 :                :         Node       *newnode;
                               1168                 :                : 
 6269                          1169                 :             36 :         newnode = coerce_to_target_type(pstate, node, inputTypeId,
                               1170                 :                :                                         BOOLOID, -1,
                               1171                 :                :                                         COERCION_ASSIGNMENT,
                               1172                 :                :                                         COERCE_IMPLICIT_CAST,
                               1173                 :                :                                         -1);
                               1174         [ +  + ]:             36 :         if (newnode == NULL)
 8136                          1175         [ +  - ]:              3 :             ereport(ERROR,
                               1176                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1177                 :                :             /* translator: first %s is name of a SQL construct, eg WHERE */
                               1178                 :                :                      errmsg("argument of %s must be type %s, not type %s",
                               1179                 :                :                             constructName, "boolean",
                               1180                 :                :                             format_type_be(inputTypeId)),
                               1181                 :                :                      parser_errposition(pstate, exprLocation(node))));
 6269                          1182                 :             33 :         node = newnode;
                               1183                 :                :     }
                               1184                 :                : 
 8569                          1185         [ -  + ]:         397599 :     if (expression_returns_set(node))
 8136 tgl@sss.pgh.pa.us        1186         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1187                 :                :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1188                 :                :         /* translator: %s is name of a SQL construct, eg WHERE */
                               1189                 :                :                  errmsg("argument of %s must not return a set",
                               1190                 :                :                         constructName),
                               1191                 :                :                  parser_errposition(pstate, exprLocation(node))));
                               1192                 :                : 
 8152 tgl@sss.pgh.pa.us        1193                 :CBC      397599 :     return node;
                               1194                 :                : }
                               1195                 :                : 
                               1196                 :                : /*
                               1197                 :                :  * coerce_to_specific_type_typmod()
                               1198                 :                :  *      Coerce an argument of a construct that requires a specific data type,
                               1199                 :                :  *      with a specific typmod.  Also check that input is not a set.
                               1200                 :                :  *
                               1201                 :                :  * Returns the possibly-transformed node tree.
                               1202                 :                :  *
                               1203                 :                :  * As with coerce_type, pstate may be NULL if no special unknown-Param
                               1204                 :                :  * processing is wanted.
                               1205                 :                :  */
                               1206                 :                : Node *
 3155 alvherre@alvh.no-ip.     1207                 :           8362 : coerce_to_specific_type_typmod(ParseState *pstate, Node *node,
                               1208                 :                :                                Oid targetTypeId, int32 targetTypmod,
                               1209                 :                :                                const char *constructName)
                               1210                 :                : {
 6885 peter_e@gmx.net          1211                 :           8362 :     Oid         inputTypeId = exprType(node);
                               1212                 :                : 
 6882 tgl@sss.pgh.pa.us        1213         [ +  + ]:           8362 :     if (inputTypeId != targetTypeId)
                               1214                 :                :     {
                               1215                 :                :         Node       *newnode;
                               1216                 :                : 
 6269                          1217                 :           5694 :         newnode = coerce_to_target_type(pstate, node, inputTypeId,
                               1218                 :                :                                         targetTypeId, targetTypmod,
                               1219                 :                :                                         COERCION_ASSIGNMENT,
                               1220                 :                :                                         COERCE_IMPLICIT_CAST,
                               1221                 :                :                                         -1);
                               1222         [ +  + ]:           5688 :         if (newnode == NULL)
 6885 peter_e@gmx.net          1223         [ +  - ]:              3 :             ereport(ERROR,
                               1224                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1225                 :                :             /* translator: first %s is name of a SQL construct, eg LIMIT */
                               1226                 :                :                      errmsg("argument of %s must be type %s, not type %s",
                               1227                 :                :                             constructName,
                               1228                 :                :                             format_type_be(targetTypeId),
                               1229                 :                :                             format_type_be(inputTypeId)),
                               1230                 :                :                      parser_errposition(pstate, exprLocation(node))));
 6269 tgl@sss.pgh.pa.us        1231                 :           5685 :         node = newnode;
                               1232                 :                :     }
                               1233                 :                : 
 6885 peter_e@gmx.net          1234         [ -  + ]:           8353 :     if (expression_returns_set(node))
 6885 peter_e@gmx.net          1235         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1236                 :                :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1237                 :                :         /* translator: %s is name of a SQL construct, eg LIMIT */
                               1238                 :                :                  errmsg("argument of %s must not return a set",
                               1239                 :                :                         constructName),
                               1240                 :                :                  parser_errposition(pstate, exprLocation(node))));
                               1241                 :                : 
 6885 peter_e@gmx.net          1242                 :CBC        8353 :     return node;
                               1243                 :                : }
                               1244                 :                : 
                               1245                 :                : /*
                               1246                 :                :  * coerce_to_specific_type()
                               1247                 :                :  *      Coerce an argument of a construct that requires a specific data type.
                               1248                 :                :  *      Also check that input is not a set.
                               1249                 :                :  *
                               1250                 :                :  * Returns the possibly-transformed node tree.
                               1251                 :                :  *
                               1252                 :                :  * As with coerce_type, pstate may be NULL if no special unknown-Param
                               1253                 :                :  * processing is wanted.
                               1254                 :                :  */
                               1255                 :                : Node *
 3155 alvherre@alvh.no-ip.     1256                 :           8334 : coerce_to_specific_type(ParseState *pstate, Node *node,
                               1257                 :                :                         Oid targetTypeId,
                               1258                 :                :                         const char *constructName)
                               1259                 :                : {
                               1260                 :           8334 :     return coerce_to_specific_type_typmod(pstate, node,
                               1261                 :                :                                           targetTypeId, -1,
                               1262                 :                :                                           constructName);
                               1263                 :                : }
                               1264                 :                : 
                               1265                 :                : /*
                               1266                 :                :  * coerce_null_to_domain()
                               1267                 :                :  *      Build a NULL constant, then wrap it in CoerceToDomain
                               1268                 :                :  *      if the desired type is a domain type.  This allows any
                               1269                 :                :  *      NOT NULL domain constraint to be enforced at runtime.
                               1270                 :                :  */
                               1271                 :                : Node *
  271 tgl@sss.pgh.pa.us        1272                 :           8970 : coerce_null_to_domain(Oid typid, int32 typmod, Oid collation,
                               1273                 :                :                       int typlen, bool typbyval)
                               1274                 :                : {
                               1275                 :                :     Node       *result;
                               1276                 :                :     Oid         baseTypeId;
                               1277                 :           8970 :     int32       baseTypeMod = typmod;
                               1278                 :                : 
                               1279                 :                :     /*
                               1280                 :                :      * The constant must appear to have the domain's base type/typmod, else
                               1281                 :                :      * coerce_to_domain() will apply a length coercion which is useless.
                               1282                 :                :      */
                               1283                 :           8970 :     baseTypeId = getBaseTypeAndTypmod(typid, &baseTypeMod);
                               1284                 :           8970 :     result = (Node *) makeConst(baseTypeId,
                               1285                 :                :                                 baseTypeMod,
                               1286                 :                :                                 collation,
                               1287                 :                :                                 typlen,
                               1288                 :                :                                 (Datum) 0,
                               1289                 :                :                                 true,   /* isnull */
                               1290                 :                :                                 typbyval);
                               1291         [ +  + ]:           8970 :     if (typid != baseTypeId)
                               1292                 :             33 :         result = coerce_to_domain(result,
                               1293                 :                :                                   baseTypeId, baseTypeMod,
                               1294                 :                :                                   typid,
                               1295                 :                :                                   COERCION_IMPLICIT,
                               1296                 :                :                                   COERCE_IMPLICIT_CAST,
                               1297                 :                :                                   -1,
                               1298                 :                :                                   false);
                               1299                 :           8970 :     return result;
                               1300                 :                : }
                               1301                 :                : 
                               1302                 :                : /*
                               1303                 :                :  * parser_coercion_errposition - report coercion error location, if possible
                               1304                 :                :  *
                               1305                 :                :  * We prefer to point at the coercion request (CAST, ::, etc) if possible;
                               1306                 :                :  * but there may be no such location in the case of an implicit coercion.
                               1307                 :                :  * In that case point at the input expression.
                               1308                 :                :  *
                               1309                 :                :  * XXX possibly this is more generally useful than coercion errors;
                               1310                 :                :  * if so, should rename and place with parser_errposition.
                               1311                 :                :  */
                               1312                 :                : int
 6269                          1313                 :             11 : parser_coercion_errposition(ParseState *pstate,
                               1314                 :                :                             int coerce_location,
                               1315                 :                :                             Node *input_expr)
                               1316                 :                : {
                               1317         [ +  - ]:             11 :     if (coerce_location >= 0)
 2042                          1318                 :             11 :         return parser_errposition(pstate, coerce_location);
                               1319                 :                :     else
 2042 tgl@sss.pgh.pa.us        1320                 :UBC           0 :         return parser_errposition(pstate, exprLocation(input_expr));
                               1321                 :                : }
                               1322                 :                : 
                               1323                 :                : 
                               1324                 :                : /*
                               1325                 :                :  * select_common_type()
                               1326                 :                :  *      Determine the common supertype of a list of input expressions.
                               1327                 :                :  *      This is used for determining the output type of CASE, UNION,
                               1328                 :                :  *      and similar constructs.
                               1329                 :                :  *
                               1330                 :                :  * 'exprs' is a *nonempty* list of expressions.  Note that earlier items
                               1331                 :                :  * in the list will be preferred if there is doubt.
                               1332                 :                :  * 'context' is a phrase to use in the error message if we fail to select
                               1333                 :                :  * a usable type.  Pass NULL to have the routine return InvalidOid
                               1334                 :                :  * rather than throwing an error on failure.
                               1335                 :                :  * 'which_expr': if not NULL, receives a pointer to the particular input
                               1336                 :                :  * expression from which the result type was taken.
                               1337                 :                :  *
                               1338                 :                :  * Caution: "failure" just means that there were inputs of different type
                               1339                 :                :  * categories.  It is not guaranteed that all the inputs are coercible to the
                               1340                 :                :  * selected type; caller must check that (see verify_common_type).
                               1341                 :                :  */
                               1342                 :                : Oid
 6269 tgl@sss.pgh.pa.us        1343                 :CBC       71339 : select_common_type(ParseState *pstate, List *exprs, const char *context,
                               1344                 :                :                    Node **which_expr)
                               1345                 :                : {
                               1346                 :                :     Node       *pexpr;
                               1347                 :                :     Oid         ptype;
                               1348                 :                :     TYPCATEGORY pcategory;
                               1349                 :                :     bool        pispreferred;
                               1350                 :                :     ListCell   *lc;
                               1351                 :                : 
                               1352         [ -  + ]:          71339 :     Assert(exprs != NIL);
                               1353                 :          71339 :     pexpr = (Node *) linitial(exprs);
 2296                          1354                 :          71339 :     lc = list_second_cell(exprs);
 6269                          1355                 :          71339 :     ptype = exprType(pexpr);
                               1356                 :                : 
                               1357                 :                :     /*
                               1358                 :                :      * If all input types are valid and exactly the same, just pick that type.
                               1359                 :                :      * This is the only way that we will resolve the result as being a domain
                               1360                 :                :      * type; otherwise domains are smashed to their base types for comparison.
                               1361                 :                :      */
 6545                          1362         [ +  + ]:          71339 :     if (ptype != UNKNOWNOID)
                               1363                 :                :     {
 2296                          1364   [ +  -  +  +  :          94037 :         for_each_cell(lc, exprs, lc)
                                              +  + ]
                               1365                 :                :         {
 5982 bruce@momjian.us         1366                 :          59533 :             Node       *nexpr = (Node *) lfirst(lc);
                               1367                 :          59533 :             Oid         ntype = exprType(nexpr);
                               1368                 :                : 
 6545 tgl@sss.pgh.pa.us        1369         [ +  + ]:          59533 :             if (ntype != ptype)
                               1370                 :          20114 :                 break;
                               1371                 :                :         }
 6269                          1372         [ +  + ]:          54618 :         if (lc == NULL)         /* got to the end of the list? */
                               1373                 :                :         {
                               1374         [ +  + ]:          34504 :             if (which_expr)
                               1375                 :          22951 :                 *which_expr = pexpr;
 6545                          1376                 :          34504 :             return ptype;
                               1377                 :                :         }
                               1378                 :                :     }
                               1379                 :                : 
                               1380                 :                :     /*
                               1381                 :                :      * Nope, so set up for the full algorithm.  Note that at this point, lc
                               1382                 :                :      * points to the first list item with type different from pexpr's; we need
                               1383                 :                :      * not re-examine any items the previous loop advanced over.
                               1384                 :                :      */
                               1385                 :          36835 :     ptype = getBaseType(ptype);
 6298                          1386                 :          36835 :     get_type_category_preferred(ptype, &pcategory, &pispreferred);
                               1387                 :                : 
 2296                          1388   [ +  -  +  +  :         108244 :     for_each_cell(lc, exprs, lc)
                                              +  + ]
                               1389                 :                :     {
 6269                          1390                 :          71417 :         Node       *nexpr = (Node *) lfirst(lc);
                               1391                 :          71417 :         Oid         ntype = getBaseType(exprType(nexpr));
                               1392                 :                : 
                               1393                 :                :         /* move on to next one if no new information... */
 6545                          1394   [ +  +  +  + ]:          71417 :         if (ntype != UNKNOWNOID && ntype != ptype)
                               1395                 :                :         {
                               1396                 :                :             TYPCATEGORY ncategory;
                               1397                 :                :             bool        nispreferred;
                               1398                 :                : 
 6298                          1399                 :          13629 :             get_type_category_preferred(ntype, &ncategory, &nispreferred);
 6545                          1400         [ +  + ]:          13629 :             if (ptype == UNKNOWNOID)
                               1401                 :                :             {
                               1402                 :                :                 /* so far, only unknowns so take anything... */
 6269                          1403                 :           9626 :                 pexpr = nexpr;
 9153                          1404                 :           9626 :                 ptype = ntype;
 6298                          1405                 :           9626 :                 pcategory = ncategory;
                               1406                 :           9626 :                 pispreferred = nispreferred;
                               1407                 :                :             }
                               1408         [ +  + ]:           4003 :             else if (ncategory != pcategory)
                               1409                 :                :             {
                               1410                 :                :                 /*
                               1411                 :                :                  * both types in different categories? then not much hope...
                               1412                 :                :                  */
 6211                          1413         [ +  + ]:              8 :                 if (context == NULL)
                               1414                 :              2 :                     return InvalidOid;
 8136                          1415         [ +  - ]:              6 :                 ereport(ERROR,
                               1416                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1417                 :                :                 /*------
                               1418                 :                :                   translator: first %s is name of a SQL construct, eg CASE */
                               1419                 :                :                          errmsg("%s types %s and %s cannot be matched",
                               1420                 :                :                                 context,
                               1421                 :                :                                 format_type_be(ptype),
                               1422                 :                :                                 format_type_be(ntype)),
                               1423                 :                :                          parser_errposition(pstate, exprLocation(nexpr))));
                               1424                 :                :             }
 6298                          1425   [ +  +  +  + ]:           5212 :             else if (!pispreferred &&
 7317 bruce@momjian.us         1426                 :           1217 :                      can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
                               1427         [ +  + ]:            783 :                      !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
                               1428                 :                :             {
                               1429                 :                :                 /*
                               1430                 :                :                  * take new type if can coerce to it implicitly but not the
                               1431                 :                :                  * other way; but if we have a preferred type, stay on it.
                               1432                 :                :                  */
 6269 tgl@sss.pgh.pa.us        1433                 :            687 :                 pexpr = nexpr;
 9153                          1434                 :            687 :                 ptype = ntype;
 6298                          1435                 :            687 :                 pcategory = ncategory;
                               1436                 :            687 :                 pispreferred = nispreferred;
                               1437                 :                :             }
                               1438                 :                :         }
                               1439                 :                :     }
                               1440                 :                : 
                               1441                 :                :     /*
                               1442                 :                :      * If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
                               1443                 :                :      * then resolve as type TEXT.  This situation comes up with constructs
                               1444                 :                :      * like SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END); SELECT 'foo'
                               1445                 :                :      * UNION SELECT 'bar'; It might seem desirable to leave the construct's
                               1446                 :                :      * output type as UNKNOWN, but that really doesn't work, because we'd
                               1447                 :                :      * probably end up needing a runtime coercion from UNKNOWN to something
                               1448                 :                :      * else, and we usually won't have it.  We need to coerce the unknown
                               1449                 :                :      * literals while they are still literals, so a decision has to be made
                               1450                 :                :      * now.
                               1451                 :                :      */
 9118                          1452         [ +  + ]:          36827 :     if (ptype == UNKNOWNOID)
                               1453                 :           7095 :         ptype = TEXTOID;
                               1454                 :                : 
 6269                          1455         [ +  + ]:          36827 :     if (which_expr)
                               1456                 :           7593 :         *which_expr = pexpr;
 9153                          1457                 :          36827 :     return ptype;
                               1458                 :                : }
                               1459                 :                : 
                               1460                 :                : /*
                               1461                 :                :  * select_common_type_from_oids()
                               1462                 :                :  *      Determine the common supertype of an array of type OIDs.
                               1463                 :                :  *
                               1464                 :                :  * This is the same logic as select_common_type(), but working from
                               1465                 :                :  * an array of type OIDs not a list of expressions.  As in that function,
                               1466                 :                :  * earlier entries in the array have some preference over later ones.
                               1467                 :                :  * On failure, return InvalidOid if noerror is true, else throw an error.
                               1468                 :                :  *
                               1469                 :                :  * Caution: "failure" just means that there were inputs of different type
                               1470                 :                :  * categories.  It is not guaranteed that all the inputs are coercible to the
                               1471                 :                :  * selected type; caller must check that (see verify_common_type_from_oids).
                               1472                 :                :  *
                               1473                 :                :  * Note: neither caller will pass any UNKNOWNOID entries, so the tests
                               1474                 :                :  * for that in this function are dead code.  However, they don't cost much,
                               1475                 :                :  * and it seems better to keep this logic as close to select_common_type()
                               1476                 :                :  * as possible.
                               1477                 :                :  */
                               1478                 :                : static Oid
 2048                          1479                 :           3738 : select_common_type_from_oids(int nargs, const Oid *typeids, bool noerror)
                               1480                 :                : {
                               1481                 :                :     Oid         ptype;
                               1482                 :                :     TYPCATEGORY pcategory;
                               1483                 :                :     bool        pispreferred;
                               1484                 :           3738 :     int         i = 1;
                               1485                 :                : 
                               1486         [ -  + ]:           3738 :     Assert(nargs > 0);
                               1487                 :           3738 :     ptype = typeids[0];
                               1488                 :                : 
                               1489                 :                :     /* If all input types are valid and exactly the same, pick that type. */
                               1490         [ +  - ]:           3738 :     if (ptype != UNKNOWNOID)
                               1491                 :                :     {
                               1492         [ +  + ]:           5436 :         for (; i < nargs; i++)
                               1493                 :                :         {
                               1494         [ +  + ]:           2247 :             if (typeids[i] != ptype)
                               1495                 :            549 :                 break;
                               1496                 :                :         }
                               1497         [ +  + ]:           3738 :         if (i == nargs)
                               1498                 :           3189 :             return ptype;
                               1499                 :                :     }
                               1500                 :                : 
                               1501                 :                :     /*
                               1502                 :                :      * Nope, so set up for the full algorithm.  Note that at this point, we
                               1503                 :                :      * can skip array entries before "i"; they are all equal to ptype.
                               1504                 :                :      */
                               1505                 :            549 :     ptype = getBaseType(ptype);
                               1506                 :            549 :     get_type_category_preferred(ptype, &pcategory, &pispreferred);
                               1507                 :                : 
                               1508         [ +  + ]:            801 :     for (; i < nargs; i++)
                               1509                 :                :     {
                               1510                 :            576 :         Oid         ntype = getBaseType(typeids[i]);
                               1511                 :                : 
                               1512                 :                :         /* move on to next one if no new information... */
                               1513   [ +  -  +  + ]:            576 :         if (ntype != UNKNOWNOID && ntype != ptype)
                               1514                 :                :         {
                               1515                 :                :             TYPCATEGORY ncategory;
                               1516                 :                :             bool        nispreferred;
                               1517                 :                : 
                               1518                 :            570 :             get_type_category_preferred(ntype, &ncategory, &nispreferred);
                               1519         [ -  + ]:            570 :             if (ptype == UNKNOWNOID)
                               1520                 :                :             {
                               1521                 :                :                 /* so far, only unknowns so take anything... */
 2048 tgl@sss.pgh.pa.us        1522                 :UBC           0 :                 ptype = ntype;
                               1523                 :              0 :                 pcategory = ncategory;
                               1524                 :              0 :                 pispreferred = nispreferred;
                               1525                 :                :             }
 2048 tgl@sss.pgh.pa.us        1526         [ +  + ]:CBC         570 :             else if (ncategory != pcategory)
                               1527                 :                :             {
                               1528                 :                :                 /*
                               1529                 :                :                  * both types in different categories? then not much hope...
                               1530                 :                :                  */
                               1531         [ +  - ]:            324 :                 if (noerror)
                               1532                 :            324 :                     return InvalidOid;
 2048 tgl@sss.pgh.pa.us        1533         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               1534                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               1535                 :                :                          errmsg("argument types %s and %s cannot be matched",
                               1536                 :                :                                 format_type_be(ptype),
                               1537                 :                :                                 format_type_be(ntype))));
                               1538                 :                :             }
 2048 tgl@sss.pgh.pa.us        1539   [ +  +  +  + ]:CBC         456 :             else if (!pispreferred &&
                               1540                 :            210 :                      can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
                               1541         [ +  - ]:            135 :                      !can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT))
                               1542                 :                :             {
                               1543                 :                :                 /*
                               1544                 :                :                  * take new type if can coerce to it implicitly but not the
                               1545                 :                :                  * other way; but if we have a preferred type, stay on it.
                               1546                 :                :                  */
                               1547                 :            135 :                 ptype = ntype;
                               1548                 :            135 :                 pcategory = ncategory;
                               1549                 :            135 :                 pispreferred = nispreferred;
                               1550                 :                :             }
                               1551                 :                :         }
                               1552                 :                :     }
                               1553                 :                : 
                               1554                 :                :     /* Like select_common_type(), choose TEXT if all inputs were UNKNOWN */
                               1555         [ -  + ]:            225 :     if (ptype == UNKNOWNOID)
 2048 tgl@sss.pgh.pa.us        1556                 :UBC           0 :         ptype = TEXTOID;
                               1557                 :                : 
 2048 tgl@sss.pgh.pa.us        1558                 :CBC         225 :     return ptype;
                               1559                 :                : }
                               1560                 :                : 
                               1561                 :                : /*
                               1562                 :                :  * coerce_to_common_type()
                               1563                 :                :  *      Coerce an expression to the given type.
                               1564                 :                :  *
                               1565                 :                :  * This is used following select_common_type() to coerce the individual
                               1566                 :                :  * expressions to the desired type.  'context' is a phrase to use in the
                               1567                 :                :  * error message if we fail to coerce.
                               1568                 :                :  *
                               1569                 :                :  * As with coerce_type, pstate may be NULL if no special unknown-Param
                               1570                 :                :  * processing is wanted.
                               1571                 :                :  */
                               1572                 :                : Node *
 8217                          1573                 :         171795 : coerce_to_common_type(ParseState *pstate, Node *node,
                               1574                 :                :                       Oid targetTypeId, const char *context)
                               1575                 :                : {
 9153                          1576                 :         171795 :     Oid         inputTypeId = exprType(node);
                               1577                 :                : 
                               1578         [ +  + ]:         171795 :     if (inputTypeId == targetTypeId)
                               1579                 :          97788 :         return node;            /* no work */
 8440                          1580         [ +  - ]:          74007 :     if (can_coerce_type(1, &inputTypeId, &targetTypeId, COERCION_IMPLICIT))
 7803                          1581                 :          74007 :         node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1,
                               1582                 :                :                            COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
                               1583                 :                :     else
 8136 tgl@sss.pgh.pa.us        1584         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1585                 :                :                 (errcode(ERRCODE_CANNOT_COERCE),
                               1586                 :                :         /* translator: first %s is name of a SQL construct, eg CASE */
                               1587                 :                :                  errmsg("%s could not convert type %s to %s",
                               1588                 :                :                         context,
                               1589                 :                :                         format_type_be(inputTypeId),
                               1590                 :                :                         format_type_be(targetTypeId)),
                               1591                 :                :                  parser_errposition(pstate, exprLocation(node))));
 9153 tgl@sss.pgh.pa.us        1592                 :CBC       74004 :     return node;
                               1593                 :                : }
                               1594                 :                : 
                               1595                 :                : /*
                               1596                 :                :  * verify_common_type()
                               1597                 :                :  *      Verify that all input types can be coerced to a proposed common type.
                               1598                 :                :  *      Return true if so, false if not all coercions are possible.
                               1599                 :                :  *
                               1600                 :                :  * Most callers of select_common_type() don't need to do this explicitly
                               1601                 :                :  * because the checks will happen while trying to convert input expressions
                               1602                 :                :  * to the right type, e.g. in coerce_to_common_type().  However, if a separate
                               1603                 :                :  * check step is needed to validate the applicability of the common type, call
                               1604                 :                :  * this.
                               1605                 :                :  */
                               1606                 :                : bool
 1367                          1607                 :           9270 : verify_common_type(Oid common_type, List *exprs)
                               1608                 :                : {
                               1609                 :                :     ListCell   *lc;
                               1610                 :                : 
                               1611   [ +  -  +  +  :          46474 :     foreach(lc, exprs)
                                              +  + ]
                               1612                 :                :     {
                               1613                 :          37207 :         Node       *nexpr = (Node *) lfirst(lc);
                               1614                 :          37207 :         Oid         ntype = exprType(nexpr);
                               1615                 :                : 
                               1616         [ +  + ]:          37207 :         if (!can_coerce_type(1, &ntype, &common_type, COERCION_IMPLICIT))
                               1617                 :              3 :             return false;
                               1618                 :                :     }
                               1619                 :           9267 :     return true;
                               1620                 :                : }
                               1621                 :                : 
                               1622                 :                : /*
                               1623                 :                :  * verify_common_type_from_oids()
                               1624                 :                :  *      As above, but work from an array of type OIDs.
                               1625                 :                :  */
                               1626                 :                : static bool
                               1627                 :           3414 : verify_common_type_from_oids(Oid common_type, int nargs, const Oid *typeids)
                               1628                 :                : {
                               1629         [ +  + ]:           8772 :     for (int i = 0; i < nargs; i++)
                               1630                 :                :     {
                               1631         [ +  + ]:           5364 :         if (!can_coerce_type(1, &typeids[i], &common_type, COERCION_IMPLICIT))
                               1632                 :              6 :             return false;
                               1633                 :                :     }
                               1634                 :           3408 :     return true;
                               1635                 :                : }
                               1636                 :                : 
                               1637                 :                : /*
                               1638                 :                :  * select_common_typmod()
                               1639                 :                :  *      Determine the common typmod of a list of input expressions.
                               1640                 :                :  *
                               1641                 :                :  * common_type is the selected common type of the expressions, typically
                               1642                 :                :  * computed using select_common_type().
                               1643                 :                :  */
                               1644                 :                : int32
 1826 peter@eisentraut.org     1645                 :          37098 : select_common_typmod(ParseState *pstate, List *exprs, Oid common_type)
                               1646                 :                : {
                               1647                 :                :     ListCell   *lc;
                               1648                 :          37098 :     bool        first = true;
                               1649                 :          37098 :     int32       result = -1;
                               1650                 :                : 
                               1651   [ +  -  +  +  :         116078 :     foreach(lc, exprs)
                                              +  + ]
                               1652                 :                :     {
 1757 tgl@sss.pgh.pa.us        1653                 :          79091 :         Node       *expr = (Node *) lfirst(lc);
                               1654                 :                : 
                               1655                 :                :         /* Types must match */
 1826 peter@eisentraut.org     1656         [ +  + ]:          79091 :         if (exprType(expr) != common_type)
                               1657                 :            111 :             return -1;
                               1658         [ +  + ]:          79001 :         else if (first)
                               1659                 :                :         {
                               1660                 :          37053 :             result = exprTypmod(expr);
                               1661                 :          37053 :             first = false;
                               1662                 :                :         }
                               1663                 :                :         else
                               1664                 :                :         {
                               1665                 :                :             /* As soon as we see a non-matching typmod, fall back to -1 */
                               1666         [ +  + ]:          41948 :             if (result != exprTypmod(expr))
                               1667                 :             21 :                 return -1;
                               1668                 :                :         }
                               1669                 :                :     }
                               1670                 :                : 
                               1671                 :          36987 :     return result;
                               1672                 :                : }
                               1673                 :                : 
                               1674                 :                : /*
                               1675                 :                :  * check_generic_type_consistency()
                               1676                 :                :  *      Are the actual arguments potentially compatible with a
                               1677                 :                :  *      polymorphic function?
                               1678                 :                :  *
                               1679                 :                :  * The argument consistency rules are:
                               1680                 :                :  *
                               1681                 :                :  * 1) All arguments declared ANYELEMENT must have the same datatype.
                               1682                 :                :  * 2) All arguments declared ANYARRAY must have the same datatype,
                               1683                 :                :  *    which must be a varlena array type.
                               1684                 :                :  * 3) All arguments declared ANYRANGE must be the same range type.
                               1685                 :                :  *    Similarly, all arguments declared ANYMULTIRANGE must be the same
                               1686                 :                :  *    multirange type; and if both of these appear, the ANYRANGE type
                               1687                 :                :  *    must be the element type of the ANYMULTIRANGE type.
                               1688                 :                :  * 4) If there are arguments of more than one of these polymorphic types,
                               1689                 :                :  *    the array element type and/or range subtype must be the same as each
                               1690                 :                :  *    other and the same as the ANYELEMENT type.
                               1691                 :                :  * 5) ANYENUM is treated the same as ANYELEMENT except that if it is used
                               1692                 :                :  *    (alone or in combination with plain ANYELEMENT), we add the extra
                               1693                 :                :  *    condition that the ANYELEMENT type must be an enum.
                               1694                 :                :  * 6) ANYNONARRAY is treated the same as ANYELEMENT except that if it is used,
                               1695                 :                :  *    we add the extra condition that the ANYELEMENT type must not be an array.
                               1696                 :                :  *    (This is a no-op if used in combination with ANYARRAY or ANYENUM, but
                               1697                 :                :  *    is an extra restriction if not.)
                               1698                 :                :  * 7) All arguments declared ANYCOMPATIBLE must be implicitly castable
                               1699                 :                :  *    to a common supertype (chosen as per select_common_type's rules).
                               1700                 :                :  *    ANYCOMPATIBLENONARRAY works like ANYCOMPATIBLE but also requires the
                               1701                 :                :  *    common supertype to not be an array.  If there are ANYCOMPATIBLEARRAY
                               1702                 :                :  *    or ANYCOMPATIBLERANGE or ANYCOMPATIBLEMULTIRANGE arguments, their element
                               1703                 :                :  *    types or subtypes are included while making the choice of common supertype.
                               1704                 :                :  * 8) The resolved type of ANYCOMPATIBLEARRAY arguments will be the array
                               1705                 :                :  *    type over the common supertype (which might not be the same array type
                               1706                 :                :  *    as any of the original arrays).
                               1707                 :                :  * 9) All ANYCOMPATIBLERANGE arguments must be the exact same range type
                               1708                 :                :  *    (after domain flattening), since we have no preference rule that would
                               1709                 :                :  *    let us choose one over another.  Furthermore, that range's subtype
                               1710                 :                :  *    must exactly match the common supertype chosen by rule 7.
                               1711                 :                :  * 10) All ANYCOMPATIBLEMULTIRANGE arguments must be the exact same multirange
                               1712                 :                :  *    type (after domain flattening), since we have no preference rule that
                               1713                 :                :  *    would let us choose one over another.  Furthermore, if ANYCOMPATIBLERANGE
                               1714                 :                :  *    also appears, that range type must be the multirange's element type;
                               1715                 :                :  *    otherwise, the multirange's range's subtype must exactly match the
                               1716                 :                :  *    common supertype chosen by rule 7.
                               1717                 :                :  *
                               1718                 :                :  * Domains over arrays match ANYARRAY, and are immediately flattened to their
                               1719                 :                :  * base type.  (Thus, for example, we will consider it a match if one ANYARRAY
                               1720                 :                :  * argument is a domain over int4[] while another one is just int4[].)  Also
                               1721                 :                :  * notice that such a domain does *not* match ANYNONARRAY.  The same goes
                               1722                 :                :  * for ANYCOMPATIBLEARRAY and ANYCOMPATIBLENONARRAY.
                               1723                 :                :  *
                               1724                 :                :  * Similarly, domains over ranges match ANYRANGE or ANYCOMPATIBLERANGE,
                               1725                 :                :  * and are immediately flattened to their base type.  Likewise, domains
                               1726                 :                :  * over multiranges match ANYMULTIRANGE or ANYCOMPATIBLEMULTIRANGE and are
                               1727                 :                :  * immediately flattened to their base type.
                               1728                 :                :  *
                               1729                 :                :  * Note that domains aren't currently considered to match ANYENUM,
                               1730                 :                :  * even if their base type would match.
                               1731                 :                :  *
                               1732                 :                :  * If we have UNKNOWN input (ie, an untyped literal) for any polymorphic
                               1733                 :                :  * argument, assume it is okay.
                               1734                 :                :  *
                               1735                 :                :  * We do not ereport here, but just return false if a rule is violated.
                               1736                 :                :  */
                               1737                 :                : bool
 2561 peter_e@gmx.net          1738                 :          66236 : check_generic_type_consistency(const Oid *actual_arg_types,
                               1739                 :                :                                const Oid *declared_arg_types,
                               1740                 :                :                                int nargs)
                               1741                 :                : {
 8238 tgl@sss.pgh.pa.us        1742                 :          66236 :     Oid         elem_typeid = InvalidOid;
                               1743                 :          66236 :     Oid         array_typeid = InvalidOid;
 5107 heikki.linnakangas@i     1744                 :          66236 :     Oid         range_typeid = InvalidOid;
 1772 akorotkov@postgresql     1745                 :          66236 :     Oid         multirange_typeid = InvalidOid;
 2048 tgl@sss.pgh.pa.us        1746                 :          66236 :     Oid         anycompatible_range_typeid = InvalidOid;
                               1747                 :          66236 :     Oid         anycompatible_range_typelem = InvalidOid;
 1772 akorotkov@postgresql     1748                 :          66236 :     Oid         anycompatible_multirange_typeid = InvalidOid;
                               1749                 :          66236 :     Oid         anycompatible_multirange_typelem = InvalidOid;
                               1750                 :          66236 :     Oid         range_typelem = InvalidOid;
 6718 tgl@sss.pgh.pa.us        1751                 :          66236 :     bool        have_anynonarray = false;
 6783                          1752                 :          66236 :     bool        have_anyenum = false;
 2048                          1753                 :          66236 :     bool        have_anycompatible_nonarray = false;
                               1754                 :          66236 :     int         n_anycompatible_args = 0;
                               1755                 :                :     Oid         anycompatible_actual_types[FUNC_MAX_ARGS];
                               1756                 :                : 
                               1757                 :                :     /*
                               1758                 :                :      * Loop through the arguments to see if we have any that are polymorphic.
                               1759                 :                :      * If so, require the actual types to be consistent.
                               1760                 :                :      */
                               1761         [ -  + ]:          66236 :     Assert(nargs <= FUNC_MAX_ARGS);
 2050                          1762         [ +  + ]:         153868 :     for (int j = 0; j < nargs; j++)
                               1763                 :                :     {
 6718                          1764                 :         107136 :         Oid         decl_type = declared_arg_types[j];
 8120 bruce@momjian.us         1765                 :         107136 :         Oid         actual_type = actual_arg_types[j];
                               1766                 :                : 
 6718 tgl@sss.pgh.pa.us        1767   [ +  +  +  + ]:         107136 :         if (decl_type == ANYELEMENTOID ||
                               1768         [ +  + ]:          96279 :             decl_type == ANYNONARRAYOID ||
                               1769                 :                :             decl_type == ANYENUMOID)
                               1770                 :                :         {
                               1771         [ +  + ]:          23310 :             if (decl_type == ANYNONARRAYOID)
                               1772                 :           7734 :                 have_anynonarray = true;
                               1773         [ +  + ]:          15576 :             else if (decl_type == ANYENUMOID)
 6783                          1774                 :          12453 :                 have_anyenum = true;
 8238                          1775         [ +  + ]:          23310 :             if (actual_type == UNKNOWNOID)
                               1776                 :           1356 :                 continue;
                               1777   [ +  +  +  + ]:          21954 :             if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
                               1778                 :           4427 :                 return false;
                               1779                 :          17527 :             elem_typeid = actual_type;
                               1780                 :                :         }
 6718                          1781         [ +  + ]:          83826 :         else if (decl_type == ANYARRAYOID)
                               1782                 :                :         {
 8238                          1783         [ +  + ]:          31778 :             if (actual_type == UNKNOWNOID)
                               1784                 :           1560 :                 continue;
 3050                          1785                 :          30218 :             actual_type = getBaseType(actual_type); /* flatten domains */
 8238                          1786   [ +  +  +  + ]:          30218 :             if (OidIsValid(array_typeid) && actual_type != array_typeid)
                               1787                 :           4395 :                 return false;
                               1788                 :          25823 :             array_typeid = actual_type;
                               1789                 :                :         }
 5107 heikki.linnakangas@i     1790         [ +  + ]:          52048 :         else if (decl_type == ANYRANGEOID)
                               1791                 :                :         {
                               1792         [ +  + ]:          17575 :             if (actual_type == UNKNOWNOID)
                               1793                 :           1129 :                 continue;
 3050 tgl@sss.pgh.pa.us        1794                 :          16446 :             actual_type = getBaseType(actual_type); /* flatten domains */
 5107 heikki.linnakangas@i     1795   [ +  +  +  + ]:          16446 :             if (OidIsValid(range_typeid) && actual_type != range_typeid)
                               1796                 :           4832 :                 return false;
                               1797                 :          11614 :             range_typeid = actual_type;
                               1798                 :                :         }
 1772 akorotkov@postgresql     1799         [ +  + ]:          34473 :         else if (decl_type == ANYMULTIRANGEOID)
                               1800                 :                :         {
                               1801         [ +  + ]:          19899 :             if (actual_type == UNKNOWNOID)
                               1802                 :           1138 :                 continue;
                               1803                 :          18761 :             actual_type = getBaseType(actual_type); /* flatten domains */
                               1804   [ +  +  +  + ]:          18761 :             if (OidIsValid(multirange_typeid) && actual_type != multirange_typeid)
                               1805                 :           4829 :                 return false;
                               1806                 :          13932 :             multirange_typeid = actual_type;
                               1807                 :                :         }
 2048 tgl@sss.pgh.pa.us        1808   [ +  +  +  + ]:          14574 :         else if (decl_type == ANYCOMPATIBLEOID ||
                               1809                 :                :                  decl_type == ANYCOMPATIBLENONARRAYOID)
                               1810                 :                :         {
                               1811         [ +  + ]:           2088 :             if (decl_type == ANYCOMPATIBLENONARRAYOID)
                               1812                 :             18 :                 have_anycompatible_nonarray = true;
                               1813         [ +  + ]:           2088 :             if (actual_type == UNKNOWNOID)
                               1814                 :            811 :                 continue;
                               1815                 :                :             /* collect the actual types of non-unknown COMPATIBLE args */
                               1816                 :           1277 :             anycompatible_actual_types[n_anycompatible_args++] = actual_type;
                               1817                 :                :         }
                               1818         [ +  + ]:          12486 :         else if (decl_type == ANYCOMPATIBLEARRAYOID)
                               1819                 :                :         {
                               1820                 :                :             Oid         elem_type;
                               1821                 :                : 
                               1822         [ +  + ]:           3068 :             if (actual_type == UNKNOWNOID)
                               1823                 :            531 :                 continue;
                               1824                 :           2537 :             actual_type = getBaseType(actual_type); /* flatten domains */
                               1825                 :           2537 :             elem_type = get_element_type(actual_type);
                               1826         [ +  + ]:           2537 :             if (!OidIsValid(elem_type))
                               1827                 :           1009 :                 return false;   /* not an array */
                               1828                 :                :             /* collect the element type for common-supertype choice */
                               1829                 :           1528 :             anycompatible_actual_types[n_anycompatible_args++] = elem_type;
                               1830                 :                :         }
                               1831         [ +  + ]:           9418 :         else if (decl_type == ANYCOMPATIBLERANGEOID)
                               1832                 :                :         {
                               1833         [ +  + ]:             93 :             if (actual_type == UNKNOWNOID)
                               1834                 :              6 :                 continue;
                               1835                 :             87 :             actual_type = getBaseType(actual_type); /* flatten domains */
                               1836         [ +  + ]:             87 :             if (OidIsValid(anycompatible_range_typeid))
                               1837                 :                :             {
                               1838                 :                :                 /* All ANYCOMPATIBLERANGE arguments must be the same type */
                               1839         [ +  + ]:              6 :                 if (anycompatible_range_typeid != actual_type)
                               1840                 :              3 :                     return false;
                               1841                 :                :             }
                               1842                 :                :             else
                               1843                 :                :             {
                               1844                 :             81 :                 anycompatible_range_typeid = actual_type;
                               1845                 :             81 :                 anycompatible_range_typelem = get_range_subtype(actual_type);
                               1846         [ +  + ]:             81 :                 if (!OidIsValid(anycompatible_range_typelem))
                               1847                 :              3 :                     return false;   /* not a range type */
                               1848                 :                :                 /* collect the subtype for common-supertype choice */
                               1849                 :             78 :                 anycompatible_actual_types[n_anycompatible_args++] = anycompatible_range_typelem;
                               1850                 :                :             }
                               1851                 :                :         }
 1772 akorotkov@postgresql     1852         [ +  + ]:           9325 :         else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
                               1853                 :                :         {
                               1854         [ +  + ]:             69 :             if (actual_type == UNKNOWNOID)
                               1855                 :              9 :                 continue;
                               1856                 :             60 :             actual_type = getBaseType(actual_type); /* flatten domains */
                               1857         [ +  + ]:             60 :             if (OidIsValid(anycompatible_multirange_typeid))
                               1858                 :                :             {
                               1859                 :                :                 /* All ANYCOMPATIBLEMULTIRANGE arguments must be the same type */
                               1860         [ +  + ]:              6 :                 if (anycompatible_multirange_typeid != actual_type)
                               1861                 :              3 :                     return false;
                               1862                 :                :             }
                               1863                 :                :             else
                               1864                 :                :             {
                               1865                 :             54 :                 anycompatible_multirange_typeid = actual_type;
                               1866                 :             54 :                 anycompatible_multirange_typelem = get_multirange_range(actual_type);
                               1867         [ +  + ]:             54 :                 if (!OidIsValid(anycompatible_multirange_typelem))
                               1868                 :              3 :                     return false;   /* not a multirange type */
                               1869                 :                :                 /* we'll consider the subtype below */
                               1870                 :                :             }
                               1871                 :                :         }
                               1872                 :                :     }
                               1873                 :                : 
                               1874                 :                :     /* Get the element type based on the array type, if we have one */
 8238 tgl@sss.pgh.pa.us        1875         [ +  + ]:          46732 :     if (OidIsValid(array_typeid))
                               1876                 :                :     {
 8070                          1877         [ +  - ]:          20252 :         if (array_typeid == ANYARRAYOID)
                               1878                 :                :         {
                               1879                 :                :             /*
                               1880                 :                :              * Special case for matching ANYARRAY input to an ANYARRAY
                               1881                 :                :              * argument: allow it for now.  enforce_generic_type_consistency()
                               1882                 :                :              * might complain later, depending on the presence of other
                               1883                 :                :              * polymorphic arguments or results, but it will deliver a less
                               1884                 :                :              * surprising error message than "function does not exist".
                               1885                 :                :              *
                               1886                 :                :              * (If you think to change this, note that can_coerce_type will
                               1887                 :                :              * consider such a situation as a match, so that we might not even
                               1888                 :                :              * get here.)
                               1889                 :                :              */
                               1890                 :                :         }
                               1891                 :                :         else
                               1892                 :                :         {
                               1893                 :                :             Oid         array_typelem;
                               1894                 :                : 
 2050                          1895                 :          20252 :             array_typelem = get_element_type(array_typeid);
                               1896         [ +  + ]:          20252 :             if (!OidIsValid(array_typelem))
                               1897                 :           7659 :                 return false;   /* should be an array, but isn't */
                               1898                 :                : 
                               1899         [ +  + ]:          12593 :             if (!OidIsValid(elem_typeid))
                               1900                 :                :             {
                               1901                 :                :                 /*
                               1902                 :                :                  * if we don't have an element type yet, use the one we just
                               1903                 :                :                  * got
                               1904                 :                :                  */
                               1905                 :          12527 :                 elem_typeid = array_typelem;
                               1906                 :                :             }
                               1907         [ +  + ]:             66 :             else if (array_typelem != elem_typeid)
                               1908                 :                :             {
                               1909                 :                :                 /* otherwise, they better match */
                               1910                 :             25 :                 return false;
                               1911                 :                :             }
                               1912                 :                :         }
                               1913                 :                :     }
                               1914                 :                : 
                               1915                 :                :     /* Deduce range type from multirange type, or check that they agree */
 1772 akorotkov@postgresql     1916         [ +  + ]:          39048 :     if (OidIsValid(multirange_typeid))
                               1917                 :                :     {
                               1918                 :                :         Oid         multirange_typelem;
                               1919                 :                : 
                               1920                 :           7776 :         multirange_typelem = get_multirange_range(multirange_typeid);
                               1921         [ +  + ]:           7776 :         if (!OidIsValid(multirange_typelem))
                               1922                 :           6744 :             return false;       /* should be a multirange, but isn't */
                               1923                 :                : 
                               1924         [ +  + ]:           1032 :         if (!OidIsValid(range_typeid))
                               1925                 :                :         {
                               1926                 :                :             /* If we don't have a range type yet, use the one we just got */
                               1927                 :            766 :             range_typeid = multirange_typelem;
                               1928                 :            766 :             range_typelem = get_range_subtype(multirange_typelem);
                               1929         [ -  + ]:            766 :             if (!OidIsValid(range_typelem))
 1772 akorotkov@postgresql     1930                 :UBC           0 :                 return false;   /* should be a range, but isn't */
                               1931                 :                :         }
 1772 akorotkov@postgresql     1932         [ +  + ]:CBC         266 :         else if (multirange_typelem != range_typeid)
                               1933                 :                :         {
                               1934                 :                :             /* otherwise, they better match */
                               1935                 :            152 :             return false;
                               1936                 :                :         }
                               1937                 :                :     }
                               1938                 :                : 
                               1939                 :                :     /* Get the element type based on the range type, if we have one */
 1553 tgl@sss.pgh.pa.us        1940         [ +  + ]:          32152 :     if (OidIsValid(range_typeid))
                               1941                 :                :     {
                               1942                 :           5172 :         range_typelem = get_range_subtype(range_typeid);
                               1943         [ +  + ]:           5172 :         if (!OidIsValid(range_typelem))
                               1944                 :           2491 :             return false;       /* should be a range, but isn't */
                               1945                 :                : 
 1772 akorotkov@postgresql     1946         [ +  + ]:           2681 :         if (!OidIsValid(elem_typeid))
                               1947                 :                :         {
                               1948                 :                :             /*
                               1949                 :                :              * If we don't have an element type yet, use the one we just got
                               1950                 :                :              */
                               1951                 :           2476 :             elem_typeid = range_typelem;
                               1952                 :                :         }
                               1953         [ +  + ]:            205 :         else if (range_typelem != elem_typeid)
                               1954                 :                :         {
                               1955                 :                :             /* otherwise, they better match */
                               1956                 :             92 :             return false;
                               1957                 :                :         }
                               1958                 :                :     }
                               1959                 :                : 
 5090 tgl@sss.pgh.pa.us        1960         [ +  + ]:          29569 :     if (have_anynonarray)
                               1961                 :                :     {
                               1962                 :                :         /* require the element type to not be an array or domain over array */
                               1963         [ +  + ]:           7558 :         if (type_is_array_domain(elem_typeid))
                               1964                 :            141 :             return false;
                               1965                 :                :     }
                               1966                 :                : 
                               1967         [ +  + ]:          29428 :     if (have_anyenum)
                               1968                 :                :     {
                               1969                 :                :         /* require the element type to be an enum */
                               1970         [ +  + ]:           1906 :         if (!type_is_enum(elem_typeid))
                               1971                 :           1708 :             return false;
                               1972                 :                :     }
                               1973                 :                : 
                               1974                 :                :     /* Deduce range type from multirange type, or check that they agree */
 1553                          1975         [ +  + ]:          27720 :     if (OidIsValid(anycompatible_multirange_typeid))
                               1976                 :                :     {
                               1977         [ +  + ]:             48 :         if (OidIsValid(anycompatible_range_typeid))
                               1978                 :                :         {
                               1979         [ +  + ]:              6 :             if (anycompatible_multirange_typelem !=
                               1980                 :                :                 anycompatible_range_typeid)
                               1981                 :              3 :                 return false;
                               1982                 :                :         }
                               1983                 :                :         else
                               1984                 :                :         {
                               1985                 :             42 :             anycompatible_range_typeid = anycompatible_multirange_typelem;
                               1986                 :             42 :             anycompatible_range_typelem = get_range_subtype(anycompatible_range_typeid);
                               1987         [ -  + ]:             42 :             if (!OidIsValid(anycompatible_range_typelem))
 1553 tgl@sss.pgh.pa.us        1988                 :UBC           0 :                 return false;   /* not a range type */
                               1989                 :                :             /* collect the subtype for common-supertype choice */
 1553 tgl@sss.pgh.pa.us        1990                 :CBC          42 :             anycompatible_actual_types[n_anycompatible_args++] =
                               1991                 :                :                 anycompatible_range_typelem;
                               1992                 :                :         }
                               1993                 :                :     }
                               1994                 :                : 
                               1995                 :                :     /* Check matching of ANYCOMPATIBLE-family arguments, if any */
 2048                          1996         [ +  + ]:          27717 :     if (n_anycompatible_args > 0)
                               1997                 :                :     {
                               1998                 :                :         Oid         anycompatible_typeid;
                               1999                 :                : 
                               2000                 :                :         anycompatible_typeid =
                               2001                 :           1773 :             select_common_type_from_oids(n_anycompatible_args,
                               2002                 :                :                                          anycompatible_actual_types,
                               2003                 :                :                                          true);
                               2004                 :                : 
                               2005         [ +  + ]:           1773 :         if (!OidIsValid(anycompatible_typeid))
 1367                          2006                 :            324 :             return false;       /* there's definitely no common supertype */
                               2007                 :                : 
                               2008                 :                :         /* We have to verify that the selected type actually works */
                               2009         [ +  + ]:           1449 :         if (!verify_common_type_from_oids(anycompatible_typeid,
                               2010                 :                :                                           n_anycompatible_args,
                               2011                 :                :                                           anycompatible_actual_types))
                               2012                 :              6 :             return false;
                               2013                 :                : 
 2048                          2014         [ +  + ]:           1443 :         if (have_anycompatible_nonarray)
                               2015                 :                :         {
                               2016                 :                :             /*
                               2017                 :                :              * require the anycompatible type to not be an array or domain
                               2018                 :                :              * over array
                               2019                 :                :              */
                               2020         [ +  + ]:              9 :             if (type_is_array_domain(anycompatible_typeid))
                               2021                 :              3 :                 return false;
                               2022                 :                :         }
                               2023                 :                : 
                               2024                 :                :         /*
                               2025                 :                :          * The anycompatible type must exactly match the range element type,
                               2026                 :                :          * if we were able to identify one. This checks compatibility for
                               2027                 :                :          * anycompatiblemultirange too since that also sets
                               2028                 :                :          * anycompatible_range_typelem above.
                               2029                 :                :          */
                               2030   [ +  +  +  + ]:           1440 :         if (OidIsValid(anycompatible_range_typelem) &&
                               2031                 :                :             anycompatible_range_typelem != anycompatible_typeid)
                               2032                 :             21 :             return false;
                               2033                 :                :     }
                               2034                 :                : 
                               2035                 :                :     /* Looks valid */
 8238                          2036                 :          27363 :     return true;
                               2037                 :                : }
                               2038                 :                : 
                               2039                 :                : /*
                               2040                 :                :  * enforce_generic_type_consistency()
                               2041                 :                :  *      Make sure a polymorphic function is legally callable, and
                               2042                 :                :  *      deduce actual argument and result types.
                               2043                 :                :  *
                               2044                 :                :  * If any polymorphic pseudotype is used in a function's arguments or
                               2045                 :                :  * return type, we make sure the actual data types are consistent with
                               2046                 :                :  * each other.  The argument consistency rules are shown above for
                               2047                 :                :  * check_generic_type_consistency().
                               2048                 :                :  *
                               2049                 :                :  * If we have UNKNOWN input (ie, an untyped literal) for any polymorphic
                               2050                 :                :  * argument, we attempt to deduce the actual type it should have.  If
                               2051                 :                :  * successful, we alter that position of declared_arg_types[] so that
                               2052                 :                :  * make_fn_arguments will coerce the literal to the right thing.
                               2053                 :                :  *
                               2054                 :                :  * If we have polymorphic arguments of the ANYCOMPATIBLE family,
                               2055                 :                :  * we similarly alter declared_arg_types[] entries to show the resolved
                               2056                 :                :  * common supertype, so that make_fn_arguments will coerce the actual
                               2057                 :                :  * arguments to the proper type.
                               2058                 :                :  *
                               2059                 :                :  * Rules are applied to the function's return type (possibly altering it)
                               2060                 :                :  * if it is declared as a polymorphic type and there is at least one
                               2061                 :                :  * polymorphic argument type:
                               2062                 :                :  *
                               2063                 :                :  * 1) If return type is ANYELEMENT, and any argument is ANYELEMENT, use the
                               2064                 :                :  *    argument's actual type as the function's return type.
                               2065                 :                :  * 2) If return type is ANYARRAY, and any argument is ANYARRAY, use the
                               2066                 :                :  *    argument's actual type as the function's return type.
                               2067                 :                :  * 3) Similarly, if return type is ANYRANGE or ANYMULTIRANGE, and any
                               2068                 :                :  *    argument is ANYRANGE or ANYMULTIRANGE, use that argument's actual type
                               2069                 :                :  *    (or the corresponding range or multirange type) as the function's return
                               2070                 :                :  *    type.
                               2071                 :                :  * 4) Otherwise, if return type is ANYELEMENT or ANYARRAY, and there is
                               2072                 :                :  *    at least one ANYELEMENT, ANYARRAY, ANYRANGE, or ANYMULTIRANGE input,
                               2073                 :                :  *    deduce the return type from those inputs, or throw error if we can't.
                               2074                 :                :  * 5) Otherwise, if return type is ANYRANGE or ANYMULTIRANGE, throw error.
                               2075                 :                :  *    (We have no way to select a specific range type if the arguments don't
                               2076                 :                :  *    include ANYRANGE or ANYMULTIRANGE.)
                               2077                 :                :  * 6) ANYENUM is treated the same as ANYELEMENT except that if it is used
                               2078                 :                :  *    (alone or in combination with plain ANYELEMENT), we add the extra
                               2079                 :                :  *    condition that the ANYELEMENT type must be an enum.
                               2080                 :                :  * 7) ANYNONARRAY is treated the same as ANYELEMENT except that if it is used,
                               2081                 :                :  *    we add the extra condition that the ANYELEMENT type must not be an array.
                               2082                 :                :  *    (This is a no-op if used in combination with ANYARRAY or ANYENUM, but
                               2083                 :                :  *    is an extra restriction if not.)
                               2084                 :                :  * 8) ANYCOMPATIBLE, ANYCOMPATIBLEARRAY, and ANYCOMPATIBLENONARRAY are handled
                               2085                 :                :  *    by resolving the common supertype of those arguments (or their element
                               2086                 :                :  *    types, for array inputs), and then coercing all those arguments to the
                               2087                 :                :  *    common supertype, or the array type over the common supertype for
                               2088                 :                :  *    ANYCOMPATIBLEARRAY.
                               2089                 :                :  * 9) For ANYCOMPATIBLERANGE and ANYCOMPATIBLEMULTIRANGE, there must be at
                               2090                 :                :  *    least one non-UNKNOWN input matching those arguments, and all such
                               2091                 :                :  *    inputs must be the same range type (or its multirange type, as
                               2092                 :                :  *    appropriate), since we cannot deduce a range type from non-range types.
                               2093                 :                :  *    Furthermore, the range type's subtype is included while choosing the
                               2094                 :                :  *    common supertype for ANYCOMPATIBLE et al, and it must exactly match
                               2095                 :                :  *    that common supertype.
                               2096                 :                :  *
                               2097                 :                :  * Domains over arrays or ranges match ANYARRAY or ANYRANGE arguments,
                               2098                 :                :  * respectively, and are immediately flattened to their base type.  (In
                               2099                 :                :  * particular, if the return type is also ANYARRAY or ANYRANGE, we'll set
                               2100                 :                :  * it to the base type not the domain type.)  The same is true for
                               2101                 :                :  * ANYMULTIRANGE, ANYCOMPATIBLEARRAY, ANYCOMPATIBLERANGE, and
                               2102                 :                :  * ANYCOMPATIBLEMULTIRANGE.
                               2103                 :                :  *
                               2104                 :                :  * When allow_poly is false, we are not expecting any of the actual_arg_types
                               2105                 :                :  * to be polymorphic, and we should not return a polymorphic result type
                               2106                 :                :  * either.  When allow_poly is true, it is okay to have polymorphic "actual"
                               2107                 :                :  * arg types, and we can return a matching polymorphic type as the result.
                               2108                 :                :  * (This case is currently used only to check compatibility of an aggregate's
                               2109                 :                :  * declaration with the underlying transfn.)
                               2110                 :                :  *
                               2111                 :                :  * A special case is that we could see ANYARRAY as an actual_arg_type even
                               2112                 :                :  * when allow_poly is false (this is possible only because pg_statistic has
                               2113                 :                :  * columns shown as anyarray in the catalogs).  We allow this to match a
                               2114                 :                :  * declared ANYARRAY argument, but only if there is no other polymorphic
                               2115                 :                :  * argument that we would need to match it with, and no need to determine
                               2116                 :                :  * the element type to infer the result type.  Note this means that functions
                               2117                 :                :  * taking ANYARRAY had better behave sanely if applied to the pg_statistic
                               2118                 :                :  * columns; they can't just assume that successive inputs are of the same
                               2119                 :                :  * actual element type.  There is no similar logic for ANYCOMPATIBLEARRAY;
                               2120                 :                :  * there isn't a need for it since there are no catalog columns of that type,
                               2121                 :                :  * so we won't see it as input.  We could consider matching an actual ANYARRAY
                               2122                 :                :  * input to an ANYCOMPATIBLEARRAY argument, but at present that seems useless
                               2123                 :                :  * as well, since there's no value in using ANYCOMPATIBLEARRAY unless there's
                               2124                 :                :  * at least one other ANYCOMPATIBLE-family argument or result.
                               2125                 :                :  *
                               2126                 :                :  * Also, if there are no arguments declared to be of polymorphic types,
                               2127                 :                :  * we'll return the rettype unmodified even if it's polymorphic.  This should
                               2128                 :                :  * never occur for user-declared functions, because CREATE FUNCTION prevents
                               2129                 :                :  * it.  But it does happen for some built-in functions, such as array_in().
                               2130                 :                :  */
                               2131                 :                : Oid
 2561 peter_e@gmx.net          2132                 :         515802 : enforce_generic_type_consistency(const Oid *actual_arg_types,
                               2133                 :                :                                  Oid *declared_arg_types,
                               2134                 :                :                                  int nargs,
                               2135                 :                :                                  Oid rettype,
                               2136                 :                :                                  bool allow_poly)
                               2137                 :                : {
 2048 tgl@sss.pgh.pa.us        2138                 :         515802 :     bool        have_poly_anycompatible = false;
 2050                          2139                 :         515802 :     bool        have_poly_unknowns = false;
 8238                          2140                 :         515802 :     Oid         elem_typeid = InvalidOid;
                               2141                 :         515802 :     Oid         array_typeid = InvalidOid;
 5107 heikki.linnakangas@i     2142                 :         515802 :     Oid         range_typeid = InvalidOid;
 1772 akorotkov@postgresql     2143                 :         515802 :     Oid         multirange_typeid = InvalidOid;
 2048 tgl@sss.pgh.pa.us        2144                 :         515802 :     Oid         anycompatible_typeid = InvalidOid;
                               2145                 :         515802 :     Oid         anycompatible_array_typeid = InvalidOid;
                               2146                 :         515802 :     Oid         anycompatible_range_typeid = InvalidOid;
                               2147                 :         515802 :     Oid         anycompatible_range_typelem = InvalidOid;
 1772 akorotkov@postgresql     2148                 :         515802 :     Oid         anycompatible_multirange_typeid = InvalidOid;
                               2149                 :         515802 :     Oid         anycompatible_multirange_typelem = InvalidOid;
 6718 tgl@sss.pgh.pa.us        2150                 :         515802 :     bool        have_anynonarray = (rettype == ANYNONARRAYOID);
 6783                          2151                 :         515802 :     bool        have_anyenum = (rettype == ANYENUMOID);
 1553                          2152                 :         515802 :     bool        have_anymultirange = (rettype == ANYMULTIRANGEOID);
 2048                          2153                 :         515802 :     bool        have_anycompatible_nonarray = (rettype == ANYCOMPATIBLENONARRAYOID);
                               2154                 :         515802 :     bool        have_anycompatible_array = (rettype == ANYCOMPATIBLEARRAYOID);
                               2155                 :         515802 :     bool        have_anycompatible_range = (rettype == ANYCOMPATIBLERANGEOID);
 1553                          2156                 :         515802 :     bool        have_anycompatible_multirange = (rettype == ANYCOMPATIBLEMULTIRANGEOID);
 2048                          2157                 :         515802 :     int         n_poly_args = 0;    /* this counts all family-1 arguments */
                               2158                 :         515802 :     int         n_anycompatible_args = 0;   /* this counts only non-unknowns */
                               2159                 :                :     Oid         anycompatible_actual_types[FUNC_MAX_ARGS];
                               2160                 :                : 
                               2161                 :                :     /*
                               2162                 :                :      * Loop through the arguments to see if we have any that are polymorphic.
                               2163                 :                :      * If so, require the actual types to be consistent.
                               2164                 :                :      */
                               2165         [ -  + ]:         515802 :     Assert(nargs <= FUNC_MAX_ARGS);
 2050                          2166         [ +  + ]:        1521170 :     for (int j = 0; j < nargs; j++)
                               2167                 :                :     {
 6718                          2168                 :        1005368 :         Oid         decl_type = declared_arg_types[j];
 8120 bruce@momjian.us         2169                 :        1005368 :         Oid         actual_type = actual_arg_types[j];
                               2170                 :                : 
 6718 tgl@sss.pgh.pa.us        2171   [ +  +  +  + ]:        1005368 :         if (decl_type == ANYELEMENTOID ||
                               2172         [ +  + ]:         994739 :             decl_type == ANYNONARRAYOID ||
                               2173                 :                :             decl_type == ANYENUMOID)
                               2174                 :                :         {
 2050                          2175                 :          11048 :             n_poly_args++;
 6718                          2176         [ +  + ]:          11048 :             if (decl_type == ANYNONARRAYOID)
                               2177                 :           7354 :                 have_anynonarray = true;
                               2178         [ +  + ]:           3694 :             else if (decl_type == ANYENUMOID)
 6783                          2179                 :            419 :                 have_anyenum = true;
 8238                          2180         [ +  + ]:          11048 :             if (actual_type == UNKNOWNOID)
                               2181                 :                :             {
 2050                          2182                 :            378 :                 have_poly_unknowns = true;
 8238                          2183                 :            378 :                 continue;
                               2184                 :                :             }
 6499                          2185   [ +  +  +  + ]:          10670 :             if (allow_poly && decl_type == actual_type)
                               2186                 :             88 :                 continue;       /* no new information here */
 8238                          2187   [ +  +  -  + ]:          10582 :             if (OidIsValid(elem_typeid) && actual_type != elem_typeid)
 8136 tgl@sss.pgh.pa.us        2188         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2189                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2190                 :                :                          errmsg("arguments declared \"%s\" are not all alike", "anyelement"),
                               2191                 :                :                          errdetail("%s versus %s",
                               2192                 :                :                                    format_type_be(elem_typeid),
                               2193                 :                :                                    format_type_be(actual_type))));
 8238 tgl@sss.pgh.pa.us        2194                 :CBC       10582 :             elem_typeid = actual_type;
                               2195                 :                :         }
 6718                          2196         [ +  + ]:         994320 :         else if (decl_type == ANYARRAYOID)
                               2197                 :                :         {
 2050                          2198                 :          17651 :             n_poly_args++;
 8238                          2199         [ +  + ]:          17651 :             if (actual_type == UNKNOWNOID)
                               2200                 :                :             {
 2050                          2201                 :           2252 :                 have_poly_unknowns = true;
 8238                          2202                 :           2252 :                 continue;
                               2203                 :                :             }
 6499                          2204   [ +  +  +  + ]:          15399 :             if (allow_poly && decl_type == actual_type)
                               2205                 :             49 :                 continue;       /* no new information here */
 3050                          2206                 :          15350 :             actual_type = getBaseType(actual_type); /* flatten domains */
 8238                          2207   [ +  +  -  + ]:          15350 :             if (OidIsValid(array_typeid) && actual_type != array_typeid)
 8136 tgl@sss.pgh.pa.us        2208         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2209                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2210                 :                :                          errmsg("arguments declared \"%s\" are not all alike", "anyarray"),
                               2211                 :                :                          errdetail("%s versus %s",
                               2212                 :                :                                    format_type_be(array_typeid),
                               2213                 :                :                                    format_type_be(actual_type))));
 8238 tgl@sss.pgh.pa.us        2214                 :CBC       15350 :             array_typeid = actual_type;
                               2215                 :                :         }
 5107 heikki.linnakangas@i     2216         [ +  + ]:         976669 :         else if (decl_type == ANYRANGEOID)
                               2217                 :                :         {
 2050 tgl@sss.pgh.pa.us        2218                 :           4038 :             n_poly_args++;
 5107 heikki.linnakangas@i     2219         [ +  + ]:           4038 :             if (actual_type == UNKNOWNOID)
                               2220                 :                :             {
 2050 tgl@sss.pgh.pa.us        2221                 :            420 :                 have_poly_unknowns = true;
 5107 heikki.linnakangas@i     2222                 :            420 :                 continue;
                               2223                 :                :             }
                               2224   [ -  +  -  - ]:           3618 :             if (allow_poly && decl_type == actual_type)
 5107 heikki.linnakangas@i     2225                 :UBC           0 :                 continue;       /* no new information here */
 3050 tgl@sss.pgh.pa.us        2226                 :CBC        3618 :             actual_type = getBaseType(actual_type); /* flatten domains */
 5107 heikki.linnakangas@i     2227   [ +  +  -  + ]:           3618 :             if (OidIsValid(range_typeid) && actual_type != range_typeid)
 5107 heikki.linnakangas@i     2228         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2229                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2230                 :                :                          errmsg("arguments declared \"%s\" are not all alike", "anyrange"),
                               2231                 :                :                          errdetail("%s versus %s",
                               2232                 :                :                                    format_type_be(range_typeid),
                               2233                 :                :                                    format_type_be(actual_type))));
 5107 heikki.linnakangas@i     2234                 :CBC        3618 :             range_typeid = actual_type;
                               2235                 :                :         }
 1772 akorotkov@postgresql     2236         [ +  + ]:         972631 :         else if (decl_type == ANYMULTIRANGEOID)
                               2237                 :                :         {
                               2238                 :           2491 :             n_poly_args++;
 1553 tgl@sss.pgh.pa.us        2239                 :           2491 :             have_anymultirange = true;
 1772 akorotkov@postgresql     2240         [ +  + ]:           2491 :             if (actual_type == UNKNOWNOID)
                               2241                 :                :             {
                               2242                 :            132 :                 have_poly_unknowns = true;
                               2243                 :            132 :                 continue;
                               2244                 :                :             }
                               2245   [ -  +  -  - ]:           2359 :             if (allow_poly && decl_type == actual_type)
 1772 akorotkov@postgresql     2246                 :UBC           0 :                 continue;       /* no new information here */
 1772 akorotkov@postgresql     2247                 :CBC        2359 :             actual_type = getBaseType(actual_type); /* flatten domains */
                               2248   [ +  +  -  + ]:           2359 :             if (OidIsValid(multirange_typeid) && actual_type != multirange_typeid)
 1772 akorotkov@postgresql     2249         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2250                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2251                 :                :                          errmsg("arguments declared \"%s\" are not all alike", "anymultirange"),
                               2252                 :                :                          errdetail("%s versus %s",
                               2253                 :                :                                    format_type_be(multirange_typeid),
                               2254                 :                :                                    format_type_be(actual_type))));
 1772 akorotkov@postgresql     2255                 :CBC        2359 :             multirange_typeid = actual_type;
                               2256                 :                :         }
 2048 tgl@sss.pgh.pa.us        2257   [ +  +  +  + ]:         970140 :         else if (decl_type == ANYCOMPATIBLEOID ||
                               2258                 :                :                  decl_type == ANYCOMPATIBLENONARRAYOID)
                               2259                 :                :         {
                               2260                 :           1087 :             have_poly_anycompatible = true;
                               2261         [ +  + ]:           1087 :             if (decl_type == ANYCOMPATIBLENONARRAYOID)
                               2262                 :             12 :                 have_anycompatible_nonarray = true;
                               2263         [ +  + ]:           1087 :             if (actual_type == UNKNOWNOID)
                               2264                 :            582 :                 continue;
                               2265   [ +  +  +  + ]:            505 :             if (allow_poly && decl_type == actual_type)
                               2266                 :              4 :                 continue;       /* no new information here */
                               2267                 :                :             /* collect the actual types of non-unknown COMPATIBLE args */
                               2268                 :            501 :             anycompatible_actual_types[n_anycompatible_args++] = actual_type;
                               2269                 :                :         }
                               2270         [ +  + ]:         969053 :         else if (decl_type == ANYCOMPATIBLEARRAYOID)
                               2271                 :                :         {
                               2272                 :                :             Oid         anycompatible_elem_type;
                               2273                 :                : 
                               2274                 :           2737 :             have_poly_anycompatible = true;
                               2275                 :           2737 :             have_anycompatible_array = true;
                               2276         [ +  + ]:           2737 :             if (actual_type == UNKNOWNOID)
                               2277                 :             15 :                 continue;
                               2278   [ +  +  +  + ]:           2722 :             if (allow_poly && decl_type == actual_type)
                               2279                 :              4 :                 continue;       /* no new information here */
                               2280                 :           2718 :             actual_type = getBaseType(actual_type); /* flatten domains */
                               2281                 :           2718 :             anycompatible_elem_type = get_element_type(actual_type);
                               2282         [ -  + ]:           2718 :             if (!OidIsValid(anycompatible_elem_type))
 2048 tgl@sss.pgh.pa.us        2283         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2284                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2285                 :                :                          errmsg("argument declared %s is not an array but type %s",
                               2286                 :                :                                 "anycompatiblearray",
                               2287                 :                :                                 format_type_be(actual_type))));
                               2288                 :                :             /* collect the element type for common-supertype choice */
 2048 tgl@sss.pgh.pa.us        2289                 :CBC        2718 :             anycompatible_actual_types[n_anycompatible_args++] = anycompatible_elem_type;
                               2290                 :                :         }
                               2291         [ +  + ]:         966316 :         else if (decl_type == ANYCOMPATIBLERANGEOID)
                               2292                 :                :         {
                               2293                 :             69 :             have_poly_anycompatible = true;
                               2294                 :             69 :             have_anycompatible_range = true;
                               2295         [ +  + ]:             69 :             if (actual_type == UNKNOWNOID)
                               2296                 :              6 :                 continue;
                               2297   [ -  +  -  - ]:             63 :             if (allow_poly && decl_type == actual_type)
 2048 tgl@sss.pgh.pa.us        2298                 :UBC           0 :                 continue;       /* no new information here */
 2048 tgl@sss.pgh.pa.us        2299                 :CBC          63 :             actual_type = getBaseType(actual_type); /* flatten domains */
                               2300         [ +  + ]:             63 :             if (OidIsValid(anycompatible_range_typeid))
                               2301                 :                :             {
                               2302                 :                :                 /* All ANYCOMPATIBLERANGE arguments must be the same type */
                               2303         [ -  + ]:              3 :                 if (anycompatible_range_typeid != actual_type)
 2048 tgl@sss.pgh.pa.us        2304         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2305                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2306                 :                :                              errmsg("arguments declared \"%s\" are not all alike", "anycompatiblerange"),
                               2307                 :                :                              errdetail("%s versus %s",
                               2308                 :                :                                        format_type_be(anycompatible_range_typeid),
                               2309                 :                :                                        format_type_be(actual_type))));
                               2310                 :                :             }
                               2311                 :                :             else
                               2312                 :                :             {
 2048 tgl@sss.pgh.pa.us        2313                 :CBC          60 :                 anycompatible_range_typeid = actual_type;
                               2314                 :             60 :                 anycompatible_range_typelem = get_range_subtype(actual_type);
                               2315         [ -  + ]:             60 :                 if (!OidIsValid(anycompatible_range_typelem))
 2048 tgl@sss.pgh.pa.us        2316         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2317                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2318                 :                :                              errmsg("argument declared %s is not a range type but type %s",
                               2319                 :                :                                     "anycompatiblerange",
                               2320                 :                :                                     format_type_be(actual_type))));
                               2321                 :                :                 /* collect the subtype for common-supertype choice */
 2048 tgl@sss.pgh.pa.us        2322                 :CBC          60 :                 anycompatible_actual_types[n_anycompatible_args++] = anycompatible_range_typelem;
                               2323                 :                :             }
                               2324                 :                :         }
 1772 akorotkov@postgresql     2325         [ +  + ]:         966247 :         else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
                               2326                 :                :         {
                               2327                 :             48 :             have_poly_anycompatible = true;
 1553 tgl@sss.pgh.pa.us        2328                 :             48 :             have_anycompatible_multirange = true;
 1772 akorotkov@postgresql     2329         [ +  + ]:             48 :             if (actual_type == UNKNOWNOID)
                               2330                 :              9 :                 continue;
                               2331   [ -  +  -  - ]:             39 :             if (allow_poly && decl_type == actual_type)
 1772 akorotkov@postgresql     2332                 :UBC           0 :                 continue;       /* no new information here */
 1772 akorotkov@postgresql     2333                 :CBC          39 :             actual_type = getBaseType(actual_type); /* flatten domains */
                               2334         [ +  + ]:             39 :             if (OidIsValid(anycompatible_multirange_typeid))
                               2335                 :                :             {
                               2336                 :                :                 /* All ANYCOMPATIBLEMULTIRANGE arguments must be the same type */
                               2337         [ -  + ]:              3 :                 if (anycompatible_multirange_typeid != actual_type)
 1772 akorotkov@postgresql     2338         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2339                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2340                 :                :                              errmsg("arguments declared \"%s\" are not all alike", "anycompatiblemultirange"),
                               2341                 :                :                              errdetail("%s versus %s",
                               2342                 :                :                                        format_type_be(anycompatible_multirange_typeid),
                               2343                 :                :                                        format_type_be(actual_type))));
                               2344                 :                :             }
                               2345                 :                :             else
                               2346                 :                :             {
 1772 akorotkov@postgresql     2347                 :CBC          36 :                 anycompatible_multirange_typeid = actual_type;
                               2348                 :             36 :                 anycompatible_multirange_typelem = get_multirange_range(actual_type);
                               2349         [ -  + ]:             36 :                 if (!OidIsValid(anycompatible_multirange_typelem))
 1772 akorotkov@postgresql     2350         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2351                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2352                 :                :                              errmsg("argument declared %s is not a multirange type but type %s",
                               2353                 :                :                                     "anycompatiblemultirange",
                               2354                 :                :                                     format_type_be(actual_type))));
                               2355                 :                :                 /* we'll consider the subtype below */
                               2356                 :                :             }
                               2357                 :                :         }
                               2358                 :                :     }
                               2359                 :                : 
                               2360                 :                :     /*
                               2361                 :                :      * Fast Track: if none of the arguments are polymorphic, return the
                               2362                 :                :      * unmodified rettype.  Not our job to resolve it if it's polymorphic.
                               2363                 :                :      */
 2048 tgl@sss.pgh.pa.us        2364   [ +  +  +  + ]:CBC      515802 :     if (n_poly_args == 0 && !have_poly_anycompatible)
 8238                          2365                 :         485367 :         return rettype;
                               2366                 :                : 
                               2367                 :                :     /* Check matching of family-1 polymorphic arguments, if any */
 2050                          2368         [ +  + ]:          30435 :     if (n_poly_args)
                               2369                 :                :     {
                               2370                 :                :         /* Get the element type based on the array type, if we have one */
                               2371         [ +  + ]:          28481 :         if (OidIsValid(array_typeid))
                               2372                 :                :         {
                               2373                 :                :             Oid         array_typelem;
                               2374                 :                : 
                               2375         [ +  + ]:          14515 :             if (array_typeid == ANYARRAYOID)
                               2376                 :                :             {
                               2377                 :                :                 /*
                               2378                 :                :                  * Special case for matching ANYARRAY input to an ANYARRAY
                               2379                 :                :                  * argument: allow it iff no other arguments are family-1
                               2380                 :                :                  * polymorphics (otherwise we couldn't be sure whether the
                               2381                 :                :                  * array element type matches up) and the result type doesn't
                               2382                 :                :                  * require us to infer a specific element type.
                               2383                 :                :                  */
                               2384   [ +  -  +  + ]:             21 :                 if (n_poly_args != 1 ||
 2048                          2385         [ +  + ]:              9 :                     (rettype != ANYARRAYOID &&
                               2386   [ +  -  +  -  :              3 :                      IsPolymorphicTypeFamily1(rettype)))
                                     +  -  +  -  -  
                                                 + ]
 2050                          2387         [ +  - ]:              6 :                     ereport(ERROR,
                               2388                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2389                 :                :                              errmsg("cannot determine element type of \"anyarray\" argument")));
                               2390                 :             15 :                 array_typelem = ANYELEMENTOID;
                               2391                 :                :             }
                               2392                 :                :             else
                               2393                 :                :             {
                               2394                 :          14494 :                 array_typelem = get_element_type(array_typeid);
                               2395         [ -  + ]:          14494 :                 if (!OidIsValid(array_typelem))
 2050 tgl@sss.pgh.pa.us        2396         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2397                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2398                 :                :                              errmsg("argument declared %s is not an array but type %s",
                               2399                 :                :                                     "anyarray", format_type_be(array_typeid))));
                               2400                 :                :             }
                               2401                 :                : 
 2050 tgl@sss.pgh.pa.us        2402         [ +  + ]:CBC       14509 :             if (!OidIsValid(elem_typeid))
                               2403                 :                :             {
                               2404                 :                :                 /*
                               2405                 :                :                  * if we don't have an element type yet, use the one we just
                               2406                 :                :                  * got
                               2407                 :                :                  */
                               2408                 :          14468 :                 elem_typeid = array_typelem;
                               2409                 :                :             }
                               2410         [ -  + ]:             41 :             else if (array_typelem != elem_typeid)
                               2411                 :                :             {
                               2412                 :                :                 /* otherwise, they better match */
 6161 tgl@sss.pgh.pa.us        2413         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2414                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2415                 :                :                          errmsg("argument declared %s is not consistent with argument declared %s",
                               2416                 :                :                                 "anyarray", "anyelement"),
                               2417                 :                :                          errdetail("%s versus %s",
                               2418                 :                :                                    format_type_be(array_typeid),
                               2419                 :                :                                    format_type_be(elem_typeid))));
                               2420                 :                :             }
                               2421                 :                :         }
                               2422                 :                : 
                               2423                 :                :         /* Deduce range type from multirange type, or vice versa */
 1772 akorotkov@postgresql     2424         [ +  + ]:CBC       28475 :         if (OidIsValid(multirange_typeid))
                               2425                 :                :         {
                               2426                 :                :             Oid         multirange_typelem;
                               2427                 :                : 
                               2428                 :           1768 :             multirange_typelem = get_multirange_range(multirange_typeid);
                               2429         [ -  + ]:           1768 :             if (!OidIsValid(multirange_typelem))
 1772 akorotkov@postgresql     2430         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2431                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2432                 :                :                          errmsg("argument declared %s is not a multirange type but type %s",
                               2433                 :                :                                 "anymultirange",
                               2434                 :                :                                 format_type_be(multirange_typeid))));
                               2435                 :                : 
 1772 akorotkov@postgresql     2436         [ +  + ]:CBC        1768 :             if (!OidIsValid(range_typeid))
                               2437                 :                :             {
                               2438                 :                :                 /* if we don't have a range type yet, use the one we just got */
                               2439                 :           1101 :                 range_typeid = multirange_typelem;
                               2440                 :                :             }
                               2441         [ -  + ]:            667 :             else if (multirange_typelem != range_typeid)
                               2442                 :                :             {
                               2443                 :                :                 /* otherwise, they better match */
 1772 akorotkov@postgresql     2444         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2445                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2446                 :                :                          errmsg("argument declared %s is not consistent with argument declared %s",
                               2447                 :                :                                 "anymultirange", "anyrange"),
                               2448                 :                :                          errdetail("%s versus %s",
                               2449                 :                :                                    format_type_be(multirange_typeid),
                               2450                 :                :                                    format_type_be(range_typeid))));
                               2451                 :                :             }
                               2452                 :                :         }
 1553 tgl@sss.pgh.pa.us        2453   [ +  +  +  + ]:CBC       26707 :         else if (have_anymultirange && OidIsValid(range_typeid))
                               2454                 :                :         {
                               2455                 :            157 :             multirange_typeid = get_range_multirange(range_typeid);
                               2456                 :                :             /* We'll complain below if that didn't work */
                               2457                 :                :         }
                               2458                 :                : 
                               2459                 :                :         /* Get the element type based on the range type, if we have one */
                               2460         [ +  + ]:          28475 :         if (OidIsValid(range_typeid))
                               2461                 :                :         {
                               2462                 :                :             Oid         range_typelem;
                               2463                 :                : 
                               2464                 :           3757 :             range_typelem = get_range_subtype(range_typeid);
                               2465         [ -  + ]:           3757 :             if (!OidIsValid(range_typelem))
 1553 tgl@sss.pgh.pa.us        2466         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2467                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2468                 :                :                          errmsg("argument declared %s is not a range type but type %s",
                               2469                 :                :                                 "anyrange",
                               2470                 :                :                                 format_type_be(range_typeid))));
                               2471                 :                : 
 1772 akorotkov@postgresql     2472         [ +  + ]:CBC        3757 :             if (!OidIsValid(elem_typeid))
                               2473                 :                :             {
                               2474                 :                :                 /*
                               2475                 :                :                  * if we don't have an element type yet, use the one we just
                               2476                 :                :                  * got
                               2477                 :                :                  */
                               2478                 :           3514 :                 elem_typeid = range_typelem;
                               2479                 :                :             }
                               2480         [ -  + ]:            243 :             else if (range_typelem != elem_typeid)
                               2481                 :                :             {
                               2482                 :                :                 /* otherwise, they better match */
 1772 akorotkov@postgresql     2483         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2484                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2485                 :                :                          errmsg("argument declared %s is not consistent with argument declared %s",
                               2486                 :                :                                 "anyrange", "anyelement"),
                               2487                 :                :                          errdetail("%s versus %s",
                               2488                 :                :                                    format_type_be(range_typeid),
                               2489                 :                :                                    format_type_be(elem_typeid))));
                               2490                 :                :             }
                               2491                 :                :         }
                               2492                 :                : 
 5107 heikki.linnakangas@i     2493         [ +  + ]:CBC       28475 :         if (!OidIsValid(elem_typeid))
                               2494                 :                :         {
 2050 tgl@sss.pgh.pa.us        2495         [ +  + ]:            110 :             if (allow_poly)
                               2496                 :                :             {
                               2497                 :             98 :                 elem_typeid = ANYELEMENTOID;
                               2498                 :             98 :                 array_typeid = ANYARRAYOID;
                               2499                 :             98 :                 range_typeid = ANYRANGEOID;
 1772 akorotkov@postgresql     2500                 :             98 :                 multirange_typeid = ANYMULTIRANGEOID;
                               2501                 :                :             }
                               2502                 :                :             else
                               2503                 :                :             {
                               2504                 :                :                 /*
                               2505                 :                :                  * Only way to get here is if all the family-1 polymorphic
                               2506                 :                :                  * arguments have UNKNOWN inputs.
                               2507                 :                :                  */
 2050 tgl@sss.pgh.pa.us        2508         [ +  - ]:             12 :                 ereport(ERROR,
                               2509                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2510                 :                :                          errmsg("could not determine polymorphic type because input has type %s",
                               2511                 :                :                                 "unknown")));
                               2512                 :                :             }
                               2513                 :                :         }
                               2514                 :                : 
                               2515   [ +  +  +  - ]:          28463 :         if (have_anynonarray && elem_typeid != ANYELEMENTOID)
                               2516                 :                :         {
                               2517                 :                :             /*
                               2518                 :                :              * require the element type to not be an array or domain over
                               2519                 :                :              * array
                               2520                 :                :              */
                               2521         [ -  + ]:           7178 :             if (type_is_array_domain(elem_typeid))
 2050 tgl@sss.pgh.pa.us        2522         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2523                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2524                 :                :                          errmsg("type matched to anynonarray is an array type: %s",
                               2525                 :                :                                 format_type_be(elem_typeid))));
                               2526                 :                :         }
                               2527                 :                : 
 2050 tgl@sss.pgh.pa.us        2528   [ +  +  +  - ]:CBC       28463 :         if (have_anyenum && elem_typeid != ANYELEMENTOID)
                               2529                 :                :         {
                               2530                 :                :             /* require the element type to be an enum */
                               2531         [ -  + ]:            286 :             if (!type_is_enum(elem_typeid))
 2050 tgl@sss.pgh.pa.us        2532         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2533                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2534                 :                :                          errmsg("type matched to anyenum is not an enum type: %s",
                               2535                 :                :                                 format_type_be(elem_typeid))));
                               2536                 :                :         }
                               2537                 :                :     }
                               2538                 :                : 
                               2539                 :                :     /* Check matching of family-2 polymorphic arguments, if any */
 2048 tgl@sss.pgh.pa.us        2540         [ +  + ]:CBC       30417 :     if (have_poly_anycompatible)
                               2541                 :                :     {
                               2542                 :                :         /* Deduce range type from multirange type, or vice versa */
 1553                          2543         [ +  + ]:           1981 :         if (OidIsValid(anycompatible_multirange_typeid))
                               2544                 :                :         {
                               2545         [ +  + ]:             36 :             if (OidIsValid(anycompatible_range_typeid))
                               2546                 :                :             {
                               2547         [ -  + ]:              3 :                 if (anycompatible_multirange_typelem !=
                               2548                 :                :                     anycompatible_range_typeid)
 1553 tgl@sss.pgh.pa.us        2549         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2550                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2551                 :                :                              errmsg("argument declared %s is not consistent with argument declared %s",
                               2552                 :                :                                     "anycompatiblemultirange",
                               2553                 :                :                                     "anycompatiblerange"),
                               2554                 :                :                              errdetail("%s versus %s",
                               2555                 :                :                                        format_type_be(anycompatible_multirange_typeid),
                               2556                 :                :                                        format_type_be(anycompatible_range_typeid))));
                               2557                 :                :             }
                               2558                 :                :             else
                               2559                 :                :             {
 1553 tgl@sss.pgh.pa.us        2560                 :CBC          33 :                 anycompatible_range_typeid = anycompatible_multirange_typelem;
                               2561                 :             33 :                 anycompatible_range_typelem = get_range_subtype(anycompatible_range_typeid);
                               2562         [ -  + ]:             33 :                 if (!OidIsValid(anycompatible_range_typelem))
 1553 tgl@sss.pgh.pa.us        2563         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2564                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2565                 :                :                              errmsg("argument declared %s is not a multirange type but type %s",
                               2566                 :                :                                     "anycompatiblemultirange",
                               2567                 :                :                                     format_type_be(anycompatible_multirange_typeid))));
                               2568                 :                :                 /* this enables element type matching check below */
 1553 tgl@sss.pgh.pa.us        2569                 :CBC          33 :                 have_anycompatible_range = true;
                               2570                 :                :                 /* collect the subtype for common-supertype choice */
                               2571                 :             33 :                 anycompatible_actual_types[n_anycompatible_args++] =
                               2572                 :                :                     anycompatible_range_typelem;
                               2573                 :                :             }
                               2574                 :                :         }
                               2575   [ +  +  +  + ]:           1945 :         else if (have_anycompatible_multirange &&
                               2576                 :                :                  OidIsValid(anycompatible_range_typeid))
                               2577                 :                :         {
                               2578                 :              3 :             anycompatible_multirange_typeid = get_range_multirange(anycompatible_range_typeid);
                               2579                 :                :             /* We'll complain below if that didn't work */
                               2580                 :                :         }
                               2581                 :                : 
 2048                          2582         [ +  + ]:           1981 :         if (n_anycompatible_args > 0)
                               2583                 :                :         {
                               2584                 :                :             anycompatible_typeid =
                               2585                 :           1965 :                 select_common_type_from_oids(n_anycompatible_args,
                               2586                 :                :                                              anycompatible_actual_types,
                               2587                 :                :                                              false);
                               2588                 :                : 
                               2589                 :                :             /* We have to verify that the selected type actually works */
 1367                          2590         [ -  + ]:           1965 :             if (!verify_common_type_from_oids(anycompatible_typeid,
                               2591                 :                :                                               n_anycompatible_args,
                               2592                 :                :                                               anycompatible_actual_types))
 1367 tgl@sss.pgh.pa.us        2593         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2594                 :                :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2595                 :                :                          errmsg("arguments of anycompatible family cannot be cast to a common type")));
                               2596                 :                : 
 2048 tgl@sss.pgh.pa.us        2597         [ +  + ]:CBC        1965 :             if (have_anycompatible_array)
                               2598                 :                :             {
                               2599                 :           1866 :                 anycompatible_array_typeid = get_array_type(anycompatible_typeid);
                               2600         [ -  + ]:           1866 :                 if (!OidIsValid(anycompatible_array_typeid))
 2048 tgl@sss.pgh.pa.us        2601         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2602                 :                :                             (errcode(ERRCODE_UNDEFINED_OBJECT),
                               2603                 :                :                              errmsg("could not find array type for data type %s",
                               2604                 :                :                                     format_type_be(anycompatible_typeid))));
                               2605                 :                :             }
                               2606                 :                : 
 2048 tgl@sss.pgh.pa.us        2607         [ +  + ]:CBC        1965 :             if (have_anycompatible_range)
                               2608                 :                :             {
                               2609                 :                :                 /* we can't infer a range type from the others */
                               2610         [ +  + ]:             96 :                 if (!OidIsValid(anycompatible_range_typeid))
                               2611         [ +  - ]:              3 :                     ereport(ERROR,
                               2612                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2613                 :                :                              errmsg("could not determine polymorphic type %s because input has type %s",
                               2614                 :                :                                     "anycompatiblerange", "unknown")));
                               2615                 :                : 
                               2616                 :                :                 /*
                               2617                 :                :                  * the anycompatible type must exactly match the range element
                               2618                 :                :                  * type
                               2619                 :                :                  */
                               2620         [ -  + ]:             93 :                 if (anycompatible_range_typelem != anycompatible_typeid)
 2048 tgl@sss.pgh.pa.us        2621         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2622                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2623                 :                :                              errmsg("anycompatiblerange type %s does not match anycompatible type %s",
                               2624                 :                :                                     format_type_be(anycompatible_range_typeid),
                               2625                 :                :                                     format_type_be(anycompatible_typeid))));
                               2626                 :                :             }
                               2627                 :                : 
 1553 tgl@sss.pgh.pa.us        2628         [ +  + ]:CBC        1962 :             if (have_anycompatible_multirange)
                               2629                 :                :             {
                               2630                 :                :                 /* we can't infer a multirange type from the others */
                               2631         [ +  + ]:             42 :                 if (!OidIsValid(anycompatible_multirange_typeid))
                               2632         [ +  - ]:              3 :                     ereport(ERROR,
                               2633                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2634                 :                :                              errmsg("could not determine polymorphic type %s because input has type %s",
                               2635                 :                :                                     "anycompatiblemultirange", "unknown")));
                               2636                 :                : 
                               2637                 :                :                 /*
                               2638                 :                :                  * the anycompatible type must exactly match the multirange
                               2639                 :                :                  * element type
                               2640                 :                :                  */
                               2641         [ -  + ]:             39 :                 if (anycompatible_range_typelem != anycompatible_typeid)
 1553 tgl@sss.pgh.pa.us        2642         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2643                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2644                 :                :                              errmsg("anycompatiblemultirange type %s does not match anycompatible type %s",
                               2645                 :                :                                     format_type_be(anycompatible_multirange_typeid),
                               2646                 :                :                                     format_type_be(anycompatible_typeid))));
                               2647                 :                :             }
                               2648                 :                : 
 2048 tgl@sss.pgh.pa.us        2649         [ +  + ]:CBC        1959 :             if (have_anycompatible_nonarray)
                               2650                 :                :             {
                               2651                 :                :                 /*
                               2652                 :                :                  * require the element type to not be an array or domain over
                               2653                 :                :                  * array
                               2654                 :                :                  */
                               2655         [ -  + ]:              6 :                 if (type_is_array_domain(anycompatible_typeid))
 2048 tgl@sss.pgh.pa.us        2656         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2657                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2658                 :                :                              errmsg("type matched to anycompatiblenonarray is an array type: %s",
                               2659                 :                :                                     format_type_be(anycompatible_typeid))));
                               2660                 :                :             }
                               2661                 :                :         }
                               2662                 :                :         else
                               2663                 :                :         {
 2048 tgl@sss.pgh.pa.us        2664         [ +  + ]:CBC          16 :             if (allow_poly)
                               2665                 :                :             {
                               2666                 :              4 :                 anycompatible_typeid = ANYCOMPATIBLEOID;
                               2667                 :              4 :                 anycompatible_array_typeid = ANYCOMPATIBLEARRAYOID;
                               2668                 :              4 :                 anycompatible_range_typeid = ANYCOMPATIBLERANGEOID;
 1772 akorotkov@postgresql     2669                 :              4 :                 anycompatible_multirange_typeid = ANYCOMPATIBLEMULTIRANGEOID;
                               2670                 :                :             }
                               2671                 :                :             else
                               2672                 :                :             {
                               2673                 :                :                 /*
                               2674                 :                :                  * Only way to get here is if all the family-2 polymorphic
                               2675                 :                :                  * arguments have UNKNOWN inputs.  Resolve to TEXT as
                               2676                 :                :                  * select_common_type() would do.  That doesn't license us to
                               2677                 :                :                  * use TEXTRANGE or TEXTMULTIRANGE, though.
                               2678                 :                :                  */
 2048 tgl@sss.pgh.pa.us        2679                 :             12 :                 anycompatible_typeid = TEXTOID;
                               2680                 :             12 :                 anycompatible_array_typeid = TEXTARRAYOID;
                               2681         [ +  + ]:             12 :                 if (have_anycompatible_range)
                               2682         [ +  - ]:              6 :                     ereport(ERROR,
                               2683                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2684                 :                :                              errmsg("could not determine polymorphic type %s because input has type %s",
                               2685                 :                :                                     "anycompatiblerange", "unknown")));
 1553                          2686         [ +  + ]:              6 :                 if (have_anycompatible_multirange)
                               2687         [ +  - ]:              3 :                     ereport(ERROR,
                               2688                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2689                 :                :                              errmsg("could not determine polymorphic type %s because input has type %s",
                               2690                 :                :                                     "anycompatiblemultirange", "unknown")));
                               2691                 :                :             }
                               2692                 :                :         }
                               2693                 :                : 
                               2694                 :                :         /* replace family-2 polymorphic types by selected types */
 2048                          2695         [ +  + ]:           5955 :         for (int j = 0; j < nargs; j++)
                               2696                 :                :         {
                               2697                 :           3989 :             Oid         decl_type = declared_arg_types[j];
                               2698                 :                : 
                               2699   [ +  +  +  + ]:           3989 :             if (decl_type == ANYCOMPATIBLEOID ||
                               2700                 :                :                 decl_type == ANYCOMPATIBLENONARRAYOID)
                               2701                 :           1081 :                 declared_arg_types[j] = anycompatible_typeid;
                               2702         [ +  + ]:           2908 :             else if (decl_type == ANYCOMPATIBLEARRAYOID)
                               2703                 :           2737 :                 declared_arg_types[j] = anycompatible_array_typeid;
                               2704         [ +  + ]:            171 :             else if (decl_type == ANYCOMPATIBLERANGEOID)
                               2705                 :             63 :                 declared_arg_types[j] = anycompatible_range_typeid;
 1772 akorotkov@postgresql     2706         [ +  + ]:            108 :             else if (decl_type == ANYCOMPATIBLEMULTIRANGEOID)
                               2707                 :             39 :                 declared_arg_types[j] = anycompatible_multirange_typeid;
                               2708                 :                :         }
                               2709                 :                :     }
                               2710                 :                : 
                               2711                 :                :     /*
                               2712                 :                :      * If we had any UNKNOWN inputs for family-1 polymorphic arguments,
                               2713                 :                :      * re-scan to assign correct types to them.
                               2714                 :                :      *
                               2715                 :                :      * Note: we don't have to consider unknown inputs that were matched to
                               2716                 :                :      * family-2 polymorphic arguments, because we forcibly updated their
                               2717                 :                :      * declared_arg_types[] positions just above.
                               2718                 :                :      */
 2050 tgl@sss.pgh.pa.us        2719         [ +  + ]:          30402 :     if (have_poly_unknowns)
                               2720                 :                :     {
                               2721         [ +  + ]:           9928 :         for (int j = 0; j < nargs; j++)
                               2722                 :                :         {
 6718                          2723                 :           6767 :             Oid         decl_type = declared_arg_types[j];
 8120 bruce@momjian.us         2724                 :           6767 :             Oid         actual_type = actual_arg_types[j];
                               2725                 :                : 
 8238 tgl@sss.pgh.pa.us        2726         [ +  + ]:           6767 :             if (actual_type != UNKNOWNOID)
                               2727                 :           3275 :                 continue;
                               2728                 :                : 
 6718                          2729   [ +  +  +  + ]:           3492 :             if (decl_type == ANYELEMENTOID ||
                               2730         [ +  + ]:           3205 :                 decl_type == ANYNONARRAYOID ||
                               2731                 :                :                 decl_type == ANYENUMOID)
 8238                          2732                 :            375 :                 declared_arg_types[j] = elem_typeid;
 6718                          2733         [ +  + ]:           3117 :             else if (decl_type == ANYARRAYOID)
                               2734                 :                :             {
 8238                          2735         [ +  + ]:           2252 :                 if (!OidIsValid(array_typeid))
                               2736                 :                :                 {
                               2737                 :             15 :                     array_typeid = get_array_type(elem_typeid);
                               2738         [ -  + ]:             15 :                     if (!OidIsValid(array_typeid))
 8136 tgl@sss.pgh.pa.us        2739         [ #  # ]:UBC           0 :                         ereport(ERROR,
                               2740                 :                :                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
                               2741                 :                :                                  errmsg("could not find array type for data type %s",
                               2742                 :                :                                         format_type_be(elem_typeid))));
                               2743                 :                :                 }
 8238 tgl@sss.pgh.pa.us        2744                 :CBC        2252 :                 declared_arg_types[j] = array_typeid;
                               2745                 :                :             }
 5107 heikki.linnakangas@i     2746         [ +  + ]:            865 :             else if (decl_type == ANYRANGEOID)
                               2747                 :                :             {
                               2748         [ -  + ]:            417 :                 if (!OidIsValid(range_typeid))
                               2749                 :                :                 {
                               2750                 :                :                     /* we can't infer a range type from the others */
 5107 heikki.linnakangas@i     2751         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2752                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2753                 :                :                              errmsg("could not determine polymorphic type %s because input has type %s",
                               2754                 :                :                                     "anyrange", "unknown")));
                               2755                 :                :                 }
 5107 heikki.linnakangas@i     2756                 :CBC         417 :                 declared_arg_types[j] = range_typeid;
                               2757                 :                :             }
 1772 akorotkov@postgresql     2758         [ +  + ]:            448 :             else if (decl_type == ANYMULTIRANGEOID)
                               2759                 :                :             {
                               2760         [ -  + ]:            126 :                 if (!OidIsValid(multirange_typeid))
                               2761                 :                :                 {
                               2762                 :                :                     /* we can't infer a multirange type from the others */
 1772 akorotkov@postgresql     2763         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2764                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2765                 :                :                              errmsg("could not determine polymorphic type %s because input has type %s",
                               2766                 :                :                                     "anymultirange", "unknown")));
                               2767                 :                :                 }
 1772 akorotkov@postgresql     2768                 :CBC         126 :                 declared_arg_types[j] = multirange_typeid;
                               2769                 :                :             }
                               2770                 :                :         }
                               2771                 :                :     }
                               2772                 :                : 
                               2773                 :                :     /* if we return ANYELEMENT use the appropriate argument type */
 2050 tgl@sss.pgh.pa.us        2774   [ +  +  +  - ]:          30402 :     if (rettype == ANYELEMENTOID ||
                               2775         [ +  + ]:          24679 :         rettype == ANYNONARRAYOID ||
                               2776                 :                :         rettype == ANYENUMOID)
                               2777                 :           5837 :         return elem_typeid;
                               2778                 :                : 
                               2779                 :                :     /* if we return ANYARRAY use the appropriate argument type */
 8238                          2780         [ +  + ]:          24565 :     if (rettype == ANYARRAYOID)
                               2781                 :                :     {
                               2782         [ +  + ]:           7344 :         if (!OidIsValid(array_typeid))
                               2783                 :                :         {
 8160 bruce@momjian.us         2784                 :           6745 :             array_typeid = get_array_type(elem_typeid);
 8238 tgl@sss.pgh.pa.us        2785         [ -  + ]:           6745 :             if (!OidIsValid(array_typeid))
 8136 tgl@sss.pgh.pa.us        2786         [ #  # ]:UBC           0 :                 ereport(ERROR,
                               2787                 :                :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
                               2788                 :                :                          errmsg("could not find array type for data type %s",
                               2789                 :                :                                 format_type_be(elem_typeid))));
                               2790                 :                :         }
 8238 tgl@sss.pgh.pa.us        2791                 :CBC        7344 :         return array_typeid;
                               2792                 :                :     }
                               2793                 :                : 
                               2794                 :                :     /* if we return ANYRANGE use the appropriate argument type */
 5107 heikki.linnakangas@i     2795         [ +  + ]:          17221 :     if (rettype == ANYRANGEOID)
                               2796                 :                :     {
                               2797                 :                :         /* this error is unreachable if the function signature is valid: */
                               2798         [ -  + ]:            211 :         if (!OidIsValid(range_typeid))
 5107 heikki.linnakangas@i     2799         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2800                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2801                 :                :                      errmsg_internal("could not determine polymorphic type %s because input has type %s",
                               2802                 :                :                                      "anyrange", "unknown")));
 5107 heikki.linnakangas@i     2803                 :CBC         211 :         return range_typeid;
                               2804                 :                :     }
                               2805                 :                : 
                               2806                 :                :     /* if we return ANYMULTIRANGE use the appropriate argument type */
 1772 akorotkov@postgresql     2807         [ +  + ]:          17010 :     if (rettype == ANYMULTIRANGEOID)
                               2808                 :                :     {
                               2809                 :                :         /* this error is unreachable if the function signature is valid: */
                               2810         [ -  + ]:            442 :         if (!OidIsValid(multirange_typeid))
 1553 tgl@sss.pgh.pa.us        2811         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2812                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2813                 :                :                      errmsg_internal("could not determine polymorphic type %s because input has type %s",
                               2814                 :                :                                      "anymultirange", "unknown")));
 1772 akorotkov@postgresql     2815                 :CBC         442 :         return multirange_typeid;
                               2816                 :                :     }
                               2817                 :                : 
                               2818                 :                :     /* if we return ANYCOMPATIBLE use the appropriate type */
 2048 tgl@sss.pgh.pa.us        2819   [ +  +  -  + ]:          16568 :     if (rettype == ANYCOMPATIBLEOID ||
                               2820                 :                :         rettype == ANYCOMPATIBLENONARRAYOID)
                               2821                 :                :     {
                               2822                 :                :         /* this error is unreachable if the function signature is valid: */
                               2823         [ -  + ]:             79 :         if (!OidIsValid(anycompatible_typeid))
 2048 tgl@sss.pgh.pa.us        2824         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2825                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2826                 :                :                      errmsg_internal("could not identify anycompatible type")));
 2048 tgl@sss.pgh.pa.us        2827                 :CBC          79 :         return anycompatible_typeid;
                               2828                 :                :     }
                               2829                 :                : 
                               2830                 :                :     /* if we return ANYCOMPATIBLEARRAY use the appropriate type */
                               2831         [ +  + ]:          16489 :     if (rettype == ANYCOMPATIBLEARRAYOID)
                               2832                 :                :     {
                               2833                 :                :         /* this error is unreachable if the function signature is valid: */
                               2834         [ -  + ]:           1698 :         if (!OidIsValid(anycompatible_array_typeid))
 2048 tgl@sss.pgh.pa.us        2835         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2836                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2837                 :                :                      errmsg_internal("could not identify anycompatiblearray type")));
 2048 tgl@sss.pgh.pa.us        2838                 :CBC        1698 :         return anycompatible_array_typeid;
                               2839                 :                :     }
                               2840                 :                : 
                               2841                 :                :     /* if we return ANYCOMPATIBLERANGE use the appropriate argument type */
                               2842         [ +  + ]:          14791 :     if (rettype == ANYCOMPATIBLERANGEOID)
                               2843                 :                :     {
                               2844                 :                :         /* this error is unreachable if the function signature is valid: */
                               2845         [ -  + ]:             21 :         if (!OidIsValid(anycompatible_range_typeid))
 2048 tgl@sss.pgh.pa.us        2846         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2847                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2848                 :                :                      errmsg_internal("could not identify anycompatiblerange type")));
 2048 tgl@sss.pgh.pa.us        2849                 :CBC          21 :         return anycompatible_range_typeid;
                               2850                 :                :     }
                               2851                 :                : 
                               2852                 :                :     /* if we return ANYCOMPATIBLEMULTIRANGE use the appropriate argument type */
 1772 akorotkov@postgresql     2853         [ +  + ]:          14770 :     if (rettype == ANYCOMPATIBLEMULTIRANGEOID)
                               2854                 :                :     {
                               2855                 :                :         /* this error is unreachable if the function signature is valid: */
                               2856         [ -  + ]:             15 :         if (!OidIsValid(anycompatible_multirange_typeid))
 1772 akorotkov@postgresql     2857         [ #  # ]:UBC           0 :             ereport(ERROR,
                               2858                 :                :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
                               2859                 :                :                      errmsg_internal("could not identify anycompatiblemultirange type")));
 1772 akorotkov@postgresql     2860                 :CBC          15 :         return anycompatible_multirange_typeid;
                               2861                 :                :     }
                               2862                 :                : 
                               2863                 :                :     /* we don't return a generic type; send back the original return type */
 8238 tgl@sss.pgh.pa.us        2864                 :          14755 :     return rettype;
                               2865                 :                : }
                               2866                 :                : 
                               2867                 :                : /*
                               2868                 :                :  * check_valid_polymorphic_signature()
                               2869                 :                :  *      Is a proposed function signature valid per polymorphism rules?
                               2870                 :                :  *
                               2871                 :                :  * Returns NULL if the signature is valid (either ret_type is not polymorphic,
                               2872                 :                :  * or it can be deduced from the given declared argument types).  Otherwise,
                               2873                 :                :  * returns a palloc'd, already translated errdetail string saying why not.
                               2874                 :                :  */
                               2875                 :                : char *
 2050                          2876                 :          20267 : check_valid_polymorphic_signature(Oid ret_type,
                               2877                 :                :                                   const Oid *declared_arg_types,
                               2878                 :                :                                   int nargs)
                               2879                 :                : {
 1772 akorotkov@postgresql     2880   [ +  +  +  + ]:          20267 :     if (ret_type == ANYRANGEOID || ret_type == ANYMULTIRANGEOID)
                               2881                 :                :     {
                               2882                 :                :         /*
                               2883                 :                :          * ANYRANGE and ANYMULTIRANGE require an ANYRANGE or ANYMULTIRANGE
                               2884                 :                :          * input, else we can't tell which of several range types with the
                               2885                 :                :          * same element type to use.
                               2886                 :                :          */
 2050 tgl@sss.pgh.pa.us        2887         [ +  + ]:            114 :         for (int i = 0; i < nargs; i++)
                               2888                 :                :         {
 1772 akorotkov@postgresql     2889         [ +  + ]:             78 :             if (declared_arg_types[i] == ANYRANGEOID ||
                               2890         [ +  + ]:             59 :                 declared_arg_types[i] == ANYMULTIRANGEOID)
 2050 tgl@sss.pgh.pa.us        2891                 :             38 :                 return NULL;    /* OK */
                               2892                 :                :         }
 1772 akorotkov@postgresql     2893                 :             36 :         return psprintf(_("A result of type %s requires at least one input of type anyrange or anymultirange."),
                               2894                 :                :                         format_type_be(ret_type));
                               2895                 :                :     }
                               2896   [ +  +  +  + ]:          20193 :     else if (ret_type == ANYCOMPATIBLERANGEOID || ret_type == ANYCOMPATIBLEMULTIRANGEOID)
                               2897                 :                :     {
                               2898                 :                :         /*
                               2899                 :                :          * ANYCOMPATIBLERANGE and ANYCOMPATIBLEMULTIRANGE require an
                               2900                 :                :          * ANYCOMPATIBLERANGE or ANYCOMPATIBLEMULTIRANGE input, else we can't
                               2901                 :                :          * tell which of several range types with the same element type to
                               2902                 :                :          * use.
                               2903                 :                :          */
                               2904         [ +  + ]:             72 :         for (int i = 0; i < nargs; i++)
                               2905                 :                :         {
                               2906         [ +  + ]:             51 :             if (declared_arg_types[i] == ANYCOMPATIBLERANGEOID ||
                               2907         [ +  + ]:             36 :                 declared_arg_types[i] == ANYCOMPATIBLEMULTIRANGEOID)
                               2908                 :             24 :                 return NULL;    /* OK */
                               2909                 :                :         }
                               2910                 :             21 :         return psprintf(_("A result of type %s requires at least one input of type anycompatiblerange or anycompatiblemultirange."),
                               2911                 :                :                         format_type_be(ret_type));
                               2912                 :                :     }
 2048 tgl@sss.pgh.pa.us        2913   [ +  +  +  +  :          20148 :     else if (IsPolymorphicTypeFamily1(ret_type))
                                     +  -  +  -  +  
                                           -  -  + ]
                               2914                 :                :     {
                               2915                 :                :         /* Otherwise, any family-1 type can be deduced from any other */
 2050                          2916         [ +  + ]:            640 :         for (int i = 0; i < nargs; i++)
                               2917                 :                :         {
 2048                          2918   [ +  +  +  +  :            583 :             if (IsPolymorphicTypeFamily1(declared_arg_types[i]))
                                     +  -  +  -  +  
                                           +  +  + ]
 2050                          2919                 :            532 :                 return NULL;    /* OK */
                               2920                 :                :         }
                               2921                 :                :         /* Keep this list in sync with IsPolymorphicTypeFamily1! */
 1772 akorotkov@postgresql     2922                 :             57 :         return psprintf(_("A result of type %s requires at least one input of type anyelement, anyarray, anynonarray, anyenum, anyrange, or anymultirange."),
                               2923                 :                :                         format_type_be(ret_type));
                               2924                 :                :     }
 2048 tgl@sss.pgh.pa.us        2925   [ +  +  +  +  :          19559 :     else if (IsPolymorphicTypeFamily2(ret_type))
                                     +  -  +  -  -  
                                                 + ]
                               2926                 :                :     {
                               2927                 :                :         /* Otherwise, any family-2 type can be deduced from any other */
                               2928         [ +  + ]:            118 :         for (int i = 0; i < nargs; i++)
                               2929                 :                :         {
                               2930   [ +  +  +  +  :            115 :             if (IsPolymorphicTypeFamily2(declared_arg_types[i]))
                                     +  +  +  +  +  
                                                 + ]
                               2931                 :             94 :                 return NULL;    /* OK */
                               2932                 :                :         }
                               2933                 :                :         /* Keep this list in sync with IsPolymorphicTypeFamily2! */
 1553                          2934                 :              3 :         return psprintf(_("A result of type %s requires at least one input of type anycompatible, anycompatiblearray, anycompatiblenonarray, anycompatiblerange, or anycompatiblemultirange."),
                               2935                 :                :                         format_type_be(ret_type));
                               2936                 :                :     }
                               2937                 :                :     else
 2050                          2938                 :          19462 :         return NULL;            /* OK, ret_type is not polymorphic */
                               2939                 :                : }
                               2940                 :                : 
                               2941                 :                : /*
                               2942                 :                :  * check_valid_internal_signature()
                               2943                 :                :  *      Is a proposed function signature valid per INTERNAL safety rules?
                               2944                 :                :  *
                               2945                 :                :  * Returns NULL if OK, or a suitable error message if ret_type is INTERNAL but
                               2946                 :                :  * none of the declared arg types are.  (It's unsafe to create such a function
                               2947                 :                :  * since it would allow invocation of INTERNAL-consuming functions directly
                               2948                 :                :  * from SQL.)  It's overkill to return the error detail message, since there
                               2949                 :                :  * is only one possibility, but we do it like this to keep the API similar to
                               2950                 :                :  * check_valid_polymorphic_signature().
                               2951                 :                :  */
                               2952                 :                : char *
                               2953                 :          19739 : check_valid_internal_signature(Oid ret_type,
                               2954                 :                :                                const Oid *declared_arg_types,
                               2955                 :                :                                int nargs)
                               2956                 :                : {
                               2957         [ +  + ]:          19739 :     if (ret_type == INTERNALOID)
                               2958                 :                :     {
                               2959         [ +  - ]:            963 :         for (int i = 0; i < nargs; i++)
                               2960                 :                :         {
                               2961         [ +  + ]:            963 :             if (declared_arg_types[i] == ret_type)
                               2962                 :            670 :                 return NULL;    /* OK */
                               2963                 :                :         }
 2050 tgl@sss.pgh.pa.us        2964                 :UBC           0 :         return pstrdup(_("A result of type internal requires at least one input of type internal."));
                               2965                 :                :     }
                               2966                 :                :     else
 2050 tgl@sss.pgh.pa.us        2967                 :CBC       19069 :         return NULL;            /* OK, ret_type is not INTERNAL */
                               2968                 :                : }
                               2969                 :                : 
                               2970                 :                : 
                               2971                 :                : /* TypeCategory()
                               2972                 :                :  *      Assign a category to the specified type OID.
                               2973                 :                :  *
                               2974                 :                :  * NB: this must not return TYPCATEGORY_INVALID.
                               2975                 :                :  */
                               2976                 :                : TYPCATEGORY
 6298                          2977                 :          93074 : TypeCategory(Oid type)
                               2978                 :                : {
                               2979                 :                :     char        typcategory;
                               2980                 :                :     bool        typispreferred;
                               2981                 :                : 
                               2982                 :          93074 :     get_type_category_preferred(type, &typcategory, &typispreferred);
                               2983         [ -  + ]:          93074 :     Assert(typcategory != TYPCATEGORY_INVALID);
                               2984                 :          93074 :     return (TYPCATEGORY) typcategory;
                               2985                 :                : }
                               2986                 :                : 
                               2987                 :                : 
                               2988                 :                : /* IsPreferredType()
                               2989                 :                :  *      Check if this type is a preferred type for the given category.
                               2990                 :                :  *
                               2991                 :                :  * If category is TYPCATEGORY_INVALID, then we'll return true for preferred
                               2992                 :                :  * types of any category; otherwise, only for preferred types of that
                               2993                 :                :  * category.
                               2994                 :                :  */
                               2995                 :                : bool
                               2996                 :          16088 : IsPreferredType(TYPCATEGORY category, Oid type)
                               2997                 :                : {
                               2998                 :                :     char        typcategory;
                               2999                 :                :     bool        typispreferred;
                               3000                 :                : 
                               3001                 :          16088 :     get_type_category_preferred(type, &typcategory, &typispreferred);
                               3002   [ +  +  -  + ]:          16088 :     if (category == typcategory || category == TYPCATEGORY_INVALID)
                               3003                 :          10431 :         return typispreferred;
                               3004                 :                :     else
 8190                          3005                 :           5657 :         return false;
                               3006                 :                : }
                               3007                 :                : 
                               3008                 :                : 
                               3009                 :                : /* IsBinaryCoercible()
                               3010                 :                :  *      Check if srctype is binary-coercible to targettype.
                               3011                 :                :  *
                               3012                 :                :  * This notion allows us to cheat and directly exchange values without
                               3013                 :                :  * going through the trouble of calling a conversion function.  Note that
                               3014                 :                :  * in general, this should only be an implementation shortcut.  Before 7.4,
                               3015                 :                :  * this was also used as a heuristic for resolving overloaded functions and
                               3016                 :                :  * operators, but that's basically a bad idea.
                               3017                 :                :  *
                               3018                 :                :  * As of 7.3, binary coercibility isn't hardwired into the code anymore.
                               3019                 :                :  * We consider two types binary-coercible if there is an implicitly
                               3020                 :                :  * invokable, no-function-needed pg_cast entry.  Also, a domain is always
                               3021                 :                :  * binary-coercible to its base type, though *not* vice versa (in the other
                               3022                 :                :  * direction, one must apply domain constraint checks before accepting the
                               3023                 :                :  * value as legitimate).  We also need to special-case various polymorphic
                               3024                 :                :  * types.
                               3025                 :                :  *
                               3026                 :                :  * This function replaces IsBinaryCompatible(), which was an inherently
                               3027                 :                :  * symmetric test.  Since the pg_cast entries aren't necessarily symmetric,
                               3028                 :                :  * the order of the operands is now significant.
                               3029                 :                :  */
                               3030                 :                : bool
 8440                          3031                 :        1244034 : IsBinaryCoercible(Oid srctype, Oid targettype)
                               3032                 :                : {
                               3033                 :                :     Oid         castoid;
                               3034                 :                : 
 1106                          3035                 :        1244034 :     return IsBinaryCoercibleWithCast(srctype, targettype, &castoid);
                               3036                 :                : }
                               3037                 :                : 
                               3038                 :                : /* IsBinaryCoercibleWithCast()
                               3039                 :                :  *      Check if srctype is binary-coercible to targettype.
                               3040                 :                :  *
                               3041                 :                :  * This variant also returns the OID of the pg_cast entry if one is involved.
                               3042                 :                :  * *castoid is set to InvalidOid if no binary-coercible cast exists, or if
                               3043                 :                :  * there is a hard-wired rule for it rather than a pg_cast entry.
                               3044                 :                :  */
                               3045                 :                : bool
                               3046                 :        1244136 : IsBinaryCoercibleWithCast(Oid srctype, Oid targettype,
                               3047                 :                :                           Oid *castoid)
                               3048                 :                : {
                               3049                 :                :     HeapTuple   tuple;
                               3050                 :                :     Form_pg_cast castForm;
                               3051                 :                :     bool        result;
                               3052                 :                : 
                               3053                 :        1244136 :     *castoid = InvalidOid;
                               3054                 :                : 
                               3055                 :                :     /* Fast path if same type */
 8440                          3056         [ +  + ]:        1244136 :     if (srctype == targettype)
 8457                          3057                 :         248604 :         return true;
                               3058                 :                : 
                               3059                 :                :     /* Anything is coercible to ANY or ANYELEMENT or ANYCOMPATIBLE */
 2048                          3060   [ +  +  +  +  :         995532 :     if (targettype == ANYOID || targettype == ANYELEMENTOID ||
                                              -  + ]
                               3061                 :                :         targettype == ANYCOMPATIBLEOID)
 4326                          3062                 :             94 :         return true;
                               3063                 :                : 
                               3064                 :                :     /* If srctype is a domain, reduce to its base type */
 8440                          3065         [ +  - ]:         995438 :     if (OidIsValid(srctype))
                               3066                 :         995438 :         srctype = getBaseType(srctype);
                               3067                 :                : 
                               3068                 :                :     /* Somewhat-fast path for domain -> base type case */
                               3069         [ +  + ]:         995438 :     if (srctype == targettype)
 8457                          3070                 :              6 :         return true;
                               3071                 :                : 
                               3072                 :                :     /* Also accept any array type as coercible to ANY[COMPATIBLE]ARRAY */
 2048                          3073   [ +  +  -  + ]:         995432 :     if (targettype == ANYARRAYOID || targettype == ANYCOMPATIBLEARRAYOID)
 5090                          3074         [ +  + ]:          52901 :         if (type_is_array(srctype))
 6718                          3075                 :           2479 :             return true;
                               3076                 :                : 
                               3077                 :                :     /* Also accept any non-array type as coercible to ANY[COMPATIBLE]NONARRAY */
 2048                          3078   [ +  -  -  + ]:         992953 :     if (targettype == ANYNONARRAYOID || targettype == ANYCOMPATIBLENONARRAYOID)
 5090 tgl@sss.pgh.pa.us        3079         [ #  # ]:UBC           0 :         if (!type_is_array(srctype))
 8158                          3080                 :              0 :             return true;
                               3081                 :                : 
                               3082                 :                :     /* Also accept any enum type as coercible to ANYENUM */
 6783 tgl@sss.pgh.pa.us        3083         [ +  + ]:CBC      992953 :     if (targettype == ANYENUMOID)
                               3084         [ +  + ]:          48911 :         if (type_is_enum(srctype))
                               3085                 :            111 :             return true;
                               3086                 :                : 
                               3087                 :                :     /* Also accept any range type as coercible to ANY[COMPATIBLE]RANGE */
 2048                          3088   [ +  +  -  + ]:         992842 :     if (targettype == ANYRANGEOID || targettype == ANYCOMPATIBLERANGEOID)
 5107 heikki.linnakangas@i     3089         [ +  + ]:          12646 :         if (type_is_range(srctype))
                               3090                 :           1691 :             return true;
                               3091                 :                : 
                               3092                 :                :     /* Also, any multirange type is coercible to ANY[COMPATIBLE]MULTIRANGE */
 1772 akorotkov@postgresql     3093   [ +  +  -  + ]:         991151 :     if (targettype == ANYMULTIRANGEOID || targettype == ANYCOMPATIBLEMULTIRANGEOID)
                               3094         [ +  + ]:          28325 :         if (type_is_multirange(srctype))
                               3095                 :            192 :             return true;
                               3096                 :                : 
                               3097                 :                :     /* Also accept any composite type as coercible to RECORD */
 6223 tgl@sss.pgh.pa.us        3098         [ +  + ]:         990959 :     if (targettype == RECORDOID)
                               3099         [ +  + ]:          10770 :         if (ISCOMPLEX(srctype))
                               3100                 :            441 :             return true;
                               3101                 :                : 
                               3102                 :                :     /* Also accept any composite array type as coercible to RECORD[] */
                               3103         [ -  + ]:         990518 :     if (targettype == RECORDARRAYOID)
 6223 tgl@sss.pgh.pa.us        3104         [ #  # ]:UBC           0 :         if (is_complex_array(srctype))
                               3105                 :              0 :             return true;
                               3106                 :                : 
                               3107                 :                :     /* Else look in pg_cast */
 5734 rhaas@postgresql.org     3108                 :CBC      990518 :     tuple = SearchSysCache2(CASTSOURCETARGET,
                               3109                 :                :                             ObjectIdGetDatum(srctype),
                               3110                 :                :                             ObjectIdGetDatum(targettype));
 8457 tgl@sss.pgh.pa.us        3111         [ +  + ]:         990518 :     if (!HeapTupleIsValid(tuple))
                               3112                 :         798867 :         return false;           /* no cast */
                               3113                 :         191651 :     castForm = (Form_pg_cast) GETSTRUCT(tuple);
                               3114                 :                : 
 5716 heikki.linnakangas@i     3115         [ +  + ]:         211097 :     result = (castForm->castmethod == COERCION_METHOD_BINARY &&
 8440 tgl@sss.pgh.pa.us        3116         [ +  + ]:          19446 :               castForm->castcontext == COERCION_CODE_IMPLICIT);
                               3117                 :                : 
 1106                          3118         [ +  + ]:         191651 :     if (result)
                               3119                 :           7555 :         *castoid = castForm->oid;
                               3120                 :                : 
 8457                          3121                 :         191651 :     ReleaseSysCache(tuple);
                               3122                 :                : 
                               3123                 :         191651 :     return result;
                               3124                 :                : }
                               3125                 :                : 
                               3126                 :                : 
                               3127                 :                : /*
                               3128                 :                :  * find_coercion_pathway
                               3129                 :                :  *      Look for a coercion pathway between two types.
                               3130                 :                :  *
                               3131                 :                :  * Currently, this deals only with scalar-type cases; it does not consider
                               3132                 :                :  * polymorphic types nor casts between composite types.  (Perhaps fold
                               3133                 :                :  * those in someday?)
                               3134                 :                :  *
                               3135                 :                :  * ccontext determines the set of available casts.
                               3136                 :                :  *
                               3137                 :                :  * The possible result codes are:
                               3138                 :                :  *  COERCION_PATH_NONE: failed to find any coercion pathway
                               3139                 :                :  *              *funcid is set to InvalidOid
                               3140                 :                :  *  COERCION_PATH_FUNC: apply the coercion function returned in *funcid
                               3141                 :                :  *  COERCION_PATH_RELABELTYPE: binary-compatible cast, no function needed
                               3142                 :                :  *              *funcid is set to InvalidOid
                               3143                 :                :  *  COERCION_PATH_ARRAYCOERCE: need an ArrayCoerceExpr node
                               3144                 :                :  *              *funcid is set to InvalidOid
                               3145                 :                :  *  COERCION_PATH_COERCEVIAIO: need a CoerceViaIO node
                               3146                 :                :  *              *funcid is set to InvalidOid
                               3147                 :                :  *
                               3148                 :                :  * Note: COERCION_PATH_RELABELTYPE does not necessarily mean that no work is
                               3149                 :                :  * needed to do the coercion; if the target is a domain then we may need to
                               3150                 :                :  * apply domain constraint checking.  If you want to check for a zero-effort
                               3151                 :                :  * conversion then use IsBinaryCoercible().
                               3152                 :                :  */
                               3153                 :                : CoercionPathType
 8440                          3154                 :         686166 : find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId,
                               3155                 :                :                       CoercionContext ccontext,
                               3156                 :                :                       Oid *funcid)
                               3157                 :                : {
 6719                          3158                 :         686166 :     CoercionPathType result = COERCION_PATH_NONE;
                               3159                 :                :     HeapTuple   tuple;
                               3160                 :                : 
 8457                          3161                 :         686166 :     *funcid = InvalidOid;
                               3162                 :                : 
                               3163                 :                :     /* Perhaps the types are domains; if so, look at their base types */
                               3164         [ +  - ]:         686166 :     if (OidIsValid(sourceTypeId))
                               3165                 :         686166 :         sourceTypeId = getBaseType(sourceTypeId);
                               3166         [ +  - ]:         686166 :     if (OidIsValid(targetTypeId))
                               3167                 :         686166 :         targetTypeId = getBaseType(targetTypeId);
                               3168                 :                : 
                               3169                 :                :     /* Domains are always coercible to and from their base type */
                               3170         [ +  + ]:         686166 :     if (sourceTypeId == targetTypeId)
 6719                          3171                 :          46049 :         return COERCION_PATH_RELABELTYPE;
                               3172                 :                : 
                               3173                 :                :     /* Look in pg_cast */
 5734 rhaas@postgresql.org     3174                 :         640117 :     tuple = SearchSysCache2(CASTSOURCETARGET,
                               3175                 :                :                             ObjectIdGetDatum(sourceTypeId),
                               3176                 :                :                             ObjectIdGetDatum(targetTypeId));
                               3177                 :                : 
 8502 peter_e@gmx.net          3178         [ +  + ]:         640117 :     if (HeapTupleIsValid(tuple))
                               3179                 :                :     {
 8457 tgl@sss.pgh.pa.us        3180                 :         250545 :         Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
                               3181                 :                :         CoercionContext castcontext;
                               3182                 :                : 
                               3183                 :                :         /* convert char value for castcontext to CoercionContext enum */
 8440                          3184   [ +  +  +  - ]:         250545 :         switch (castForm->castcontext)
                               3185                 :                :         {
                               3186                 :         199342 :             case COERCION_CODE_IMPLICIT:
                               3187                 :         199342 :                 castcontext = COERCION_IMPLICIT;
                               3188                 :         199342 :                 break;
                               3189                 :          46372 :             case COERCION_CODE_ASSIGNMENT:
                               3190                 :          46372 :                 castcontext = COERCION_ASSIGNMENT;
                               3191                 :          46372 :                 break;
                               3192                 :           4831 :             case COERCION_CODE_EXPLICIT:
                               3193                 :           4831 :                 castcontext = COERCION_EXPLICIT;
                               3194                 :           4831 :                 break;
 8440 tgl@sss.pgh.pa.us        3195                 :UBC           0 :             default:
 8136                          3196         [ #  # ]:              0 :                 elog(ERROR, "unrecognized castcontext: %d",
                               3197                 :                :                      (int) castForm->castcontext);
                               3198                 :                :                 castcontext = 0;    /* keep compiler quiet */
                               3199                 :                :                 break;
                               3200                 :                :         }
                               3201                 :                : 
                               3202                 :                :         /* Rely on ordering of enum for correct behavior here */
 8440 tgl@sss.pgh.pa.us        3203         [ +  + ]:CBC      250545 :         if (ccontext >= castcontext)
                               3204                 :                :         {
 6205 heikki.linnakangas@i     3205   [ +  +  +  - ]:         210129 :             switch (castForm->castmethod)
                               3206                 :                :             {
                               3207                 :          76662 :                 case COERCION_METHOD_FUNCTION:
                               3208                 :          76662 :                     result = COERCION_PATH_FUNC;
                               3209                 :          76662 :                     *funcid = castForm->castfunc;
                               3210                 :          76662 :                     break;
                               3211                 :            344 :                 case COERCION_METHOD_INOUT:
                               3212                 :            344 :                     result = COERCION_PATH_COERCEVIAIO;
                               3213                 :            344 :                     break;
                               3214                 :         133123 :                 case COERCION_METHOD_BINARY:
                               3215                 :         133123 :                     result = COERCION_PATH_RELABELTYPE;
                               3216                 :         133123 :                     break;
 6205 heikki.linnakangas@i     3217                 :UBC           0 :                 default:
                               3218         [ #  # ]:              0 :                     elog(ERROR, "unrecognized castmethod: %d",
                               3219                 :                :                          (int) castForm->castmethod);
                               3220                 :                :                     break;
                               3221                 :                :             }
                               3222                 :                :         }
                               3223                 :                : 
 8502 peter_e@gmx.net          3224                 :CBC      250545 :         ReleaseSysCache(tuple);
                               3225                 :                :     }
                               3226                 :                :     else
                               3227                 :                :     {
                               3228                 :                :         /*
                               3229                 :                :          * If there's no pg_cast entry, perhaps we are dealing with a pair of
                               3230                 :                :          * array types.  If so, and if their element types have a conversion
                               3231                 :                :          * pathway, report that we can coerce with an ArrayCoerceExpr.
                               3232                 :                :          *
                               3233                 :                :          * Hack: disallow coercions to oidvector and int2vector, which
                               3234                 :                :          * otherwise tend to capture coercions that should go to "real" array
                               3235                 :                :          * types.  We want those types to be considered "real" arrays for many
                               3236                 :                :          * purposes, but not this one.  (Also, ArrayCoerceExpr isn't
                               3237                 :                :          * guaranteed to produce an output that meets the restrictions of
                               3238                 :                :          * these datatypes, such as being 1-dimensional.)
                               3239                 :                :          */
 6719 tgl@sss.pgh.pa.us        3240   [ +  +  +  - ]:         389572 :         if (targetTypeId != OIDVECTOROID && targetTypeId != INT2VECTOROID)
                               3241                 :                :         {
                               3242                 :                :             Oid         targetElem;
                               3243                 :                :             Oid         sourceElem;
                               3244                 :                : 
                               3245   [ +  +  +  + ]:         389229 :             if ((targetElem = get_element_type(targetTypeId)) != InvalidOid &&
 2949                          3246                 :           5715 :                 (sourceElem = get_element_type(sourceTypeId)) != InvalidOid)
                               3247                 :                :             {
                               3248                 :                :                 CoercionPathType elempathtype;
                               3249                 :                :                 Oid         elemfuncid;
                               3250                 :                : 
 6719                          3251                 :           5122 :                 elempathtype = find_coercion_pathway(targetElem,
                               3252                 :                :                                                      sourceElem,
                               3253                 :                :                                                      ccontext,
                               3254                 :                :                                                      &elemfuncid);
 2949                          3255         [ +  + ]:           5122 :                 if (elempathtype != COERCION_PATH_NONE)
                               3256                 :                :                 {
                               3257                 :           5040 :                     result = COERCION_PATH_ARRAYCOERCE;
                               3258                 :                :                 }
                               3259                 :                :             }
                               3260                 :                :         }
                               3261                 :                : 
                               3262                 :                :         /*
                               3263                 :                :          * If we still haven't found a possibility, consider automatic casting
                               3264                 :                :          * using I/O functions.  We allow assignment casts to string types and
                               3265                 :                :          * explicit casts from string types to be handled this way. (The
                               3266                 :                :          * CoerceViaIO mechanism is a lot more general than that, but this is
                               3267                 :                :          * all we want to allow in the absence of a pg_cast entry.) It would
                               3268                 :                :          * probably be better to insist on explicit casts in both directions,
                               3269                 :                :          * but this is a compromise to preserve something of the pre-8.3
                               3270                 :                :          * behavior that many types had implicit (yipes!) casts to text.
                               3271                 :                :          */
 6719                          3272         [ +  + ]:         389572 :         if (result == COERCION_PATH_NONE)
                               3273                 :                :         {
                               3274   [ +  +  +  + ]:         409502 :             if (ccontext >= COERCION_ASSIGNMENT &&
 6298                          3275                 :          24970 :                 TypeCategory(targetTypeId) == TYPCATEGORY_STRING)
 6719                          3276                 :          19981 :                 result = COERCION_PATH_COERCEVIAIO;
                               3277   [ +  +  +  + ]:         369040 :             else if (ccontext >= COERCION_EXPLICIT &&
 6298                          3278                 :           4489 :                      TypeCategory(sourceTypeId) == TYPCATEGORY_STRING)
 6719                          3279                 :           2926 :                 result = COERCION_PATH_COERCEVIAIO;
                               3280                 :                :         }
                               3281                 :                :     }
                               3282                 :                : 
                               3283                 :                :     /*
                               3284                 :                :      * When parsing PL/pgSQL assignments, allow an I/O cast to be used
                               3285                 :                :      * whenever no normal coercion is available.
                               3286                 :                :      */
 1757                          3287   [ +  +  +  + ]:         640117 :     if (result == COERCION_PATH_NONE &&
                               3288                 :                :         ccontext == COERCION_PLPGSQL)
                               3289                 :            102 :         result = COERCION_PATH_COERCEVIAIO;
                               3290                 :                : 
 8457                          3291                 :         640117 :     return result;
                               3292                 :                : }
                               3293                 :                : 
                               3294                 :                : 
                               3295                 :                : /*
                               3296                 :                :  * find_typmod_coercion_function -- does the given type need length coercion?
                               3297                 :                :  *
                               3298                 :                :  * If the target type possesses a pg_cast function from itself to itself,
                               3299                 :                :  * it must need length coercion.
                               3300                 :                :  *
                               3301                 :                :  * "bpchar" (ie, char(N)) and "numeric" are examples of such types.
                               3302                 :                :  *
                               3303                 :                :  * If the given type is a varlena array type, we do not look for a coercion
                               3304                 :                :  * function associated directly with the array type, but instead look for
                               3305                 :                :  * one associated with the element type.  An ArrayCoerceExpr node must be
                               3306                 :                :  * used to apply such a function.  (Note: currently, it's pointless to
                               3307                 :                :  * return the funcid in this case, because it'll just get looked up again
                               3308                 :                :  * in the recursive construction of the ArrayCoerceExpr's elemexpr.)
                               3309                 :                :  *
                               3310                 :                :  * We use the same result enum as find_coercion_pathway, but the only possible
                               3311                 :                :  * result codes are:
                               3312                 :                :  *  COERCION_PATH_NONE: no length coercion needed
                               3313                 :                :  *  COERCION_PATH_FUNC: apply the function returned in *funcid
                               3314                 :                :  *  COERCION_PATH_ARRAYCOERCE: apply the function using ArrayCoerceExpr
                               3315                 :                :  */
                               3316                 :                : CoercionPathType
 6789                          3317                 :           9478 : find_typmod_coercion_function(Oid typeId,
                               3318                 :                :                               Oid *funcid)
                               3319                 :                : {
                               3320                 :                :     CoercionPathType result;
                               3321                 :                :     Type        targetType;
                               3322                 :                :     Form_pg_type typeForm;
                               3323                 :                :     HeapTuple   tuple;
                               3324                 :                : 
                               3325                 :           9478 :     *funcid = InvalidOid;
 6719                          3326                 :           9478 :     result = COERCION_PATH_FUNC;
                               3327                 :                : 
 8502 peter_e@gmx.net          3328                 :           9478 :     targetType = typeidType(typeId);
 8440 tgl@sss.pgh.pa.us        3329                 :           9478 :     typeForm = (Form_pg_type) GETSTRUCT(targetType);
                               3330                 :                : 
                               3331                 :                :     /* Check for a "true" array type */
 1783                          3332   [ +  +  +  - ]:           9478 :     if (IsTrueArrayType(typeForm))
                               3333                 :                :     {
                               3334                 :                :         /* Yes, switch our attention to the element type */
 8440                          3335                 :             39 :         typeId = typeForm->typelem;
 6719                          3336                 :             39 :         result = COERCION_PATH_ARRAYCOERCE;
                               3337                 :                :     }
 7803                          3338                 :           9478 :     ReleaseSysCache(targetType);
                               3339                 :                : 
                               3340                 :                :     /* Look in pg_cast */
 5734 rhaas@postgresql.org     3341                 :           9478 :     tuple = SearchSysCache2(CASTSOURCETARGET,
                               3342                 :                :                             ObjectIdGetDatum(typeId),
                               3343                 :                :                             ObjectIdGetDatum(typeId));
                               3344                 :                : 
 7803 tgl@sss.pgh.pa.us        3345         [ +  + ]:           9478 :     if (HeapTupleIsValid(tuple))
                               3346                 :                :     {
                               3347                 :           9472 :         Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
                               3348                 :                : 
 6789                          3349                 :           9472 :         *funcid = castForm->castfunc;
 7803                          3350                 :           9472 :         ReleaseSysCache(tuple);
                               3351                 :                :     }
                               3352                 :                : 
 6719                          3353         [ +  + ]:           9478 :     if (!OidIsValid(*funcid))
                               3354                 :              6 :         result = COERCION_PATH_NONE;
                               3355                 :                : 
                               3356                 :           9478 :     return result;
                               3357                 :                : }
                               3358                 :                : 
                               3359                 :                : /*
                               3360                 :                :  * is_complex_array
                               3361                 :                :  *      Is this type an array of composite?
                               3362                 :                :  *
                               3363                 :                :  * Note: this will not return true for record[]; check for RECORDARRAYOID
                               3364                 :                :  * separately if needed.
                               3365                 :                :  */
                               3366                 :                : static bool
 6223                          3367                 :             14 : is_complex_array(Oid typid)
                               3368                 :                : {
                               3369                 :             14 :     Oid         elemtype = get_element_type(typid);
                               3370                 :                : 
                               3371   [ +  +  +  - ]:             14 :     return (OidIsValid(elemtype) && ISCOMPLEX(elemtype));
                               3372                 :                : }
                               3373                 :                : 
                               3374                 :                : 
                               3375                 :                : /*
                               3376                 :                :  * Check whether reltypeId is the row type of a typed table of type
                               3377                 :                :  * reloftypeId, or is a domain over such a row type.  (This is conceptually
                               3378                 :                :  * similar to the subtype relationship checked by typeInheritsFrom().)
                               3379                 :                :  */
                               3380                 :                : static bool
 5413 peter_e@gmx.net          3381                 :         398704 : typeIsOfTypedTable(Oid reltypeId, Oid reloftypeId)
                               3382                 :                : {
 2923 tgl@sss.pgh.pa.us        3383                 :         398704 :     Oid         relid = typeOrDomainTypeRelid(reltypeId);
 5314 bruce@momjian.us         3384                 :         398704 :     bool        result = false;
                               3385                 :                : 
 5413 peter_e@gmx.net          3386         [ +  + ]:         398704 :     if (relid)
                               3387                 :                :     {
                               3388                 :                :         HeapTuple   tp;
                               3389                 :                :         Form_pg_class reltup;
                               3390                 :                : 
                               3391                 :           6277 :         tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
                               3392         [ -  + ]:           6277 :         if (!HeapTupleIsValid(tp))
 5413 peter_e@gmx.net          3393         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for relation %u", relid);
                               3394                 :                : 
 5413 peter_e@gmx.net          3395                 :CBC        6277 :         reltup = (Form_pg_class) GETSTRUCT(tp);
                               3396         [ +  + ]:           6277 :         if (reltup->reloftype == reloftypeId)
                               3397                 :              6 :             result = true;
                               3398                 :                : 
                               3399                 :           6277 :         ReleaseSysCache(tp);
                               3400                 :                :     }
                               3401                 :                : 
                               3402                 :         398704 :     return result;
                               3403                 :                : }
        

Generated by: LCOV version 2.4-beta