LCOV - differential code coverage report
Current view: top level - src/backend/parser - parse_oper.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 93.2 % 293 273 1 19 1 18 254 1 4
Current Date: 2026-05-05 10:23:31 +0900 Functions: 95.0 % 20 19 1 4 15 1
Baseline: lcov-20260505-025707-baseline Branches: 69.2 % 224 155 1 68 6 7 142
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 94.7 % 19 18 1 18
(360..) days: 93.1 % 274 255 19 1 254
Function coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 94.4 % 18 17 1 2 15
Branch coverage date bins:
(30,360] days: 87.5 % 8 7 1 7
(360..) days: 68.5 % 216 148 68 6 142

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * parse_oper.c
                                  4                 :                :  *      handle operator things for parser
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/parser/parse_oper.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : 
                                 16                 :                : #include "postgres.h"
                                 17                 :                : 
                                 18                 :                : #include "access/htup_details.h"
                                 19                 :                : #include "catalog/pg_operator.h"
                                 20                 :                : #include "catalog/pg_type.h"
                                 21                 :                : #include "lib/stringinfo.h"
                                 22                 :                : #include "nodes/nodeFuncs.h"
                                 23                 :                : #include "parser/parse_coerce.h"
                                 24                 :                : #include "parser/parse_func.h"
                                 25                 :                : #include "parser/parse_oper.h"
                                 26                 :                : #include "parser/parse_type.h"
                                 27                 :                : #include "utils/builtins.h"
                                 28                 :                : #include "utils/hsearch.h"
                                 29                 :                : #include "utils/inval.h"
                                 30                 :                : #include "utils/lsyscache.h"
                                 31                 :                : #include "utils/syscache.h"
                                 32                 :                : #include "utils/typcache.h"
                                 33                 :                : 
                                 34                 :                : 
                                 35                 :                : /*
                                 36                 :                :  * The lookup key for the operator lookaside hash table.  Unused bits must be
                                 37                 :                :  * zeroes to ensure hashing works consistently --- in particular, oprname
                                 38                 :                :  * must be zero-padded and any unused entries in search_path must be zero.
                                 39                 :                :  *
                                 40                 :                :  * search_path contains the actual search_path with which the entry was
                                 41                 :                :  * derived (minus temp namespace if any), or else the single specified
                                 42                 :                :  * schema OID if we are looking up an explicitly-qualified operator name.
                                 43                 :                :  *
                                 44                 :                :  * search_path has to be fixed-length since the hashtable code insists on
                                 45                 :                :  * fixed-size keys.  If your search path is longer than that, we just punt
                                 46                 :                :  * and don't cache anything.
                                 47                 :                :  */
                                 48                 :                : 
                                 49                 :                : /* If your search_path is longer than this, sucks to be you ... */
                                 50                 :                : #define MAX_CACHED_PATH_LEN     16
                                 51                 :                : 
                                 52                 :                : typedef struct OprCacheKey
                                 53                 :                : {
                                 54                 :                :     char        oprname[NAMEDATALEN];
                                 55                 :                :     Oid         left_arg;       /* Left input OID, or 0 if prefix op */
                                 56                 :                :     Oid         right_arg;      /* Right input OID */
                                 57                 :                :     Oid         search_path[MAX_CACHED_PATH_LEN];
                                 58                 :                : } OprCacheKey;
                                 59                 :                : 
                                 60                 :                : typedef struct OprCacheEntry
                                 61                 :                : {
                                 62                 :                :     /* the hash lookup key MUST BE FIRST */
                                 63                 :                :     OprCacheKey key;
                                 64                 :                : 
                                 65                 :                :     Oid         opr_oid;        /* OID of the resolved operator */
                                 66                 :                : } OprCacheEntry;
                                 67                 :                : 
                                 68                 :                : 
                                 69                 :                : static Oid  binary_oper_exact(List *opname, Oid arg1, Oid arg2);
                                 70                 :                : static FuncDetailCode oper_select_candidate(int nargs,
                                 71                 :                :                                             Oid *input_typeids,
                                 72                 :                :                                             FuncCandidateList candidates,
                                 73                 :                :                                             Oid *operOid);
                                 74                 :                : static void op_error(ParseState *pstate, List *op,
                                 75                 :                :                      Oid arg1, Oid arg2,
                                 76                 :                :                      FuncDetailCode fdresult, int fgc_flags, int location);
                                 77                 :                : static int  oper_lookup_failure_details(int fgc_flags, bool is_unary_op);
                                 78                 :                : static bool make_oper_cache_key(ParseState *pstate, OprCacheKey *key,
                                 79                 :                :                                 List *opname, Oid ltypeId, Oid rtypeId,
                                 80                 :                :                                 int location);
                                 81                 :                : static Oid  find_oper_cache_entry(OprCacheKey *key);
                                 82                 :                : static void make_oper_cache_entry(OprCacheKey *key, Oid opr_oid);
                                 83                 :                : static void InvalidateOprCacheCallBack(Datum arg, SysCacheIdentifier cacheid,
                                 84                 :                :                                        uint32 hashvalue);
                                 85                 :                : 
                                 86                 :                : 
                                 87                 :                : /*
                                 88                 :                :  * LookupOperName
                                 89                 :                :  *      Given a possibly-qualified operator name and exact input datatypes,
                                 90                 :                :  *      look up the operator.
                                 91                 :                :  *
                                 92                 :                :  * Pass oprleft = InvalidOid for a prefix op.
                                 93                 :                :  *
                                 94                 :                :  * If the operator name is not schema-qualified, it is sought in the current
                                 95                 :                :  * namespace search path.
                                 96                 :                :  *
                                 97                 :                :  * If the operator is not found, we return InvalidOid if noError is true,
                                 98                 :                :  * else raise an error.  pstate and location are used only to report the
                                 99                 :                :  * error position; pass NULL/-1 if not available.
                                100                 :                :  */
                                101                 :                : Oid
 7357 tgl@sss.pgh.pa.us         102                 :CBC        3249 : LookupOperName(ParseState *pstate, List *opername, Oid oprleft, Oid oprright,
                                103                 :                :                bool noError, int location)
                                104                 :                : {
                                105                 :                :     Oid         result;
                                106                 :                : 
 7309                           107                 :           3249 :     result = OpernameGetOprid(opername, oprleft, oprright);
                                108         [ +  + ]:           3249 :     if (OidIsValid(result))
                                109                 :           2784 :         return result;
                                110                 :                : 
                                111                 :                :     /* we don't use op_error here because only an exact match is wanted */
 8341                           112         [ +  + ]:            465 :     if (!noError)
                                113                 :                :     {
  928                           114         [ +  + ]:             32 :         if (!OidIsValid(oprright))
 2056                           115         [ +  - ]:              8 :             ereport(ERROR,
                                116                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                117                 :                :                      errmsg("postfix operators are not supported"),
                                118                 :                :                      parser_errposition(pstate, location)));
                                119                 :                : 
 8341                           120         [ +  - ]:             24 :         ereport(ERROR,
                                121                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                122                 :                :                  errmsg("operator does not exist: %s",
                                123                 :                :                         op_signature_string(opername, oprleft, oprright)),
                                124                 :                :                  parser_errposition(pstate, location)));
                                125                 :                :     }
                                126                 :                : 
 8785                           127                 :            433 :     return InvalidOid;
                                128                 :                : }
                                129                 :                : 
                                130                 :                : /*
                                131                 :                :  * LookupOperWithArgs
                                132                 :                :  *      Like LookupOperName, but the argument types are specified by
                                133                 :                :  *      a ObjectWithArgs node.
                                134                 :                :  */
                                135                 :                : Oid
 3415 peter_e@gmx.net           136                 :           1269 : LookupOperWithArgs(ObjectWithArgs *oper, bool noError)
                                137                 :                : {
                                138                 :                :     TypeName   *oprleft,
                                139                 :                :                *oprright;
                                140                 :                :     Oid         leftoid,
                                141                 :                :                 rightoid;
                                142                 :                : 
                                143         [ -  + ]:           1269 :     Assert(list_length(oper->objargs) == 2);
 1790 tgl@sss.pgh.pa.us         144                 :           1269 :     oprleft = linitial_node(TypeName, oper->objargs);
                                145                 :           1269 :     oprright = lsecond_node(TypeName, oper->objargs);
                                146                 :                : 
 8785                           147         [ +  + ]:           1269 :     if (oprleft == NULL)
                                148                 :             22 :         leftoid = InvalidOid;
                                149                 :                :     else
 3415 peter_e@gmx.net           150                 :           1247 :         leftoid = LookupTypeNameOid(NULL, oprleft, noError);
                                151                 :                : 
 8785 tgl@sss.pgh.pa.us         152         [ +  + ]:           1265 :     if (oprright == NULL)
                                153                 :              8 :         rightoid = InvalidOid;
                                154                 :                :     else
 3415 peter_e@gmx.net           155                 :           1257 :         rightoid = LookupTypeNameOid(NULL, oprright, noError);
                                156                 :                : 
                                157                 :           1261 :     return LookupOperName(NULL, oper->objname, leftoid, rightoid,
                                158                 :                :                           noError, -1);
                                159                 :                : }
                                160                 :                : 
                                161                 :                : /*
                                162                 :                :  * get_sort_group_operators - get default sorting/grouping operators for type
                                163                 :                :  *
                                164                 :                :  * We fetch the "<", "=", and ">" operators all at once to reduce lookup
                                165                 :                :  * overhead (knowing that most callers will be interested in at least two).
                                166                 :                :  * However, a given datatype might have only an "=" operator, if it is
                                167                 :                :  * hashable but not sortable.  (Other combinations of present and missing
                                168                 :                :  * operators shouldn't happen, unless the system catalogs are messed up.)
                                169                 :                :  *
                                170                 :                :  * If an operator is missing and the corresponding needXX flag is true,
                                171                 :                :  * throw a standard error message, else return InvalidOid.
                                172                 :                :  *
                                173                 :                :  * In addition to the operator OIDs themselves, this function can identify
                                174                 :                :  * whether the "=" operator is hashable.
                                175                 :                :  *
                                176                 :                :  * Callers can pass NULL pointers for any results they don't care to get.
                                177                 :                :  *
                                178                 :                :  * Note: the results are guaranteed to be exact or binary-compatible matches,
                                179                 :                :  * since most callers are not prepared to cope with adding any run-time type
                                180                 :                :  * coercion steps.
                                181                 :                :  */
                                182                 :                : void
 6485 tgl@sss.pgh.pa.us         183                 :         180757 : get_sort_group_operators(Oid argtype,
                                184                 :                :                          bool needLT, bool needEQ, bool needGT,
                                185                 :                :                          Oid *ltOpr, Oid *eqOpr, Oid *gtOpr,
                                186                 :                :                          bool *isHashable)
                                187                 :                : {
                                188                 :                :     TypeCacheEntry *typentry;
                                189                 :                :     int         cache_flags;
                                190                 :                :     Oid         lt_opr;
                                191                 :                :     Oid         eq_opr;
                                192                 :                :     Oid         gt_opr;
                                193                 :                :     bool        hashable;
                                194                 :                : 
                                195                 :                :     /*
                                196                 :                :      * Look up the operators using the type cache.
                                197                 :                :      *
                                198                 :                :      * Note: the search algorithm used by typcache.c ensures that the results
                                199                 :                :      * are consistent, ie all from matching opclasses.
                                200                 :                :      */
 5666                           201         [ +  + ]:         180757 :     if (isHashable != NULL)
                                202                 :         106636 :         cache_flags = TYPECACHE_LT_OPR | TYPECACHE_EQ_OPR | TYPECACHE_GT_OPR |
                                203                 :                :             TYPECACHE_HASH_PROC;
                                204                 :                :     else
                                205                 :          74121 :         cache_flags = TYPECACHE_LT_OPR | TYPECACHE_EQ_OPR | TYPECACHE_GT_OPR;
                                206                 :                : 
                                207                 :         180757 :     typentry = lookup_type_cache(argtype, cache_flags);
 6485                           208                 :         180757 :     lt_opr = typentry->lt_opr;
                                209                 :         180757 :     eq_opr = typentry->eq_opr;
                                210                 :         180757 :     gt_opr = typentry->gt_opr;
 5666                           211                 :         180757 :     hashable = OidIsValid(typentry->hash_proc);
                                212                 :                : 
                                213                 :                :     /* Report errors if needed */
 6485                           214   [ +  +  +  +  :         180757 :     if ((needLT && !OidIsValid(lt_opr)) ||
                                              +  + ]
                                215         [ -  + ]:           2353 :         (needGT && !OidIsValid(gt_opr)))
 8297                           216         [ +  - ]:              4 :         ereport(ERROR,
                                217                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                218                 :                :                  errmsg("could not identify an ordering operator for type %s",
                                219                 :                :                         format_type_be(argtype)),
                                220                 :                :                  errhint("Use an explicit ordering operator or modify the query.")));
 6485                           221   [ +  +  -  + ]:         180753 :     if (needEQ && !OidIsValid(eq_opr))
 8327 tgl@sss.pgh.pa.us         222         [ #  # ]:UBC           0 :         ereport(ERROR,
                                223                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                224                 :                :                  errmsg("could not identify an equality operator for type %s",
                                225                 :                :                         format_type_be(argtype))));
                                226                 :                : 
                                227                 :                :     /* Return results as needed */
 6485 tgl@sss.pgh.pa.us         228         [ +  + ]:CBC      180753 :     if (ltOpr)
                                229                 :         178400 :         *ltOpr = lt_opr;
                                230         [ +  - ]:         180753 :     if (eqOpr)
                                231                 :         180753 :         *eqOpr = eq_opr;
                                232         [ +  + ]:         180753 :     if (gtOpr)
                                233                 :           2353 :         *gtOpr = gt_opr;
 5666                           234         [ +  + ]:         180753 :     if (isHashable)
                                235                 :         106632 :         *isHashable = hashable;
 8348                           236                 :         180753 : }
                                237                 :                : 
                                238                 :                : 
                                239                 :                : /* given operator tuple, return the operator OID */
                                240                 :                : Oid
10388 bruce@momjian.us          241                 :         473707 : oprid(Operator op)
                                242                 :                : {
 2723 andres@anarazel.de        243                 :         473707 :     return ((Form_pg_operator) GETSTRUCT(op))->oid;
                                244                 :                : }
                                245                 :                : 
                                246                 :                : /* given operator tuple, return the underlying function's OID */
                                247                 :                : Oid
 9209 tgl@sss.pgh.pa.us         248                 :UBC           0 : oprfuncid(Operator op)
                                249                 :                : {
 9175 bruce@momjian.us          250                 :              0 :     Form_pg_operator pgopform = (Form_pg_operator) GETSTRUCT(op);
                                251                 :                : 
 9209 tgl@sss.pgh.pa.us         252                 :              0 :     return pgopform->oprcode;
                                253                 :                : }
                                254                 :                : 
                                255                 :                : 
                                256                 :                : /* binary_oper_exact()
                                257                 :                :  * Check for an "exact" match to the specified operand types.
                                258                 :                :  *
                                259                 :                :  * If one operand is an unknown literal, assume it should be taken to be
                                260                 :                :  * the same type as the other operand for this purpose.  Also, consider
                                261                 :                :  * the possibility that the other operand is a domain type that needs to
                                262                 :                :  * be reduced to its base type to find an "exact" match.
                                263                 :                :  */
                                264                 :                : static Oid
 7309 tgl@sss.pgh.pa.us         265                 :CBC       52480 : binary_oper_exact(List *opname, Oid arg1, Oid arg2)
                                266                 :                : {
                                267                 :                :     Oid         result;
 8247                           268                 :          52480 :     bool        was_unknown = false;
                                269                 :                : 
                                270                 :                :     /* Unspecified type for one of the arguments? then use the other */
 8785                           271   [ +  +  +  - ]:          52480 :     if ((arg1 == UNKNOWNOID) && (arg2 != InvalidOid))
                                272                 :                :     {
                                273                 :           2390 :         arg1 = arg2;
 8247                           274                 :           2390 :         was_unknown = true;
                                275                 :                :     }
 8785                           276   [ +  +  +  - ]:          50090 :     else if ((arg2 == UNKNOWNOID) && (arg1 != InvalidOid))
                                277                 :                :     {
                                278                 :          13446 :         arg2 = arg1;
 8247                           279                 :          13446 :         was_unknown = true;
                                280                 :                :     }
                                281                 :                : 
 7309                           282                 :          52480 :     result = OpernameGetOprid(opname, arg1, arg2);
                                283         [ +  + ]:          52480 :     if (OidIsValid(result))
                                284                 :          40126 :         return result;
                                285                 :                : 
 8247                           286         [ +  + ]:          12354 :     if (was_unknown)
                                287                 :                :     {
                                288                 :                :         /* arg1 and arg2 are the same here, need only look at arg1 */
 7919 bruce@momjian.us          289                 :           3207 :         Oid         basetype = getBaseType(arg1);
                                290                 :                : 
 8247 tgl@sss.pgh.pa.us         291         [ +  + ]:           3207 :         if (basetype != arg1)
                                292                 :                :         {
 7309                           293                 :            213 :             result = OpernameGetOprid(opname, basetype, basetype);
                                294         [ +  + ]:            213 :             if (OidIsValid(result))
                                295                 :             47 :                 return result;
                                296                 :                :         }
                                297                 :                :     }
                                298                 :                : 
 8785                           299                 :          12307 :     return InvalidOid;
                                300                 :                : }
                                301                 :                : 
                                302                 :                : 
                                303                 :                : /* oper_select_candidate()
                                304                 :                :  *      Given the input argtype array and one or more candidates
                                305                 :                :  *      for the operator, attempt to resolve the conflict.
                                306                 :                :  *
                                307                 :                :  * Returns FUNCDETAIL_NOTFOUND, FUNCDETAIL_MULTIPLE, or FUNCDETAIL_NORMAL.
                                308                 :                :  * In the success case the Oid of the best candidate is stored in *operOid.
                                309                 :                :  *
                                310                 :                :  * Note that the caller has already determined that there is no candidate
                                311                 :                :  * exactly matching the input argtype(s).  Incompatible candidates are not yet
                                312                 :                :  * pruned away, however.
                                313                 :                :  */
                                314                 :                : static FuncDetailCode
10203 lockhart@fourpalms.o      315                 :          12330 : oper_select_candidate(int nargs,
                                316                 :                :                       Oid *input_typeids,
                                317                 :                :                       FuncCandidateList candidates,
                                318                 :                :                       Oid *operOid) /* output argument */
                                319                 :                : {
                                320                 :                :     int         ncandidates;
                                321                 :                : 
                                322                 :                :     /*
                                323                 :                :      * Delete any candidates that cannot actually accept the given input
                                324                 :                :      * types, whether directly or by coercion.
                                325                 :                :      */
 8380 tgl@sss.pgh.pa.us         326                 :          12330 :     ncandidates = func_match_argtypes(nargs, input_typeids,
                                327                 :                :                                       candidates, &candidates);
                                328                 :                : 
                                329                 :                :     /* Done if no candidate or only one candidate survives */
 9543                           330         [ +  + ]:          12330 :     if (ncandidates == 0)
                                331                 :                :     {
 8341                           332                 :             79 :         *operOid = InvalidOid;
                                333                 :             79 :         return FUNCDETAIL_NOTFOUND;
                                334                 :                :     }
 9543                           335         [ +  + ]:          12251 :     if (ncandidates == 1)
                                336                 :                :     {
 8341                           337                 :           8315 :         *operOid = candidates->oid;
                                338                 :           8315 :         return FUNCDETAIL_NORMAL;
                                339                 :                :     }
                                340                 :                : 
                                341                 :                :     /*
                                342                 :                :      * Use the same heuristics as for ambiguous functions to resolve the
                                343                 :                :      * conflict.
                                344                 :                :      */
 8380                           345                 :           3936 :     candidates = func_select_candidate(nargs, input_typeids, candidates);
                                346                 :                : 
                                347         [ +  + ]:           3936 :     if (candidates)
                                348                 :                :     {
 8341                           349                 :           3932 :         *operOid = candidates->oid;
                                350                 :           3932 :         return FUNCDETAIL_NORMAL;
                                351                 :                :     }
                                352                 :                : 
                                353                 :              4 :     *operOid = InvalidOid;
 8310 bruce@momjian.us          354                 :              4 :     return FUNCDETAIL_MULTIPLE; /* failed to select a best candidate */
                                355                 :                : }
                                356                 :                : 
                                357                 :                : 
                                358                 :                : /* oper() -- search for a binary operator
                                359                 :                :  * Given operator name, types of arg1 and arg2, return oper struct.
                                360                 :                :  *
                                361                 :                :  * IMPORTANT: the returned operator (if any) is only promised to be
                                362                 :                :  * coercion-compatible with the input datatypes.  Do not use this if
                                363                 :                :  * you need an exact- or binary-compatible match; see compatible_oper.
                                364                 :                :  *
                                365                 :                :  * If no matching operator found, return NULL if noError is true,
                                366                 :                :  * raise an error if it is false.  pstate and location are used only to report
                                367                 :                :  * the error position; pass NULL/-1 if not available.
                                368                 :                :  *
                                369                 :                :  * NOTE: on success, the returned object is a syscache entry.  The caller
                                370                 :                :  * must ReleaseSysCache() the entry when done with it.
                                371                 :                :  */
                                372                 :                : Operator
 7357 tgl@sss.pgh.pa.us         373                 :         472962 : oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId,
                                374                 :                :      bool noError, int location)
                                375                 :                : {
                                376                 :                :     Oid         operOid;
                                377                 :                :     OprCacheKey key;
                                378                 :                :     bool        key_ok;
  231 tgl@sss.pgh.pa.us         379                 :GNC      472962 :     int         fgc_flags = 0;
 8341 tgl@sss.pgh.pa.us         380                 :CBC      472962 :     FuncDetailCode fdresult = FUNCDETAIL_NOTFOUND;
 8785                           381                 :         472962 :     HeapTuple   tup = NULL;
                                382                 :                : 
                                383                 :                :     /*
                                384                 :                :      * Try to find the mapping in the lookaside cache.
                                385                 :                :      */
 4066 alvherre@alvh.no-ip.      386                 :         472962 :     key_ok = make_oper_cache_key(pstate, &key, opname, ltypeId, rtypeId, location);
                                387                 :                : 
 6733 tgl@sss.pgh.pa.us         388         [ +  - ]:         472962 :     if (key_ok)
                                389                 :                :     {
                                390                 :         472962 :         operOid = find_oper_cache_entry(&key);
                                391         [ +  + ]:         472962 :         if (OidIsValid(operOid))
                                392                 :                :         {
 5924 rhaas@postgresql.org      393                 :         420482 :             tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
 6733 tgl@sss.pgh.pa.us         394         [ +  - ]:         420482 :             if (HeapTupleIsValid(tup))
                                395                 :         420482 :                 return (Operator) tup;
                                396                 :                :         }
                                397                 :                :     }
                                398                 :                : 
                                399                 :                :     /*
                                400                 :                :      * First try for an "exact" match.
                                401                 :                :      */
 7309                           402                 :          52480 :     operOid = binary_oper_exact(opname, ltypeId, rtypeId);
                                403         [ +  + ]:          52480 :     if (!OidIsValid(operOid))
                                404                 :                :     {
                                405                 :                :         /*
                                406                 :                :          * Otherwise, search for the most suitable candidate.
                                407                 :                :          */
                                408                 :                :         FuncCandidateList clist;
                                409                 :                : 
                                410                 :                :         /* Get binary operators of given name */
  231 tgl@sss.pgh.pa.us         411                 :GNC       12307 :         clist = OpernameGetCandidates(opname, 'b', false, &fgc_flags);
                                412                 :                : 
                                413                 :                :         /* No operators found? Then fail... */
 7309 tgl@sss.pgh.pa.us         414         [ +  - ]:CBC       12307 :         if (clist != NULL)
                                415                 :                :         {
                                416                 :                :             /*
                                417                 :                :              * Unspecified type for one of the arguments? then use the other
                                418                 :                :              * (XXX this is probably dead code?)
                                419                 :                :              */
                                420                 :                :             Oid         inputOids[2];
                                421                 :                : 
 8785                           422         [ -  + ]:          12307 :             if (rtypeId == InvalidOid)
 8785 tgl@sss.pgh.pa.us         423                 :UBC           0 :                 rtypeId = ltypeId;
 8785 tgl@sss.pgh.pa.us         424         [ -  + ]:CBC       12307 :             else if (ltypeId == InvalidOid)
 8785 tgl@sss.pgh.pa.us         425                 :UBC           0 :                 ltypeId = rtypeId;
 8785 tgl@sss.pgh.pa.us         426                 :CBC       12307 :             inputOids[0] = ltypeId;
                                427                 :          12307 :             inputOids[1] = rtypeId;
 8341                           428                 :          12307 :             fdresult = oper_select_candidate(2, inputOids, clist, &operOid);
                                429                 :                :         }
                                430                 :                :     }
                                431                 :                : 
 7309                           432         [ +  + ]:          52480 :     if (OidIsValid(operOid))
 5924 rhaas@postgresql.org      433                 :          52401 :         tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
                                434                 :                : 
 6733 tgl@sss.pgh.pa.us         435         [ +  + ]:          52480 :     if (HeapTupleIsValid(tup))
                                436                 :                :     {
                                437         [ +  - ]:          52401 :         if (key_ok)
                                438                 :          52401 :             make_oper_cache_entry(&key, operOid);
                                439                 :                :     }
                                440         [ +  + ]:             79 :     else if (!noError)
  231 tgl@sss.pgh.pa.us         441                 :GNC          74 :         op_error(pstate, opname, ltypeId, rtypeId,
                                442                 :                :                  fdresult, fgc_flags, location);
                                443                 :                : 
 8785 tgl@sss.pgh.pa.us         444                 :CBC       52406 :     return (Operator) tup;
                                445                 :                : }
                                446                 :                : 
                                447                 :                : /* compatible_oper()
                                448                 :                :  *  given an opname and input datatypes, find a compatible binary operator
                                449                 :                :  *
                                450                 :                :  *  This is tighter than oper() because it will not return an operator that
                                451                 :                :  *  requires coercion of the input datatypes (but binary-compatible operators
                                452                 :                :  *  are accepted).  Otherwise, the semantics are the same.
                                453                 :                :  */
                                454                 :                : Operator
 7357                           455                 :            368 : compatible_oper(ParseState *pstate, List *op, Oid arg1, Oid arg2,
                                456                 :                :                 bool noError, int location)
                                457                 :                : {
                                458                 :                :     Operator    optup;
                                459                 :                :     Form_pg_operator opform;
                                460                 :                : 
                                461                 :                :     /* oper() will find the best available match */
                                462                 :            368 :     optup = oper(pstate, op, arg1, arg2, noError, location);
 9209                           463         [ -  + ]:            368 :     if (optup == (Operator) NULL)
 9175 bruce@momjian.us          464                 :UBC           0 :         return (Operator) NULL; /* must be noError case */
                                465                 :                : 
                                466                 :                :     /* but is it good enough? */
 9209 tgl@sss.pgh.pa.us         467                 :CBC         368 :     opform = (Form_pg_operator) GETSTRUCT(optup);
 8630                           468   [ +  -  +  - ]:            736 :     if (IsBinaryCoercible(arg1, opform->oprleft) &&
                                469                 :            368 :         IsBinaryCoercible(arg2, opform->oprright))
 9209                           470                 :            368 :         return optup;
                                471                 :                : 
                                472                 :                :     /* nope... */
 9143 tgl@sss.pgh.pa.us         473                 :UBC           0 :     ReleaseSysCache(optup);
                                474                 :                : 
 9209                           475         [ #  # ]:              0 :     if (!noError)
 8327                           476         [ #  # ]:              0 :         ereport(ERROR,
                                477                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                478                 :                :                  errmsg("operator requires run-time type coercion: %s",
                                479                 :                :                         op_signature_string(op, arg1, arg2)),
                                480                 :                :                  parser_errposition(pstate, location)));
                                481                 :                : 
 9209                           482                 :              0 :     return (Operator) NULL;
                                483                 :                : }
                                484                 :                : 
                                485                 :                : /* compatible_oper_opid() -- get OID of a binary operator
                                486                 :                :  *
                                487                 :                :  * This is a convenience routine that extracts only the operator OID
                                488                 :                :  * from the result of compatible_oper().  InvalidOid is returned if the
                                489                 :                :  * lookup fails and noError is true.
                                490                 :                :  */
                                491                 :                : Oid
 8785 tgl@sss.pgh.pa.us         492                 :CBC         368 : compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
                                493                 :                : {
                                494                 :                :     Operator    optup;
                                495                 :                :     Oid         result;
                                496                 :                : 
 7357                           497                 :            368 :     optup = compatible_oper(NULL, op, arg1, arg2, noError, -1);
 9301                           498         [ +  - ]:            368 :     if (optup != NULL)
                                499                 :                :     {
                                500                 :            368 :         result = oprid(optup);
                                501                 :            368 :         ReleaseSysCache(optup);
                                502                 :            368 :         return result;
                                503                 :                :     }
 9301 tgl@sss.pgh.pa.us         504                 :UBC           0 :     return InvalidOid;
                                505                 :                : }
                                506                 :                : 
                                507                 :                : 
                                508                 :                : /* left_oper() -- search for a unary left operator (prefix operator)
                                509                 :                :  * Given operator name and type of arg, return oper struct.
                                510                 :                :  *
                                511                 :                :  * IMPORTANT: the returned operator (if any) is only promised to be
                                512                 :                :  * coercion-compatible with the input datatype.  Do not use this if
                                513                 :                :  * you need an exact- or binary-compatible match.
                                514                 :                :  *
                                515                 :                :  * If no matching operator found, return NULL if noError is true,
                                516                 :                :  * raise an error if it is false.  pstate and location are used only to report
                                517                 :                :  * the error position; pass NULL/-1 if not available.
                                518                 :                :  *
                                519                 :                :  * NOTE: on success, the returned object is a syscache entry.  The caller
                                520                 :                :  * must ReleaseSysCache() the entry when done with it.
                                521                 :                :  */
                                522                 :                : Operator
 7357 tgl@sss.pgh.pa.us         523                 :CBC         844 : left_oper(ParseState *pstate, List *op, Oid arg, bool noError, int location)
                                524                 :                : {
                                525                 :                :     Oid         operOid;
                                526                 :                :     OprCacheKey key;
                                527                 :                :     bool        key_ok;
  231 tgl@sss.pgh.pa.us         528                 :GNC         844 :     int         fgc_flags = 0;
 8341 tgl@sss.pgh.pa.us         529                 :CBC         844 :     FuncDetailCode fdresult = FUNCDETAIL_NOTFOUND;
 8785                           530                 :            844 :     HeapTuple   tup = NULL;
                                531                 :                : 
                                532                 :                :     /*
                                533                 :                :      * Try to find the mapping in the lookaside cache.
                                534                 :                :      */
 4066 alvherre@alvh.no-ip.      535                 :            844 :     key_ok = make_oper_cache_key(pstate, &key, op, InvalidOid, arg, location);
                                536                 :                : 
 6733 tgl@sss.pgh.pa.us         537         [ +  - ]:            844 :     if (key_ok)
                                538                 :                :     {
                                539                 :            844 :         operOid = find_oper_cache_entry(&key);
                                540         [ +  + ]:            844 :         if (OidIsValid(operOid))
                                541                 :                :         {
 5924 rhaas@postgresql.org      542                 :            521 :             tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
 6733 tgl@sss.pgh.pa.us         543         [ +  - ]:            521 :             if (HeapTupleIsValid(tup))
                                544                 :            521 :                 return (Operator) tup;
                                545                 :                :         }
                                546                 :                :     }
                                547                 :                : 
                                548                 :                :     /*
                                549                 :                :      * First try for an "exact" match.
                                550                 :                :      */
 7309                           551                 :            323 :     operOid = OpernameGetOprid(op, InvalidOid, arg);
                                552         [ +  + ]:            323 :     if (!OidIsValid(operOid))
                                553                 :                :     {
                                554                 :                :         /*
                                555                 :                :          * Otherwise, search for the most suitable candidate.
                                556                 :                :          */
                                557                 :                :         FuncCandidateList clist;
                                558                 :                : 
                                559                 :                :         /* Get prefix operators of given name */
  231 tgl@sss.pgh.pa.us         560                 :GNC          31 :         clist = OpernameGetCandidates(op, 'l', false, &fgc_flags);
                                561                 :                : 
                                562                 :                :         /* No operators found? Then fail... */
 7309 tgl@sss.pgh.pa.us         563         [ +  + ]:CBC          31 :         if (clist != NULL)
                                564                 :                :         {
                                565                 :                :             /*
                                566                 :                :              * The returned list has args in the form (0, oprright). Move the
                                567                 :                :              * useful data into args[0] to keep oper_select_candidate simple.
                                568                 :                :              * XXX we are assuming here that we may scribble on the list!
                                569                 :                :              */
                                570                 :                :             FuncCandidateList clisti;
                                571                 :                : 
                                572         [ +  + ]:             75 :             for (clisti = clist; clisti != NULL; clisti = clisti->next)
                                573                 :                :             {
                                574                 :             52 :                 clisti->args[0] = clisti->args[1];
                                575                 :                :             }
                                576                 :                : 
                                577                 :                :             /*
                                578                 :                :              * We must run oper_select_candidate even if only one candidate,
                                579                 :                :              * otherwise we may falsely return a non-type-compatible operator.
                                580                 :                :              */
 8341                           581                 :             23 :             fdresult = oper_select_candidate(1, &arg, clist, &operOid);
                                582                 :                :         }
                                583                 :                :     }
                                584                 :                : 
 7309                           585         [ +  + ]:            323 :     if (OidIsValid(operOid))
 5924 rhaas@postgresql.org      586                 :            311 :         tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
                                587                 :                : 
 6733 tgl@sss.pgh.pa.us         588         [ +  + ]:            323 :     if (HeapTupleIsValid(tup))
                                589                 :                :     {
                                590         [ +  - ]:            311 :         if (key_ok)
                                591                 :            311 :             make_oper_cache_entry(&key, operOid);
                                592                 :                :     }
 6733 tgl@sss.pgh.pa.us         593         [ +  - ]:GBC          12 :     else if (!noError)
  231 tgl@sss.pgh.pa.us         594                 :GNC          12 :         op_error(pstate, op, InvalidOid, arg,
                                595                 :                :                  fdresult, fgc_flags, location);
                                596                 :                : 
10108 bruce@momjian.us          597                 :CBC         311 :     return (Operator) tup;
                                598                 :                : }
                                599                 :                : 
                                600                 :                : /*
                                601                 :                :  * op_signature_string
                                602                 :                :  *      Build a string representing an operator name, including arg type(s).
                                603                 :                :  *      The result is something like "integer + integer".
                                604                 :                :  *
                                605                 :                :  * This is typically used in the construction of operator-not-found error
                                606                 :                :  * messages.
                                607                 :                :  */
                                608                 :                : const char *
  928 tgl@sss.pgh.pa.us         609                 :            110 : op_signature_string(List *op, Oid arg1, Oid arg2)
                                610                 :                : {
                                611                 :                :     StringInfoData argbuf;
                                612                 :                : 
 8341                           613                 :            110 :     initStringInfo(&argbuf);
                                614                 :                : 
  928                           615         [ +  + ]:            110 :     if (OidIsValid(arg1))
 8341                           616                 :             90 :         appendStringInfo(&argbuf, "%s ", format_type_be(arg1));
                                617                 :                : 
                                618                 :            110 :     appendStringInfoString(&argbuf, NameListToString(op));
                                619                 :                : 
 2056                           620                 :            110 :     appendStringInfo(&argbuf, " %s", format_type_be(arg2));
                                621                 :                : 
 8341                           622                 :            110 :     return argbuf.data;         /* return palloc'd string buffer */
                                623                 :                : }
                                624                 :                : 
                                625                 :                : /*
                                626                 :                :  * op_error - utility routine to complain about an unresolvable operator
                                627                 :                :  */
                                628                 :                : static void
  928                           629                 :             86 : op_error(ParseState *pstate, List *op,
                                630                 :                :          Oid arg1, Oid arg2,
                                631                 :                :          FuncDetailCode fdresult, int fgc_flags, int location)
                                632                 :                : {
 8341                           633         [ +  + ]:             86 :     if (fdresult == FUNCDETAIL_MULTIPLE)
                                634         [ +  - ]:              4 :         ereport(ERROR,
                                635                 :                :                 (errcode(ERRCODE_AMBIGUOUS_FUNCTION),
                                636                 :                :                  errmsg("operator is not unique: %s",
                                637                 :                :                         op_signature_string(op, arg1, arg2)),
                                638                 :                :                  errdetail("Could not choose a best candidate operator."),
                                639                 :                :                  errhint("You might need to add explicit type casts."),
                                640                 :                :                  parser_errposition(pstate, location)));
                                641                 :                :     else
                                642   [ +  -  +  +  :             82 :         ereport(ERROR,
                                              -  + ]
                                643                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                644                 :                :                  errmsg("operator does not exist: %s",
                                645                 :                :                         op_signature_string(op, arg1, arg2)),
                                646                 :                :                  oper_lookup_failure_details(fgc_flags, (!arg1 || !arg2)),
                                647                 :                :                  parser_errposition(pstate, location)));
                                648                 :                : }
                                649                 :                : 
                                650                 :                : /*
                                651                 :                :  * Interpret the fgc_flags and issue a suitable detail or hint message.
                                652                 :                :  */
                                653                 :                : static int
  231 tgl@sss.pgh.pa.us         654                 :GNC          82 : oper_lookup_failure_details(int fgc_flags, bool is_unary_op)
                                655                 :                : {
                                656                 :                :     /*
                                657                 :                :      * If not FGC_NAME_VISIBLE, we shouldn't raise the question of whether the
                                658                 :                :      * arguments are wrong.  If the operator name was not schema-qualified,
                                659                 :                :      * it's helpful to distinguish between doesn't-exist-anywhere and
                                660                 :                :      * not-in-search-path; but if it was, there's really nothing to add to the
                                661                 :                :      * basic "operator does not exist" message.
                                662                 :                :      *
                                663                 :                :      * Note: we passed missing_ok = false to OpernameGetCandidates, so there's
                                664                 :                :      * no need to consider FGC_SCHEMA_EXISTS here: we'd have already thrown an
                                665                 :                :      * error if an explicitly-given schema doesn't exist.
                                666                 :                :      */
                                667         [ +  + ]:             82 :     if (!(fgc_flags & FGC_NAME_VISIBLE))
                                668                 :                :     {
                                669         [ -  + ]:              8 :         if (fgc_flags & FGC_SCHEMA_GIVEN)
  231 tgl@sss.pgh.pa.us         670                 :UNC           0 :             return 0;           /* schema-qualified name */
  231 tgl@sss.pgh.pa.us         671         [ +  + ]:GNC           8 :         else if (!(fgc_flags & FGC_NAME_EXISTS))
                                672                 :              4 :             return errdetail("There is no operator of that name.");
                                673                 :                :         else
                                674                 :              4 :             return errdetail("An operator of that name exists, but it is not in the search_path.");
                                675                 :                :     }
                                676                 :                : 
                                677                 :                :     /*
                                678                 :                :      * Otherwise, the problem must be incorrect argument type(s).
                                679                 :                :      */
                                680         [ +  + ]:             74 :     if (is_unary_op)
                                681                 :                :     {
                                682                 :              4 :         (void) errdetail("No operator of that name accepts the given argument type.");
                                683                 :              4 :         return errhint("You might need to add an explicit type cast.");
                                684                 :                :     }
                                685                 :                :     else
                                686                 :                :     {
                                687                 :             70 :         (void) errdetail("No operator of that name accepts the given argument types.");
                                688                 :             70 :         return errhint("You might need to add explicit type casts.");
                                689                 :                :     }
                                690                 :                : }
                                691                 :                : 
                                692                 :                : /*
                                693                 :                :  * make_op()
                                694                 :                :  *      Operator expression construction.
                                695                 :                :  *
                                696                 :                :  * Transform operator expression ensuring type compatibility.
                                697                 :                :  * This is where some type conversion happens.
                                698                 :                :  *
                                699                 :                :  * last_srf should be a copy of pstate->p_last_srf from just before we
                                700                 :                :  * started transforming the operator's arguments; this is used for nested-SRF
                                701                 :                :  * detection.  If the caller will throw an error anyway for a set-returning
                                702                 :                :  * expression, it's okay to cheat and just pass pstate->p_last_srf.
                                703                 :                :  */
                                704                 :                : Expr *
 7357 tgl@sss.pgh.pa.us         705                 :CBC      407448 : make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
                                706                 :                :         Node *last_srf, int location)
                                707                 :                : {
                                708                 :                :     Oid         ltypeId,
                                709                 :                :                 rtypeId;
                                710                 :                :     Operator    tup;
                                711                 :                :     Form_pg_operator opform;
                                712                 :                :     Oid         actual_arg_types[2];
                                713                 :                :     Oid         declared_arg_types[2];
                                714                 :                :     int         nargs;
                                715                 :                :     List       *args;
                                716                 :                :     Oid         rettype;
                                717                 :                :     OpExpr     *result;
                                718                 :                : 
                                719                 :                :     /* Check it's not a postfix operator */
 8428                           720         [ -  + ]:         407448 :     if (rtree == NULL)
 2056 tgl@sss.pgh.pa.us         721         [ #  # ]:UBC           0 :         ereport(ERROR,
                                722                 :                :                 (errcode(ERRCODE_SYNTAX_ERROR),
                                723                 :                :                  errmsg("postfix operators are not supported")));
                                724                 :                : 
                                725                 :                :     /* Select the operator */
 2056 tgl@sss.pgh.pa.us         726         [ +  + ]:CBC      407448 :     if (ltree == NULL)
                                727                 :                :     {
                                728                 :                :         /* prefix operator */
 8428                           729                 :            824 :         rtypeId = exprType(rtree);
                                730                 :            824 :         ltypeId = InvalidOid;
 7357                           731                 :            824 :         tup = left_oper(pstate, opname, rtypeId, false, location);
                                732                 :                :     }
                                733                 :                :     else
                                734                 :                :     {
                                735                 :                :         /* otherwise, binary operator */
 8428                           736                 :         406624 :         ltypeId = exprType(ltree);
                                737                 :         406624 :         rtypeId = exprType(rtree);
 7357                           738                 :         406624 :         tup = oper(pstate, opname, ltypeId, rtypeId, false, location);
                                739                 :                :     }
                                740                 :                : 
 6587                           741                 :         407362 :     opform = (Form_pg_operator) GETSTRUCT(tup);
                                742                 :                : 
                                743                 :                :     /* Check it's not a shell */
                                744         [ -  + ]:         407362 :     if (!RegProcedureIsValid(opform->oprcode))
 6587 tgl@sss.pgh.pa.us         745         [ #  # ]:UBC           0 :         ereport(ERROR,
                                746                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                747                 :                :                  errmsg("operator is only a shell: %s",
                                748                 :                :                         op_signature_string(opname,
                                749                 :                :                                             opform->oprleft,
                                750                 :                :                                             opform->oprright)),
                                751                 :                :                  parser_errposition(pstate, location)));
                                752                 :                : 
                                753                 :                :     /* Do typecasting and build the expression tree */
 2056 tgl@sss.pgh.pa.us         754         [ +  + ]:CBC      407362 :     if (ltree == NULL)
                                755                 :                :     {
                                756                 :                :         /* prefix operator */
 6587                           757                 :            812 :         args = list_make1(rtree);
                                758                 :            812 :         actual_arg_types[0] = rtypeId;
                                759                 :            812 :         declared_arg_types[0] = opform->oprright;
                                760                 :            812 :         nargs = 1;
                                761                 :                :     }
                                762                 :                :     else
                                763                 :                :     {
                                764                 :                :         /* otherwise, binary operator */
                                765                 :         406550 :         args = list_make2(ltree, rtree);
                                766                 :         406550 :         actual_arg_types[0] = ltypeId;
                                767                 :         406550 :         actual_arg_types[1] = rtypeId;
                                768                 :         406550 :         declared_arg_types[0] = opform->oprleft;
                                769                 :         406550 :         declared_arg_types[1] = opform->oprright;
                                770                 :         406550 :         nargs = 2;
                                771                 :                :     }
                                772                 :                : 
                                773                 :                :     /*
                                774                 :                :      * enforce consistency with polymorphic argument and return types,
                                775                 :                :      * possibly adjusting return type or declared_arg_types (which will be
                                776                 :                :      * used as the cast destination by make_fn_arguments)
                                777                 :                :      */
                                778                 :         407362 :     rettype = enforce_generic_type_consistency(actual_arg_types,
                                779                 :                :                                                declared_arg_types,
                                780                 :                :                                                nargs,
                                781                 :                :                                                opform->oprresult,
                                782                 :                :                                                false);
                                783                 :                : 
                                784                 :                :     /* perform the necessary typecasting of arguments */
                                785                 :         407362 :     make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types);
                                786                 :                : 
                                787                 :                :     /* and build the expression node */
                                788                 :         407358 :     result = makeNode(OpExpr);
                                789                 :         407358 :     result->opno = oprid(tup);
                                790                 :         407358 :     result->opfuncid = opform->oprcode;
                                791                 :         407358 :     result->opresulttype = rettype;
                                792                 :         407358 :     result->opretset = get_func_retset(opform->oprcode);
                                793                 :                :     /* opcollid and inputcollid will be set by parse_collate.c */
                                794                 :         407358 :     result->args = args;
 6459                           795                 :         407358 :     result->location = location;
                                796                 :                : 
                                797                 :                :     /* if it returns a set, check that's OK */
 3521                           798         [ +  + ]:         407358 :     if (result->opretset)
                                799                 :                :     {
 3248                           800                 :              4 :         check_srf_call_placement(pstate, last_srf, location);
                                801                 :                :         /* ... and remember it for error checks at higher levels */
                                802                 :              4 :         pstate->p_last_srf = (Node *) result;
                                803                 :                :     }
                                804                 :                : 
 8428                           805                 :         407358 :     ReleaseSysCache(tup);
                                806                 :                : 
 6587                           807                 :         407358 :     return (Expr *) result;
                                808                 :                : }
                                809                 :                : 
                                810                 :                : /*
                                811                 :                :  * make_scalar_array_op()
                                812                 :                :  *      Build expression tree for "scalar op ANY/ALL (array)" construct.
                                813                 :                :  */
                                814                 :                : Expr *
 8346                           815                 :          22948 : make_scalar_array_op(ParseState *pstate, List *opname,
                                816                 :                :                      bool useOr,
                                817                 :                :                      Node *ltree, Node *rtree,
                                818                 :                :                      int location)
                                819                 :                : {
                                820                 :                :     Oid         ltypeId,
                                821                 :                :                 rtypeId,
                                822                 :                :                 atypeId,
                                823                 :                :                 res_atypeId;
                                824                 :                :     Operator    tup;
                                825                 :                :     Form_pg_operator opform;
                                826                 :                :     Oid         actual_arg_types[2];
                                827                 :                :     Oid         declared_arg_types[2];
                                828                 :                :     List       *args;
                                829                 :                :     Oid         rettype;
                                830                 :                :     ScalarArrayOpExpr *result;
                                831                 :                : 
                                832                 :          22948 :     ltypeId = exprType(ltree);
                                833                 :          22948 :     atypeId = exprType(rtree);
                                834                 :                : 
                                835                 :                :     /*
                                836                 :                :      * The right-hand input of the operator will be the element type of the
                                837                 :                :      * array.  However, if we currently have just an untyped literal on the
                                838                 :                :      * right, stay with that and hope we can resolve the operator.
                                839                 :                :      */
                                840         [ +  + ]:          22948 :     if (atypeId == UNKNOWNOID)
                                841                 :            148 :         rtypeId = UNKNOWNOID;
                                842                 :                :     else
                                843                 :                :     {
 5675                           844                 :          22800 :         rtypeId = get_base_element_type(atypeId);
 8346                           845         [ +  + ]:          22800 :         if (!OidIsValid(rtypeId))
 8327                           846         [ +  - ]:              4 :             ereport(ERROR,
                                847                 :                :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                848                 :                :                      errmsg("op ANY/ALL (array) requires array on right side"),
                                849                 :                :                      parser_errposition(pstate, location)));
                                850                 :                :     }
                                851                 :                : 
                                852                 :                :     /* Now resolve the operator */
 7357                           853                 :          22944 :     tup = oper(pstate, opname, ltypeId, rtypeId, false, location);
 8346                           854                 :          22944 :     opform = (Form_pg_operator) GETSTRUCT(tup);
                                855                 :                : 
                                856                 :                :     /* Check it's not a shell */
 6587                           857         [ -  + ]:          22944 :     if (!RegProcedureIsValid(opform->oprcode))
 6587 tgl@sss.pgh.pa.us         858         [ #  # ]:UBC           0 :         ereport(ERROR,
                                859                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                860                 :                :                  errmsg("operator is only a shell: %s",
                                861                 :                :                         op_signature_string(opname,
                                862                 :                :                                             opform->oprleft,
                                863                 :                :                                             opform->oprright)),
                                864                 :                :                  parser_errposition(pstate, location)));
                                865                 :                : 
 8010 neilc@samurai.com         866                 :CBC       22944 :     args = list_make2(ltree, rtree);
 8346 tgl@sss.pgh.pa.us         867                 :          22944 :     actual_arg_types[0] = ltypeId;
                                868                 :          22944 :     actual_arg_types[1] = rtypeId;
                                869                 :          22944 :     declared_arg_types[0] = opform->oprleft;
                                870                 :          22944 :     declared_arg_types[1] = opform->oprright;
                                871                 :                : 
                                872                 :                :     /*
                                873                 :                :      * enforce consistency with polymorphic argument and return types,
                                874                 :                :      * possibly adjusting return type or declared_arg_types (which will be
                                875                 :                :      * used as the cast destination by make_fn_arguments)
                                876                 :                :      */
                                877                 :          22944 :     rettype = enforce_generic_type_consistency(actual_arg_types,
                                878                 :                :                                                declared_arg_types,
                                879                 :                :                                                2,
                                880                 :                :                                                opform->oprresult,
                                881                 :                :                                                false);
                                882                 :                : 
                                883                 :                :     /*
                                884                 :                :      * Check that operator result is boolean
                                885                 :                :      */
                                886         [ +  + ]:          22944 :     if (rettype != BOOLOID)
 8327                           887         [ +  - ]:              4 :         ereport(ERROR,
                                888                 :                :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                889                 :                :                  errmsg("op ANY/ALL (array) requires operator to yield boolean"),
                                890                 :                :                  parser_errposition(pstate, location)));
 8346                           891         [ -  + ]:          22940 :     if (get_func_retset(opform->oprcode))
 8327 tgl@sss.pgh.pa.us         892         [ #  # ]:UBC           0 :         ereport(ERROR,
                                893                 :                :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                894                 :                :                  errmsg("op ANY/ALL (array) requires operator not to return a set"),
                                895                 :                :                  parser_errposition(pstate, location)));
                                896                 :                : 
                                897                 :                :     /*
                                898                 :                :      * Now switch back to the array type on the right, arranging for any
                                899                 :                :      * needed cast to be applied.  Beware of polymorphic operators here;
                                900                 :                :      * enforce_generic_type_consistency may or may not have replaced a
                                901                 :                :      * polymorphic type with a real one.
                                902                 :                :      */
 6973 tgl@sss.pgh.pa.us         903   [ +  -  +  -  :CBC       22940 :     if (IsPolymorphicType(declared_arg_types[1]))
                                     +  -  +  +  +  
                                     +  +  -  +  -  
                                     +  -  +  -  +  
                                           -  -  + ]
                                904                 :                :     {
                                905                 :                :         /* assume the actual array type is OK */
                                906                 :             46 :         res_atypeId = atypeId;
                                907                 :                :     }
                                908                 :                :     else
                                909                 :                :     {
                                910                 :          22894 :         res_atypeId = get_array_type(declared_arg_types[1]);
                                911         [ -  + ]:          22894 :         if (!OidIsValid(res_atypeId))
 6973 tgl@sss.pgh.pa.us         912         [ #  # ]:UBC           0 :             ereport(ERROR,
                                913                 :                :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
                                914                 :                :                      errmsg("could not find array type for data type %s",
                                915                 :                :                             format_type_be(declared_arg_types[1])),
                                916                 :                :                      parser_errposition(pstate, location)));
                                917                 :                :     }
 8346 tgl@sss.pgh.pa.us         918                 :CBC       22940 :     actual_arg_types[1] = atypeId;
                                919                 :          22940 :     declared_arg_types[1] = res_atypeId;
                                920                 :                : 
                                921                 :                :     /* perform the necessary typecasting of arguments */
                                922                 :          22940 :     make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types);
                                923                 :                : 
                                924                 :                :     /* and build the expression node */
                                925                 :          22940 :     result = makeNode(ScalarArrayOpExpr);
                                926                 :          22940 :     result->opno = oprid(tup);
 6739                           927                 :          22940 :     result->opfuncid = opform->oprcode;
 1853 drowley@postgresql.o      928                 :          22940 :     result->hashfuncid = InvalidOid;
 1763                           929                 :          22940 :     result->negfuncid = InvalidOid;
 8346 tgl@sss.pgh.pa.us         930                 :          22940 :     result->useOr = useOr;
                                931                 :                :     /* inputcollid will be set by parse_collate.c */
                                932                 :          22940 :     result->args = args;
 6459                           933                 :          22940 :     result->location = location;
                                934                 :                : 
 8346                           935                 :          22940 :     ReleaseSysCache(tup);
                                936                 :                : 
                                937                 :          22940 :     return (Expr *) result;
                                938                 :                : }
                                939                 :                : 
                                940                 :                : 
                                941                 :                : /*
                                942                 :                :  * Lookaside cache to speed operator lookup.  Possibly this should be in
                                943                 :                :  * a separate module under utils/cache/ ?
                                944                 :                :  *
                                945                 :                :  * The idea here is that the mapping from operator name and given argument
                                946                 :                :  * types is constant for a given search path (or single specified schema OID)
                                947                 :                :  * so long as the contents of pg_operator and pg_cast don't change.  And that
                                948                 :                :  * mapping is pretty expensive to compute, especially for ambiguous operators;
                                949                 :                :  * this is mainly because there are a *lot* of instances of popular operator
                                950                 :                :  * names such as "=", and we have to check each one to see which is the
                                951                 :                :  * best match.  So once we have identified the correct mapping, we save it
                                952                 :                :  * in a cache that need only be flushed on pg_operator or pg_cast change.
                                953                 :                :  * (pg_cast must be considered because changes in the set of implicit casts
                                954                 :                :  * affect the set of applicable operators for any given input datatype.)
                                955                 :                :  *
                                956                 :                :  * XXX in principle, ALTER TABLE ... INHERIT could affect the mapping as
                                957                 :                :  * well, but we disregard that since there's no convenient way to find out
                                958                 :                :  * about it, and it seems a pretty far-fetched corner-case anyway.
                                959                 :                :  *
                                960                 :                :  * Note: at some point it might be worth doing a similar cache for function
                                961                 :                :  * lookups.  However, the potential gain is a lot less since (a) function
                                962                 :                :  * names are generally not overloaded as heavily as operator names, and
                                963                 :                :  * (b) we'd have to flush on pg_proc updates, which are probably a good
                                964                 :                :  * deal more common than pg_operator updates.
                                965                 :                :  */
                                966                 :                : 
                                967                 :                : /* The operator cache hashtable */
                                968                 :                : static HTAB *OprCacheHash = NULL;
                                969                 :                : 
                                970                 :                : 
                                971                 :                : /*
                                972                 :                :  * make_oper_cache_key
                                973                 :                :  *      Fill the lookup key struct given operator name and arg types.
                                974                 :                :  *
                                975                 :                :  * Returns true if successful, false if the search_path overflowed
                                976                 :                :  * (hence no caching is possible).
                                977                 :                :  *
                                978                 :                :  * pstate/location are used only to report the error position; pass NULL/-1
                                979                 :                :  * if not available.
                                980                 :                :  */
                                981                 :                : static bool
 4066 alvherre@alvh.no-ip.      982                 :         473806 : make_oper_cache_key(ParseState *pstate, OprCacheKey *key, List *opname,
                                983                 :                :                     Oid ltypeId, Oid rtypeId, int location)
                                984                 :                : {
                                985                 :                :     char       *schemaname;
                                986                 :                :     char       *opername;
                                987                 :                : 
                                988                 :                :     /* deconstruct the name list */
 6733 tgl@sss.pgh.pa.us         989                 :         473806 :     DeconstructQualifiedName(opname, &schemaname, &opername);
                                990                 :                : 
                                991                 :                :     /* ensure zero-fill for stable hashing */
                                992   [ +  -  +  -  :        8528508 :     MemSet(key, 0, sizeof(OprCacheKey));
                                     +  -  +  -  +  
                                                 + ]
                                993                 :                : 
                                994                 :                :     /* save operator name and input types into key */
                                995                 :         473806 :     strlcpy(key->oprname, opername, NAMEDATALEN);
                                996                 :         473806 :     key->left_arg = ltypeId;
                                997                 :         473806 :     key->right_arg = rtypeId;
                                998                 :                : 
                                999         [ +  + ]:         473806 :     if (schemaname)
                               1000                 :                :     {
                               1001                 :                :         ParseCallbackState pcbstate;
                               1002                 :                : 
                               1003                 :                :         /* search only in exact schema given */
 4066 alvherre@alvh.no-ip.     1004                 :           9635 :         setup_parser_errposition_callback(&pcbstate, pstate, location);
 4847 bruce@momjian.us         1005                 :           9635 :         key->search_path[0] = LookupExplicitNamespace(schemaname, false);
 4066 alvherre@alvh.no-ip.     1006                 :           9635 :         cancel_parser_errposition_callback(&pcbstate);
                               1007                 :                :     }
                               1008                 :                :     else
                               1009                 :                :     {
                               1010                 :                :         /* get the active search path */
 6733 tgl@sss.pgh.pa.us        1011         [ -  + ]:         464171 :         if (fetch_search_path_array(key->search_path,
                               1012                 :                :                                     MAX_CACHED_PATH_LEN) > MAX_CACHED_PATH_LEN)
 6733 tgl@sss.pgh.pa.us        1013                 :UBC           0 :             return false;       /* oops, didn't fit */
                               1014                 :                :     }
                               1015                 :                : 
 6733 tgl@sss.pgh.pa.us        1016                 :CBC      473806 :     return true;
                               1017                 :                : }
                               1018                 :                : 
                               1019                 :                : /*
                               1020                 :                :  * find_oper_cache_entry
                               1021                 :                :  *
                               1022                 :                :  * Look for a cache entry matching the given key.  If found, return the
                               1023                 :                :  * contained operator OID, else return InvalidOid.
                               1024                 :                :  */
                               1025                 :                : static Oid
                               1026                 :         473806 : find_oper_cache_entry(OprCacheKey *key)
                               1027                 :                : {
                               1028                 :                :     OprCacheEntry *oprentry;
                               1029                 :                : 
                               1030         [ +  + ]:         473806 :     if (OprCacheHash == NULL)
                               1031                 :                :     {
                               1032                 :                :         /* First time through: initialize the hash table */
                               1033                 :                :         HASHCTL     ctl;
                               1034                 :                : 
                               1035                 :           6363 :         ctl.keysize = sizeof(OprCacheKey);
                               1036                 :           6363 :         ctl.entrysize = sizeof(OprCacheEntry);
                               1037                 :           6363 :         OprCacheHash = hash_create("Operator lookup cache", 256,
                               1038                 :                :                                    &ctl, HASH_ELEM | HASH_BLOBS);
                               1039                 :                : 
                               1040                 :                :         /* Arrange to flush cache on pg_operator and pg_cast changes */
                               1041                 :           6363 :         CacheRegisterSyscacheCallback(OPERNAMENSP,
                               1042                 :                :                                       InvalidateOprCacheCallBack,
                               1043                 :                :                                       (Datum) 0);
                               1044                 :           6363 :         CacheRegisterSyscacheCallback(CASTSOURCETARGET,
                               1045                 :                :                                       InvalidateOprCacheCallBack,
                               1046                 :                :                                       (Datum) 0);
                               1047                 :                :     }
                               1048                 :                : 
                               1049                 :                :     /* Look for an existing entry */
                               1050                 :         473806 :     oprentry = (OprCacheEntry *) hash_search(OprCacheHash,
                               1051                 :                :                                              key,
                               1052                 :                :                                              HASH_FIND, NULL);
                               1053         [ +  + ]:         473806 :     if (oprentry == NULL)
                               1054                 :          52803 :         return InvalidOid;
                               1055                 :                : 
                               1056                 :         421003 :     return oprentry->opr_oid;
                               1057                 :                : }
                               1058                 :                : 
                               1059                 :                : /*
                               1060                 :                :  * make_oper_cache_entry
                               1061                 :                :  *
                               1062                 :                :  * Insert a cache entry for the given key.
                               1063                 :                :  */
                               1064                 :                : static void
                               1065                 :          52712 : make_oper_cache_entry(OprCacheKey *key, Oid opr_oid)
                               1066                 :                : {
                               1067                 :                :     OprCacheEntry *oprentry;
                               1068                 :                : 
                               1069         [ -  + ]:          52712 :     Assert(OprCacheHash != NULL);
                               1070                 :                : 
                               1071                 :          52712 :     oprentry = (OprCacheEntry *) hash_search(OprCacheHash,
                               1072                 :                :                                              key,
                               1073                 :                :                                              HASH_ENTER, NULL);
                               1074                 :          52712 :     oprentry->opr_oid = opr_oid;
                               1075                 :          52712 : }
                               1076                 :                : 
                               1077                 :                : /*
                               1078                 :                :  * Callback for pg_operator and pg_cast inval events
                               1079                 :                :  */
                               1080                 :                : static void
   76 michael@paquier.xyz      1081                 :GNC        8678 : InvalidateOprCacheCallBack(Datum arg, SysCacheIdentifier cacheid,
                               1082                 :                :                            uint32 hashvalue)
                               1083                 :                : {
                               1084                 :                :     HASH_SEQ_STATUS status;
                               1085                 :                :     OprCacheEntry *hentry;
                               1086                 :                : 
 6733 tgl@sss.pgh.pa.us        1087         [ -  + ]:CBC        8678 :     Assert(OprCacheHash != NULL);
                               1088                 :                : 
                               1089                 :                :     /* Currently we just flush all entries; hard to be smarter ... */
                               1090                 :           8678 :     hash_seq_init(&status, OprCacheHash);
                               1091                 :                : 
                               1092         [ +  + ]:          22076 :     while ((hentry = (OprCacheEntry *) hash_seq_search(&status)) != NULL)
                               1093                 :                :     {
                               1094         [ -  + ]:          13398 :         if (hash_search(OprCacheHash,
 1184 peter@eisentraut.org     1095                 :          13398 :                         &hentry->key,
                               1096                 :                :                         HASH_REMOVE, NULL) == NULL)
 6733 tgl@sss.pgh.pa.us        1097         [ #  # ]:UBC           0 :             elog(ERROR, "hash table corrupted");
                               1098                 :                :     }
 6733 tgl@sss.pgh.pa.us        1099                 :CBC        8678 : }
        

Generated by: LCOV version 2.5.0-beta