LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - oid.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 74.3 % 148 110 14 22 2 17 69 6 18 19 47 31
Current Date: 2023-04-08 15:15:32 Functions: 76.0 % 25 19 6 19 6 13 6
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * oid.c
       4                 :  *    Functions for the built-in type Oid ... also oidvector.
       5                 :  *
       6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7                 :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :  *
       9                 :  *
      10                 :  * IDENTIFICATION
      11                 :  *    src/backend/utils/adt/oid.c
      12                 :  *
      13                 :  *-------------------------------------------------------------------------
      14                 :  */
      15                 : #include "postgres.h"
      16                 : 
      17                 : #include <ctype.h>
      18                 : #include <limits.h>
      19                 : 
      20                 : #include "catalog/pg_type.h"
      21                 : #include "libpq/pqformat.h"
      22                 : #include "nodes/miscnodes.h"
      23                 : #include "nodes/value.h"
      24                 : #include "utils/array.h"
      25                 : #include "utils/builtins.h"
      26                 : 
      27                 : 
      28                 : #define OidVectorSize(n)    (offsetof(oidvector, values) + (n) * sizeof(Oid))
      29                 : 
      30                 : 
      31                 : /*****************************************************************************
      32                 :  *   USER I/O ROUTINES                                                       *
      33                 :  *****************************************************************************/
      34                 : 
      35 ECB             : Datum
      36 GIC    17351216 : oidin(PG_FUNCTION_ARGS)
      37                 : {
      38        17351216 :     char       *s = PG_GETARG_CSTRING(0);
      39                 :     Oid         result;
      40 ECB             : 
      41 GNC    17351216 :     result = uint32in_subr(s, NULL, "oid", fcinfo->context);
      42 GIC    17351186 :     PG_RETURN_OID(result);
      43 ECB             : }
      44                 : 
      45                 : Datum
      46 CBC     4795923 : oidout(PG_FUNCTION_ARGS)
      47 ECB             : {
      48 CBC     4795923 :     Oid         o = PG_GETARG_OID(0);
      49 GIC     4795923 :     char       *result = (char *) palloc(12);
      50 ECB             : 
      51 GIC     4795923 :     snprintf(result, 12, "%u", o);
      52 GBC     4795923 :     PG_RETURN_CSTRING(result);
      53 EUB             : }
      54                 : 
      55                 : /*
      56 ECB             :  *      oidrecv         - converts external binary format to oid
      57                 :  */
      58                 : Datum
      59 CBC         102 : oidrecv(PG_FUNCTION_ARGS)
      60                 : {
      61 GIC         102 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
      62 ECB             : 
      63 CBC         102 :     PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid)));
      64 ECB             : }
      65                 : 
      66                 : /*
      67                 :  *      oidsend         - converts oid to binary format
      68                 :  */
      69                 : Datum
      70 GIC           7 : oidsend(PG_FUNCTION_ARGS)
      71                 : {
      72               7 :     Oid         arg1 = PG_GETARG_OID(0);
      73                 :     StringInfoData buf;
      74                 : 
      75               7 :     pq_begintypsend(&buf);
      76 CBC           7 :     pq_sendint32(&buf, arg1);
      77 GIC           7 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
      78 ECB             : }
      79                 : 
      80                 : /*
      81                 :  * construct oidvector given a raw array of Oids
      82                 :  *
      83                 :  * If oids is NULL then caller must fill values[] afterward
      84                 :  */
      85                 : oidvector *
      86 CBC      167306 : buildoidvector(const Oid *oids, int n)
      87                 : {
      88 ECB             :     oidvector  *result;
      89                 : 
      90 CBC      167306 :     result = (oidvector *) palloc0(OidVectorSize(n));
      91 ECB             : 
      92 GIC      167306 :     if (n > 0 && oids)
      93          164947 :         memcpy(result->values, oids, n * sizeof(Oid));
      94 ECB             : 
      95                 :     /*
      96                 :      * Attach standard array header.  For historical reasons, we set the index
      97                 :      * lower bound to 0 not 1.
      98                 :      */
      99 GIC      167306 :     SET_VARSIZE(result, OidVectorSize(n));
     100          167306 :     result->ndim = 1;
     101          167306 :     result->dataoffset = 0;      /* never any nulls */
     102 GBC      167306 :     result->elemtype = OIDOID;
     103 GIC      167306 :     result->dim1 = n;
     104 GBC      167306 :     result->lbound1 = 0;
     105 EUB             : 
     106 GIC      167306 :     return result;
     107                 : }
     108                 : 
     109                 : /*
     110                 :  *      oidvectorin         - converts "num num ..." to internal form
     111                 :  */
     112                 : Datum
     113         1003190 : oidvectorin(PG_FUNCTION_ARGS)
     114 EUB             : {
     115 GIC     1003190 :     char       *oidString = PG_GETARG_CSTRING(0);
     116 GNC     1003190 :     Node       *escontext = fcinfo->context;
     117                 :     oidvector  *result;
     118 EUB             :     int         nalloc;
     119                 :     int         n;
     120                 : 
     121 GBC     1003190 :     nalloc = 32;                /* arbitrary initial size guess */
     122         1003190 :     result = (oidvector *) palloc0(OidVectorSize(nalloc));
     123 EUB             : 
     124 GIC     1003190 :     for (n = 0;; n++)
     125 EUB             :     {
     126 GIC     3740530 :         while (*oidString && isspace((unsigned char) *oidString))
     127 GBC      890723 :             oidString++;
     128 GIC     2849807 :         if (*oidString == '\0')
     129         1003178 :             break;
     130 EUB             : 
     131 GBC     1846629 :         if (n >= nalloc)
     132 EUB             :         {
     133 UBC           0 :             nalloc *= 2;
     134               0 :             result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
     135                 :         }
     136                 : 
     137 GNC     1846629 :         result->values[n] = uint32in_subr(oidString, &oidString,
     138                 :                                           "oid", escontext);
     139         1846629 :         if (SOFT_ERROR_OCCURRED(escontext))
     140              12 :             PG_RETURN_NULL();
     141 EUB             :     }
     142                 : 
     143 GIC     1003178 :     SET_VARSIZE(result, OidVectorSize(n));
     144         1003178 :     result->ndim = 1;
     145         1003178 :     result->dataoffset = 0;      /* never any nulls */
     146         1003178 :     result->elemtype = OIDOID;
     147         1003178 :     result->dim1 = n;
     148 GBC     1003178 :     result->lbound1 = 0;
     149                 : 
     150         1003178 :     PG_RETURN_POINTER(result);
     151                 : }
     152                 : 
     153                 : /*
     154                 :  *      oidvectorout - converts internal form to "num num ..."
     155                 :  */
     156                 : Datum
     157 CBC        3574 : oidvectorout(PG_FUNCTION_ARGS)
     158                 : {
     159            3574 :     oidvector  *oidArray = (oidvector *) PG_GETARG_POINTER(0);
     160                 :     int         num,
     161            3574 :                 nnums = oidArray->dim1;
     162 ECB             :     char       *rp;
     163                 :     char       *result;
     164                 : 
     165                 :     /* assumes sign, 10 digits, ' ' */
     166 GIC        3574 :     rp = result = (char *) palloc(nnums * 12 + 1);
     167            8025 :     for (num = 0; num < nnums; num++)
     168                 :     {
     169            4451 :         if (num != 0)
     170 CBC        1654 :             *rp++ = ' ';
     171 GIC        4451 :         sprintf(rp, "%u", oidArray->values[num]);
     172 GBC       11292 :         while (*++rp != '\0')
     173 EUB             :             ;
     174                 :     }
     175 GIC        3574 :     *rp = '\0';
     176            3574 :     PG_RETURN_CSTRING(result);
     177                 : }
     178                 : 
     179                 : /*
     180 ECB             :  *      oidvectorrecv           - converts external binary format to oidvector
     181                 :  */
     182                 : Datum
     183 LBC           0 : oidvectorrecv(PG_FUNCTION_ARGS)
     184                 : {
     185               0 :     LOCAL_FCINFO(locfcinfo, 3);
     186               0 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
     187 ECB             :     oidvector  *result;
     188                 : 
     189                 :     /*
     190                 :      * Normally one would call array_recv() using DirectFunctionCall3, but
     191                 :      * that does not work since array_recv wants to cache some data using
     192                 :      * fcinfo->flinfo->fn_extra.  So we need to pass it our own flinfo
     193                 :      * parameter.
     194                 :      */
     195 UIC           0 :     InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
     196                 :                              InvalidOid, NULL, NULL);
     197                 : 
     198 LBC           0 :     locfcinfo->args[0].value = PointerGetDatum(buf);
     199 UIC           0 :     locfcinfo->args[0].isnull = false;
     200 LBC           0 :     locfcinfo->args[1].value = ObjectIdGetDatum(OIDOID);
     201               0 :     locfcinfo->args[1].isnull = false;
     202 UIC           0 :     locfcinfo->args[2].value = Int32GetDatum(-1);
     203 LBC           0 :     locfcinfo->args[2].isnull = false;
     204                 : 
     205 UIC           0 :     result = (oidvector *) DatumGetPointer(array_recv(locfcinfo));
     206                 : 
     207 LBC           0 :     Assert(!locfcinfo->isnull);
     208                 : 
     209 ECB             :     /* sanity checks: oidvector must be 1-D, 0-based, no nulls */
     210 LBC           0 :     if (ARR_NDIM(result) != 1 ||
     211 UIC           0 :         ARR_HASNULL(result) ||
     212 LBC           0 :         ARR_ELEMTYPE(result) != OIDOID ||
     213 UIC           0 :         ARR_LBOUND(result)[0] != 0)
     214               0 :         ereport(ERROR,
     215                 :                 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
     216 ECB             :                  errmsg("invalid oidvector data")));
     217                 : 
     218 LBC           0 :     PG_RETURN_POINTER(result);
     219 ECB             : }
     220                 : 
     221                 : /*
     222                 :  *      oidvectorsend           - converts oidvector to binary format
     223                 :  */
     224                 : Datum
     225 LBC           0 : oidvectorsend(PG_FUNCTION_ARGS)
     226                 : {
     227               0 :     return array_send(fcinfo);
     228 ECB             : }
     229                 : 
     230                 : /*
     231                 :  *      oidparse                - get OID from ICONST/FCONST node
     232                 :  */
     233                 : Oid
     234 CBC          68 : oidparse(Node *node)
     235                 : {
     236              68 :     switch (nodeTag(node))
     237 ECB             :     {
     238 GIC          62 :         case T_Integer:
     239 CBC          62 :             return intVal(node);
     240 GIC           6 :         case T_Float:
     241                 : 
     242                 :             /*
     243 ECB             :              * Values too large for int4 will be represented as Float
     244                 :              * constants by the lexer.  Accept these if they are valid OID
     245                 :              * strings.
     246                 :              */
     247 GNC           6 :             return uint32in_subr(castNode(Float, node)->fval, NULL,
     248                 :                                  "oid", NULL);
     249 LBC           0 :         default:
     250 UIC           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     251                 :     }
     252                 :     return InvalidOid;          /* keep compiler quiet */
     253 EUB             : }
     254                 : 
     255                 : /* qsort comparison function for Oids */
     256                 : int
     257 GIC      318327 : oid_cmp(const void *p1, const void *p2)
     258 EUB             : {
     259 GIC      318327 :     Oid         v1 = *((const Oid *) p1);
     260          318327 :     Oid         v2 = *((const Oid *) p2);
     261                 : 
     262 GBC      318327 :     if (v1 < v2)
     263 GIC      143529 :         return -1;
     264 GBC      174798 :     if (v1 > v2)
     265           13117 :         return 1;
     266 GIC      161681 :     return 0;
     267 EUB             : }
     268                 : 
     269                 : 
     270                 : /*****************************************************************************
     271 ECB             :  *   PUBLIC ROUTINES                                                         *
     272                 :  *****************************************************************************/
     273                 : 
     274                 : Datum
     275 CBC   144088601 : oideq(PG_FUNCTION_ARGS)
     276                 : {
     277 GIC   144088601 :     Oid         arg1 = PG_GETARG_OID(0);
     278       144088601 :     Oid         arg2 = PG_GETARG_OID(1);
     279 ECB             : 
     280 GIC   144088601 :     PG_RETURN_BOOL(arg1 == arg2);
     281 ECB             : }
     282                 : 
     283                 : Datum
     284 GIC     1069758 : oidne(PG_FUNCTION_ARGS)
     285                 : {
     286         1069758 :     Oid         arg1 = PG_GETARG_OID(0);
     287 CBC     1069758 :     Oid         arg2 = PG_GETARG_OID(1);
     288                 : 
     289         1069758 :     PG_RETURN_BOOL(arg1 != arg2);
     290                 : }
     291 ECB             : 
     292                 : Datum
     293 GIC     1806382 : oidlt(PG_FUNCTION_ARGS)
     294                 : {
     295 CBC     1806382 :     Oid         arg1 = PG_GETARG_OID(0);
     296 GIC     1806382 :     Oid         arg2 = PG_GETARG_OID(1);
     297 ECB             : 
     298 GIC     1806382 :     PG_RETURN_BOOL(arg1 < arg2);
     299 ECB             : }
     300                 : 
     301                 : Datum
     302 GIC      650180 : oidle(PG_FUNCTION_ARGS)
     303 EUB             : {
     304 GIC      650180 :     Oid         arg1 = PG_GETARG_OID(0);
     305 GBC      650180 :     Oid         arg2 = PG_GETARG_OID(1);
     306                 : 
     307          650180 :     PG_RETURN_BOOL(arg1 <= arg2);
     308                 : }
     309                 : 
     310                 : Datum
     311           25930 : oidge(PG_FUNCTION_ARGS)
     312                 : {
     313           25930 :     Oid         arg1 = PG_GETARG_OID(0);
     314 GIC       25930 :     Oid         arg2 = PG_GETARG_OID(1);
     315 EUB             : 
     316 GIC       25930 :     PG_RETURN_BOOL(arg1 >= arg2);
     317                 : }
     318                 : 
     319                 : Datum
     320          173272 : oidgt(PG_FUNCTION_ARGS)
     321                 : {
     322          173272 :     Oid         arg1 = PG_GETARG_OID(0);
     323          173272 :     Oid         arg2 = PG_GETARG_OID(1);
     324                 : 
     325          173272 :     PG_RETURN_BOOL(arg1 > arg2);
     326                 : }
     327                 : 
     328                 : Datum
     329 UIC           0 : oidlarger(PG_FUNCTION_ARGS)
     330                 : {
     331               0 :     Oid         arg1 = PG_GETARG_OID(0);
     332               0 :     Oid         arg2 = PG_GETARG_OID(1);
     333                 : 
     334               0 :     PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
     335                 : }
     336                 : 
     337                 : Datum
     338               0 : oidsmaller(PG_FUNCTION_ARGS)
     339                 : {
     340               0 :     Oid         arg1 = PG_GETARG_OID(0);
     341               0 :     Oid         arg2 = PG_GETARG_OID(1);
     342                 : 
     343               0 :     PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
     344                 : }
     345                 : 
     346                 : Datum
     347 GIC       37548 : oidvectoreq(PG_FUNCTION_ARGS)
     348                 : {
     349           37548 :     int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
     350                 : 
     351           37548 :     PG_RETURN_BOOL(cmp == 0);
     352                 : }
     353                 : 
     354                 : Datum
     355           10731 : oidvectorne(PG_FUNCTION_ARGS)
     356                 : {
     357           10731 :     int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
     358                 : 
     359           10731 :     PG_RETURN_BOOL(cmp != 0);
     360                 : }
     361                 : 
     362                 : Datum
     363            1842 : oidvectorlt(PG_FUNCTION_ARGS)
     364                 : {
     365            1842 :     int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
     366                 : 
     367            1842 :     PG_RETURN_BOOL(cmp < 0);
     368                 : }
     369                 : 
     370                 : Datum
     371             636 : oidvectorle(PG_FUNCTION_ARGS)
     372                 : {
     373             636 :     int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
     374                 : 
     375             636 :     PG_RETURN_BOOL(cmp <= 0);
     376                 : }
     377                 : 
     378                 : Datum
     379 UIC           0 : oidvectorge(PG_FUNCTION_ARGS)
     380                 : {
     381               0 :     int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
     382                 : 
     383               0 :     PG_RETURN_BOOL(cmp >= 0);
     384                 : }
     385                 : 
     386                 : Datum
     387               0 : oidvectorgt(PG_FUNCTION_ARGS)
     388                 : {
     389               0 :     int32       cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
     390                 : 
     391               0 :     PG_RETURN_BOOL(cmp > 0);
     392                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a