LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_enum.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 59 75 78.7 %
Date: 2025-04-24 12:15:10 Functions: 20 23 87.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/btree_gist/btree_enum.c
       3             :  */
       4             : #include "postgres.h"
       5             : 
       6             : #include "btree_gist.h"
       7             : #include "btree_utils_num.h"
       8             : #include "fmgr.h"
       9             : #include "utils/fmgrprotos.h"
      10             : #include "utils/fmgroids.h"
      11             : #include "utils/sortsupport.h"
      12             : 
      13             : /* enums are really Oids, so we just use the same structure */
      14             : 
      15             : typedef struct
      16             : {
      17             :     Oid         lower;
      18             :     Oid         upper;
      19             : } oidKEY;
      20             : 
      21             : /* GiST support functions */
      22           4 : PG_FUNCTION_INFO_V1(gbt_enum_compress);
      23           4 : PG_FUNCTION_INFO_V1(gbt_enum_fetch);
      24           4 : PG_FUNCTION_INFO_V1(gbt_enum_union);
      25           4 : PG_FUNCTION_INFO_V1(gbt_enum_picksplit);
      26           4 : PG_FUNCTION_INFO_V1(gbt_enum_consistent);
      27           4 : PG_FUNCTION_INFO_V1(gbt_enum_penalty);
      28           4 : PG_FUNCTION_INFO_V1(gbt_enum_same);
      29           4 : PG_FUNCTION_INFO_V1(gbt_enum_sortsupport);
      30             : 
      31             : 
      32             : static bool
      33        3186 : gbt_enumgt(const void *a, const void *b, FmgrInfo *flinfo)
      34             : {
      35        3186 :     return DatumGetBool(CallerFInfoFunctionCall2(enum_gt, flinfo, InvalidOid,
      36             :                                                  ObjectIdGetDatum(*((const Oid *) a)),
      37             :                                                  ObjectIdGetDatum(*((const Oid *) b))));
      38             : }
      39             : static bool
      40        1072 : gbt_enumge(const void *a, const void *b, FmgrInfo *flinfo)
      41             : {
      42        1072 :     return DatumGetBool(CallerFInfoFunctionCall2(enum_ge, flinfo, InvalidOid,
      43             :                                                  ObjectIdGetDatum(*((const Oid *) a)),
      44             :                                                  ObjectIdGetDatum(*((const Oid *) b))));
      45             : }
      46             : static bool
      47        1064 : gbt_enumeq(const void *a, const void *b, FmgrInfo *flinfo)
      48             : {
      49        1064 :     return (*((const Oid *) a) == *((const Oid *) b));
      50             : }
      51             : static bool
      52        1080 : gbt_enumle(const void *a, const void *b, FmgrInfo *flinfo)
      53             : {
      54        1080 :     return DatumGetBool(CallerFInfoFunctionCall2(enum_le, flinfo, InvalidOid,
      55             :                                                  ObjectIdGetDatum(*((const Oid *) a)),
      56             :                                                  ObjectIdGetDatum(*((const Oid *) b))));
      57             : }
      58             : static bool
      59        3186 : gbt_enumlt(const void *a, const void *b, FmgrInfo *flinfo)
      60             : {
      61        3186 :     return DatumGetBool(CallerFInfoFunctionCall2(enum_lt, flinfo, InvalidOid,
      62             :                                                  ObjectIdGetDatum(*((const Oid *) a)),
      63             :                                                  ObjectIdGetDatum(*((const Oid *) b))));
      64             : }
      65             : 
      66             : static int
      67        1062 : gbt_enumkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      68             : {
      69        1062 :     oidKEY     *ia = (oidKEY *) (((const Nsrt *) a)->t);
      70        1062 :     oidKEY     *ib = (oidKEY *) (((const Nsrt *) b)->t);
      71             : 
      72        1062 :     if (ia->lower == ib->lower)
      73             :     {
      74        1050 :         if (ia->upper == ib->upper)
      75        1050 :             return 0;
      76             : 
      77           0 :         return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, flinfo, InvalidOid,
      78             :                                                       ObjectIdGetDatum(ia->upper),
      79             :                                                       ObjectIdGetDatum(ib->upper)));
      80             :     }
      81             : 
      82          12 :     return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp, flinfo, InvalidOid,
      83             :                                                   ObjectIdGetDatum(ia->lower),
      84             :                                                   ObjectIdGetDatum(ib->lower)));
      85             : }
      86             : 
      87             : static const gbtree_ninfo tinfo =
      88             : {
      89             :     gbt_t_enum,
      90             :     sizeof(Oid),
      91             :     8,                          /* sizeof(gbtreekey8) */
      92             :     gbt_enumgt,
      93             :     gbt_enumge,
      94             :     gbt_enumeq,
      95             :     gbt_enumle,
      96             :     gbt_enumlt,
      97             :     gbt_enumkey_cmp,
      98             :     NULL                        /* no KNN support at least for now */
      99             : };
     100             : 
     101             : 
     102             : /**************************************************
     103             :  * GiST support functions
     104             :  **************************************************/
     105             : 
     106             : Datum
     107        1068 : gbt_enum_compress(PG_FUNCTION_ARGS)
     108             : {
     109        1068 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     110             : 
     111        1068 :     PG_RETURN_POINTER(gbt_num_compress(entry, &tinfo));
     112             : }
     113             : 
     114             : Datum
     115           0 : gbt_enum_fetch(PG_FUNCTION_ARGS)
     116             : {
     117           0 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     118             : 
     119           0 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     120             : }
     121             : 
     122             : Datum
     123        5340 : gbt_enum_consistent(PG_FUNCTION_ARGS)
     124             : {
     125        5340 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     126        5340 :     Oid         query = PG_GETARG_OID(1);
     127        5340 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     128             : 
     129             :     /* Oid      subtype = PG_GETARG_OID(3); */
     130        5340 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     131        5340 :     oidKEY     *kkk = (oidKEY *) DatumGetPointer(entry->key);
     132             :     GBT_NUMKEY_R key;
     133             : 
     134             :     /* All cases served by this function are exact */
     135        5340 :     *recheck = false;
     136             : 
     137        5340 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     138        5340 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     139             : 
     140        5340 :     PG_RETURN_BOOL(gbt_num_consistent(&key, &query, &strategy,
     141             :                                       GIST_LEAF(entry), &tinfo,
     142             :                                       fcinfo->flinfo));
     143             : }
     144             : 
     145             : Datum
     146           2 : gbt_enum_union(PG_FUNCTION_ARGS)
     147             : {
     148           2 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     149           2 :     void       *out = palloc(sizeof(oidKEY));
     150             : 
     151           2 :     *(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
     152           2 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     153             : }
     154             : 
     155             : Datum
     156           0 : gbt_enum_penalty(PG_FUNCTION_ARGS)
     157             : {
     158           0 :     oidKEY     *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     159           0 :     oidKEY     *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     160           0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     161             : 
     162           0 :     penalty_num(result, origentry->lower, origentry->upper, newentry->lower, newentry->upper);
     163             : 
     164           0 :     PG_RETURN_POINTER(result);
     165             : }
     166             : 
     167             : Datum
     168           2 : gbt_enum_picksplit(PG_FUNCTION_ARGS)
     169             : {
     170           2 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     171             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     172             :                                         &tinfo, fcinfo->flinfo));
     173             : }
     174             : 
     175             : Datum
     176           0 : gbt_enum_same(PG_FUNCTION_ARGS)
     177             : {
     178           0 :     oidKEY     *b1 = (oidKEY *) PG_GETARG_POINTER(0);
     179           0 :     oidKEY     *b2 = (oidKEY *) PG_GETARG_POINTER(1);
     180           0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     181             : 
     182           0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     183           0 :     PG_RETURN_POINTER(result);
     184             : }
     185             : 
     186             : static int
     187        9856 : gbt_enum_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     188             : {
     189        9856 :     oidKEY     *arg1 = (oidKEY *) DatumGetPointer(x);
     190        9856 :     oidKEY     *arg2 = (oidKEY *) DatumGetPointer(y);
     191             : 
     192             :     /* for leaf items we expect lower == upper, so only compare lower */
     193        9856 :     return DatumGetInt32(CallerFInfoFunctionCall2(enum_cmp,
     194        9856 :                                                   ssup->ssup_extra,
     195             :                                                   InvalidOid,
     196        9856 :                                                   arg1->lower,
     197        9856 :                                                   arg2->lower));
     198             : }
     199             : 
     200             : Datum
     201           2 : gbt_enum_sortsupport(PG_FUNCTION_ARGS)
     202             : {
     203           2 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     204             :     FmgrInfo   *flinfo;
     205             : 
     206           2 :     ssup->comparator = gbt_enum_ssup_cmp;
     207             : 
     208             :     /*
     209             :      * Since gbt_enum_ssup_cmp() uses enum_cmp() like the rest of the
     210             :      * comparison functions, it also needs to pass flinfo when calling it. The
     211             :      * caller to a SortSupport comparison function doesn't provide an FmgrInfo
     212             :      * struct, so look it up now, save it in ssup_extra and use it in
     213             :      * gbt_enum_ssup_cmp() later.
     214             :      */
     215           2 :     flinfo = MemoryContextAlloc(ssup->ssup_cxt, sizeof(FmgrInfo));
     216           2 :     fmgr_info_cxt(F_ENUM_CMP, flinfo, ssup->ssup_cxt);
     217           2 :     ssup->ssup_extra = flinfo;
     218             : 
     219           2 :     PG_RETURN_VOID();
     220             : }

Generated by: LCOV version 1.14