LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_oid.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 53.1 % 96 51
Test Date: 2026-03-03 11:15:01 Functions: 75.0 % 28 21
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*
       2              :  * contrib/btree_gist/btree_oid.c
       3              :  */
       4              : #include "postgres.h"
       5              : 
       6              : #include "btree_gist.h"
       7              : #include "btree_utils_num.h"
       8              : #include "utils/rel.h"
       9              : #include "utils/sortsupport.h"
      10              : 
      11              : typedef struct
      12              : {
      13              :     Oid         lower;
      14              :     Oid         upper;
      15              : } oidKEY;
      16              : 
      17              : /* GiST support functions */
      18            4 : PG_FUNCTION_INFO_V1(gbt_oid_compress);
      19            4 : PG_FUNCTION_INFO_V1(gbt_oid_fetch);
      20            4 : PG_FUNCTION_INFO_V1(gbt_oid_union);
      21            4 : PG_FUNCTION_INFO_V1(gbt_oid_picksplit);
      22            4 : PG_FUNCTION_INFO_V1(gbt_oid_consistent);
      23            4 : PG_FUNCTION_INFO_V1(gbt_oid_distance);
      24            4 : PG_FUNCTION_INFO_V1(gbt_oid_penalty);
      25            4 : PG_FUNCTION_INFO_V1(gbt_oid_same);
      26            4 : PG_FUNCTION_INFO_V1(gbt_oid_sortsupport);
      27              : 
      28              : 
      29              : static bool
      30         2244 : gbt_oidgt(const void *a, const void *b, FmgrInfo *flinfo)
      31              : {
      32         2244 :     return (*((const Oid *) a) > *((const Oid *) b));
      33              : }
      34              : static bool
      35          258 : gbt_oidge(const void *a, const void *b, FmgrInfo *flinfo)
      36              : {
      37          258 :     return (*((const Oid *) a) >= *((const Oid *) b));
      38              : }
      39              : static bool
      40          250 : gbt_oideq(const void *a, const void *b, FmgrInfo *flinfo)
      41              : {
      42          250 :     return (*((const Oid *) a) == *((const Oid *) b));
      43              : }
      44              : static bool
      45         1013 : gbt_oidle(const void *a, const void *b, FmgrInfo *flinfo)
      46              : {
      47         1013 :     return (*((const Oid *) a) <= *((const Oid *) b));
      48              : }
      49              : static bool
      50         2994 : gbt_oidlt(const void *a, const void *b, FmgrInfo *flinfo)
      51              : {
      52         2994 :     return (*((const Oid *) a) < *((const Oid *) b));
      53              : }
      54              : 
      55              : static int
      56         1997 : gbt_oidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      57              : {
      58         1997 :     oidKEY     *ia = (oidKEY *) (((const Nsrt *) a)->t);
      59         1997 :     oidKEY     *ib = (oidKEY *) (((const Nsrt *) b)->t);
      60              : 
      61         1997 :     if (ia->lower == ib->lower)
      62              :     {
      63            0 :         if (ia->upper == ib->upper)
      64            0 :             return 0;
      65              : 
      66            0 :         return (ia->upper > ib->upper) ? 1 : -1;
      67              :     }
      68              : 
      69         1997 :     return (ia->lower > ib->lower) ? 1 : -1;
      70              : }
      71              : 
      72              : static float8
      73            0 : gbt_oid_dist(const void *a, const void *b, FmgrInfo *flinfo)
      74              : {
      75            0 :     Oid         aa = *(const Oid *) a;
      76            0 :     Oid         bb = *(const Oid *) b;
      77              : 
      78            0 :     if (aa < bb)
      79            0 :         return (float8) (bb - aa);
      80              :     else
      81            0 :         return (float8) (aa - bb);
      82              : }
      83              : 
      84              : 
      85              : static const gbtree_ninfo tinfo =
      86              : {
      87              :     gbt_t_oid,
      88              :     sizeof(Oid),
      89              :     8,                          /* sizeof(gbtreekey8) */
      90              :     gbt_oidgt,
      91              :     gbt_oidge,
      92              :     gbt_oideq,
      93              :     gbt_oidle,
      94              :     gbt_oidlt,
      95              :     gbt_oidkey_cmp,
      96              :     gbt_oid_dist
      97              : };
      98              : 
      99              : 
     100            3 : PG_FUNCTION_INFO_V1(oid_dist);
     101              : Datum
     102            0 : oid_dist(PG_FUNCTION_ARGS)
     103              : {
     104            0 :     Oid         a = PG_GETARG_OID(0);
     105            0 :     Oid         b = PG_GETARG_OID(1);
     106              :     Oid         res;
     107              : 
     108            0 :     if (a < b)
     109            0 :         res = b - a;
     110              :     else
     111            0 :         res = a - b;
     112            0 :     PG_RETURN_OID(res);
     113              : }
     114              : 
     115              : 
     116              : /**************************************************
     117              :  * GiST support functions
     118              :  **************************************************/
     119              : 
     120              : Datum
     121         1004 : gbt_oid_compress(PG_FUNCTION_ARGS)
     122              : {
     123         1004 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     124              : 
     125         1004 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     126              : }
     127              : 
     128              : Datum
     129            0 : gbt_oid_fetch(PG_FUNCTION_ARGS)
     130              : {
     131            0 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     132              : 
     133            0 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     134              : }
     135              : 
     136              : Datum
     137         2770 : gbt_oid_consistent(PG_FUNCTION_ARGS)
     138              : {
     139         2770 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     140         2770 :     Oid         query = PG_GETARG_OID(1);
     141         2770 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     142              : #ifdef NOT_USED
     143              :     Oid         subtype = PG_GETARG_OID(3);
     144              : #endif
     145         2770 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     146         2770 :     oidKEY     *kkk = (oidKEY *) DatumGetPointer(entry->key);
     147              :     GBT_NUMKEY_R key;
     148              : 
     149              :     /* All cases served by this function are exact */
     150         2770 :     *recheck = false;
     151              : 
     152         2770 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     153         2770 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     154              : 
     155         2770 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
     156              :                                       GIST_LEAF(entry), &tinfo, fcinfo->flinfo));
     157              : }
     158              : 
     159              : Datum
     160            0 : gbt_oid_distance(PG_FUNCTION_ARGS)
     161              : {
     162            0 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     163            0 :     Oid         query = PG_GETARG_OID(1);
     164              : #ifdef NOT_USED
     165              :     Oid         subtype = PG_GETARG_OID(3);
     166              : #endif
     167            0 :     oidKEY     *kkk = (oidKEY *) DatumGetPointer(entry->key);
     168              :     GBT_NUMKEY_R key;
     169              : 
     170            0 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     171            0 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     172              : 
     173            0 :     PG_RETURN_FLOAT8(gbt_num_distance(&key, &query, GIST_LEAF(entry),
     174              :                                       &tinfo, fcinfo->flinfo));
     175              : }
     176              : 
     177              : Datum
     178            0 : gbt_oid_union(PG_FUNCTION_ARGS)
     179              : {
     180            0 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     181            0 :     void       *out = palloc(sizeof(oidKEY));
     182              : 
     183            0 :     *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
     184            0 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     185              : }
     186              : 
     187              : Datum
     188            0 : gbt_oid_penalty(PG_FUNCTION_ARGS)
     189              : {
     190            0 :     oidKEY     *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     191            0 :     oidKEY     *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     192            0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     193              : 
     194            0 :     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     195              : 
     196            0 :     PG_RETURN_POINTER(result);
     197              : }
     198              : 
     199              : Datum
     200            3 : gbt_oid_picksplit(PG_FUNCTION_ARGS)
     201              : {
     202            3 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     203              :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     204              :                                         &tinfo, fcinfo->flinfo));
     205              : }
     206              : 
     207              : Datum
     208            0 : gbt_oid_same(PG_FUNCTION_ARGS)
     209              : {
     210            0 :     oidKEY     *b1 = (oidKEY *) PG_GETARG_POINTER(0);
     211            0 :     oidKEY     *b2 = (oidKEY *) PG_GETARG_POINTER(1);
     212            0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     213              : 
     214            0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     215            0 :     PG_RETURN_POINTER(result);
     216              : }
     217              : 
     218              : static int
     219          999 : gbt_oid_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     220              : {
     221          999 :     oidKEY     *arg1 = (oidKEY *) DatumGetPointer(x);
     222          999 :     oidKEY     *arg2 = (oidKEY *) DatumGetPointer(y);
     223              : 
     224              :     /* for leaf items we expect lower == upper, so only compare lower */
     225          999 :     if (arg1->lower > arg2->lower)
     226            0 :         return 1;
     227          999 :     else if (arg1->lower < arg2->lower)
     228          999 :         return -1;
     229              :     else
     230            0 :         return 0;
     231              : }
     232              : 
     233              : Datum
     234            1 : gbt_oid_sortsupport(PG_FUNCTION_ARGS)
     235              : {
     236            1 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     237              : 
     238            1 :     ssup->comparator = gbt_oid_ssup_cmp;
     239            1 :     ssup->ssup_extra = NULL;
     240              : 
     241            1 :     PG_RETURN_VOID();
     242              : }
        

Generated by: LCOV version 2.0-1