LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_utils_var.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 236 284 83.1 %
Date: 2026-02-10 00:18:57 Functions: 19 20 95.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/btree_gist/btree_utils_var.c
       3             :  */
       4             : #include "postgres.h"
       5             : 
       6             : #include <limits.h>
       7             : #include <float.h>
       8             : 
       9             : #include "btree_gist.h"
      10             : #include "btree_utils_var.h"
      11             : #include "mb/pg_wchar.h"
      12             : #include "utils/rel.h"
      13             : #include "varatt.h"
      14             : 
      15             : /* used for key sorting */
      16             : typedef struct
      17             : {
      18             :     int         i;
      19             :     GBT_VARKEY *t;
      20             : } Vsrt;
      21             : 
      22             : typedef struct
      23             : {
      24             :     const gbtree_vinfo *tinfo;
      25             :     Oid         collation;
      26             :     FmgrInfo   *flinfo;
      27             : } gbt_vsrt_arg;
      28             : 
      29             : 
      30          22 : PG_FUNCTION_INFO_V1(gbt_var_decompress);
      31          22 : PG_FUNCTION_INFO_V1(gbt_var_fetch);
      32             : 
      33             : 
      34             : Datum
      35      129034 : gbt_var_decompress(PG_FUNCTION_ARGS)
      36             : {
      37      129034 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      38      129034 :     GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
      39             : 
      40      129034 :     if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
      41             :     {
      42      128974 :         GISTENTRY  *retval = palloc_object(GISTENTRY);
      43             : 
      44      128974 :         gistentryinit(*retval, PointerGetDatum(key),
      45             :                       entry->rel, entry->page,
      46             :                       entry->offset, false);
      47             : 
      48      128974 :         PG_RETURN_POINTER(retval);
      49             :     }
      50             : 
      51          60 :     PG_RETURN_POINTER(entry);
      52             : }
      53             : 
      54             : /* Returns a better readable representation of variable key ( sets pointer ) */
      55             : GBT_VARKEY_R
      56      613426 : gbt_var_key_readable(const GBT_VARKEY *k)
      57             : {
      58             :     GBT_VARKEY_R r;
      59             : 
      60      613426 :     r.lower = (bytea *) &(((char *) k)[VARHDRSZ]);
      61      613426 :     if (VARSIZE(k) > (VARHDRSZ + (VARSIZE(r.lower))))
      62      132462 :         r.upper = (bytea *) &(((char *) k)[VARHDRSZ + INTALIGN(VARSIZE(r.lower))]);
      63             :     else
      64      480964 :         r.upper = r.lower;
      65      613426 :     return r;
      66             : }
      67             : 
      68             : 
      69             : /*
      70             :  * Create a leaf-entry to store in the index, from a single Datum.
      71             :  */
      72             : static GBT_VARKEY *
      73       16524 : gbt_var_key_from_datum(const struct varlena *u)
      74             : {
      75       16524 :     int32       lowersize = VARSIZE(u);
      76             :     GBT_VARKEY *r;
      77             : 
      78       16524 :     r = (GBT_VARKEY *) palloc(lowersize + VARHDRSZ);
      79       16524 :     memcpy(VARDATA(r), u, lowersize);
      80       16524 :     SET_VARSIZE(r, lowersize + VARHDRSZ);
      81             : 
      82       16524 :     return r;
      83             : }
      84             : 
      85             : /*
      86             :  * Create an entry to store in the index, from lower and upper bound.
      87             :  */
      88             : GBT_VARKEY *
      89       43612 : gbt_var_key_copy(const GBT_VARKEY_R *u)
      90             : {
      91       43612 :     int32       lowersize = VARSIZE(u->lower);
      92       43612 :     int32       uppersize = VARSIZE(u->upper);
      93             :     GBT_VARKEY *r;
      94             : 
      95       43612 :     r = (GBT_VARKEY *) palloc0(INTALIGN(lowersize) + uppersize + VARHDRSZ);
      96       43612 :     memcpy(VARDATA(r), u->lower, lowersize);
      97       43612 :     memcpy(VARDATA(r) + INTALIGN(lowersize), u->upper, uppersize);
      98       43612 :     SET_VARSIZE(r, INTALIGN(lowersize) + uppersize + VARHDRSZ);
      99             : 
     100       43612 :     return r;
     101             : }
     102             : 
     103             : 
     104             : static GBT_VARKEY *
     105       94080 : gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     106             : {
     107       94080 :     GBT_VARKEY *out = leaf;
     108             : 
     109       94080 :     if (tinfo->f_l2n)
     110        7196 :         out = tinfo->f_l2n(leaf, flinfo);
     111             : 
     112       94080 :     return out;
     113             : }
     114             : 
     115             : 
     116             : /*
     117             :  * returns the common prefix length of a node key
     118             :  *
     119             :  * If the underlying type is character data, the prefix length may point in
     120             :  * the middle of a multibyte character.
     121             : */
     122             : static int32
     123          50 : gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
     124             : {
     125          50 :     GBT_VARKEY_R r = gbt_var_key_readable(node);
     126          50 :     int32       i = 0;
     127          50 :     int32       l_left_to_match = 0;
     128          50 :     int32       l_total = 0;
     129          50 :     int32       t1len = VARSIZE(r.lower) - VARHDRSZ;
     130          50 :     int32       t2len = VARSIZE(r.upper) - VARHDRSZ;
     131          50 :     int32       ml = Min(t1len, t2len);
     132          50 :     char       *p1 = VARDATA(r.lower);
     133          50 :     char       *p2 = VARDATA(r.upper);
     134          50 :     const char *end1 = p1 + t1len;
     135          50 :     const char *end2 = p2 + t2len;
     136             : 
     137          50 :     if (ml == 0)
     138           8 :         return 0;
     139             : 
     140          42 :     while (i < ml)
     141             :     {
     142          42 :         if (tinfo->eml > 1 && l_left_to_match == 0)
     143             :         {
     144           0 :             l_total = pg_mblen_range(p1, end1);
     145           0 :             if (l_total != pg_mblen_range(p2, end2))
     146             :             {
     147           0 :                 return i;
     148             :             }
     149           0 :             l_left_to_match = l_total;
     150             :         }
     151          42 :         if (*p1 != *p2)
     152             :         {
     153          42 :             if (tinfo->eml > 1)
     154             :             {
     155           0 :                 int32       l_matched_subset = l_total - l_left_to_match;
     156             : 
     157             :                 /* end common prefix at final byte of last matching char */
     158           0 :                 return i - l_matched_subset;
     159             :             }
     160             :             else
     161             :             {
     162          42 :                 return i;
     163             :             }
     164             :         }
     165             : 
     166           0 :         p1++;
     167           0 :         p2++;
     168           0 :         l_left_to_match--;
     169           0 :         i++;
     170             :     }
     171           0 :     return ml;                  /* lower == upper */
     172             : }
     173             : 
     174             : 
     175             : /*
     176             :  * returns true, if query matches prefix ( common prefix )
     177             :  */
     178             : static bool
     179         156 : gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
     180             : {
     181         156 :     bool        out = false;
     182         156 :     int32       qlen = VARSIZE(query) - VARHDRSZ;
     183         156 :     int32       nlen = VARSIZE(pf) - VARHDRSZ;
     184             : 
     185         156 :     if (nlen <= qlen)
     186             :     {
     187         156 :         char       *q = VARDATA(query);
     188         156 :         char       *n = VARDATA(pf);
     189             : 
     190         156 :         out = (memcmp(q, n, nlen) == 0);
     191             :     }
     192             : 
     193         156 :     return out;
     194             : }
     195             : 
     196             : 
     197             : /*
     198             :  * returns true, if query matches node using common prefix
     199             :  */
     200             : static bool
     201         344 : gbt_var_node_pf_match(const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
     202             : {
     203         500 :     return (tinfo->trnc &&
     204         156 :             (gbt_bytea_pf_match(node->lower, query, tinfo) ||
     205          68 :              gbt_bytea_pf_match(node->upper, query, tinfo)));
     206             : }
     207             : 
     208             : 
     209             : /*
     210             : *  truncates / compresses the node key
     211             : *  cpf_length .. common prefix length
     212             : */
     213             : static GBT_VARKEY *
     214          50 : gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
     215             : {
     216          50 :     GBT_VARKEY *out = NULL;
     217          50 :     GBT_VARKEY_R r = gbt_var_key_readable(node);
     218          50 :     int32       len1 = VARSIZE(r.lower) - VARHDRSZ;
     219          50 :     int32       len2 = VARSIZE(r.upper) - VARHDRSZ;
     220             :     int32       si;
     221             :     char       *out2;
     222             : 
     223          50 :     len1 = Min(len1, (cpf_length + 1));
     224          50 :     len2 = Min(len2, (cpf_length + 1));
     225             : 
     226          50 :     si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2;
     227          50 :     out = (GBT_VARKEY *) palloc0(si);
     228          50 :     SET_VARSIZE(out, si);
     229             : 
     230          50 :     memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ);
     231          50 :     SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ);
     232             : 
     233          50 :     out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ);
     234          50 :     memcpy(out2, r.upper, len2 + VARHDRSZ);
     235          50 :     SET_VARSIZE(out2, len2 + VARHDRSZ);
     236             : 
     237          50 :     return out;
     238             : }
     239             : 
     240             : 
     241             : 
     242             : void
     243       62850 : gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation,
     244             :                   const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     245             : {
     246       62850 :     GBT_VARKEY_R eo = gbt_var_key_readable(e);
     247             :     GBT_VARKEY_R nr;
     248             : 
     249       62850 :     if (eo.lower == eo.upper)   /* leaf */
     250             :     {
     251             :         GBT_VARKEY *tmp;
     252             : 
     253       58038 :         tmp = gbt_var_leaf2node(e, tinfo, flinfo);
     254       58038 :         if (tmp != e)
     255        2396 :             eo = gbt_var_key_readable(tmp);
     256             :     }
     257             : 
     258       62850 :     if (DatumGetPointer(*u))
     259             :     {
     260       62646 :         GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u));
     261       62646 :         bool        update = false;
     262             : 
     263       62646 :         nr.lower = ro.lower;
     264       62646 :         nr.upper = ro.upper;
     265             : 
     266       62646 :         if (tinfo->f_cmp(ro.lower, eo.lower, collation, flinfo) > 0)
     267             :         {
     268          32 :             nr.lower = eo.lower;
     269          32 :             update = true;
     270             :         }
     271             : 
     272       62646 :         if (tinfo->f_cmp(ro.upper, eo.upper, collation, flinfo) < 0)
     273             :         {
     274       25366 :             nr.upper = eo.upper;
     275       25366 :             update = true;
     276             :         }
     277             : 
     278       62646 :         if (update)
     279       25398 :             *u = PointerGetDatum(gbt_var_key_copy(&nr));
     280             :     }
     281             :     else
     282             :     {
     283         204 :         nr.lower = eo.lower;
     284         204 :         nr.upper = eo.upper;
     285         204 :         *u = PointerGetDatum(gbt_var_key_copy(&nr));
     286             :     }
     287       62850 : }
     288             : 
     289             : 
     290             : GISTENTRY *
     291       16686 : gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
     292             : {
     293             :     GISTENTRY  *retval;
     294             : 
     295       16686 :     if (entry->leafkey)
     296             :     {
     297       16524 :         struct varlena *leaf = PG_DETOAST_DATUM(entry->key);
     298             :         GBT_VARKEY *r;
     299             : 
     300       16524 :         r = gbt_var_key_from_datum(leaf);
     301             : 
     302       16524 :         retval = palloc_object(GISTENTRY);
     303       16524 :         gistentryinit(*retval, PointerGetDatum(r),
     304             :                       entry->rel, entry->page,
     305             :                       entry->offset, true);
     306             :     }
     307             :     else
     308         162 :         retval = entry;
     309             : 
     310       16686 :     return retval;
     311             : }
     312             : 
     313             : 
     314             : Datum
     315          12 : gbt_var_fetch(PG_FUNCTION_ARGS)
     316             : {
     317          12 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     318          12 :     GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
     319          12 :     GBT_VARKEY_R r = gbt_var_key_readable(key);
     320             :     GISTENTRY  *retval;
     321             : 
     322          12 :     retval = palloc_object(GISTENTRY);
     323          12 :     gistentryinit(*retval, PointerGetDatum(r.lower),
     324             :                   entry->rel, entry->page,
     325             :                   entry->offset, true);
     326             : 
     327          12 :     PG_RETURN_POINTER(retval);
     328             : }
     329             : 
     330             : 
     331             : GBT_VARKEY *
     332        3730 : gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation,
     333             :               const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     334             : {
     335        3730 :     int         i = 0,
     336        3730 :                 numranges = entryvec->n;
     337             :     GBT_VARKEY *cur;
     338             :     Datum       out;
     339             :     GBT_VARKEY_R rk;
     340             : 
     341        3730 :     *size = sizeof(GBT_VARKEY);
     342             : 
     343        3730 :     cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
     344        3730 :     rk = gbt_var_key_readable(cur);
     345        3730 :     out = PointerGetDatum(gbt_var_key_copy(&rk));
     346             : 
     347       23454 :     for (i = 1; i < numranges; i++)
     348             :     {
     349       19724 :         cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
     350       19724 :         gbt_var_bin_union(&out, cur, collation, tinfo, flinfo);
     351             :     }
     352             : 
     353             : 
     354             :     /* Truncate (=compress) key */
     355        3730 :     if (tinfo->trnc)
     356             :     {
     357             :         int32       plen;
     358           6 :         GBT_VARKEY *trc = NULL;
     359             : 
     360           6 :         plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
     361           6 :         trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);
     362             : 
     363           6 :         out = PointerGetDatum(trc);
     364             :     }
     365             : 
     366        3730 :     return ((GBT_VARKEY *) DatumGetPointer(out));
     367             : }
     368             : 
     369             : 
     370             : bool
     371        3628 : gbt_var_same(Datum d1, Datum d2, Oid collation,
     372             :              const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     373             : {
     374        3628 :     GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1);
     375        3628 :     GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2);
     376             :     GBT_VARKEY_R r1,
     377             :                 r2;
     378             : 
     379        3628 :     r1 = gbt_var_key_readable(t1);
     380        3628 :     r2 = gbt_var_key_readable(t2);
     381             : 
     382        7256 :     return (tinfo->f_cmp(r1.lower, r2.lower, collation, flinfo) == 0 &&
     383        3628 :             tinfo->f_cmp(r1.upper, r2.upper, collation, flinfo) == 0);
     384             : }
     385             : 
     386             : 
     387             : float *
     388           0 : gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
     389             :                 Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     390             : {
     391           0 :     GBT_VARKEY *orge = (GBT_VARKEY *) DatumGetPointer(o->key);
     392           0 :     GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
     393             :     GBT_VARKEY_R ok,
     394             :                 nk;
     395             : 
     396           0 :     *res = 0.0;
     397             : 
     398           0 :     nk = gbt_var_key_readable(newe);
     399           0 :     if (nk.lower == nk.upper)   /* leaf */
     400             :     {
     401             :         GBT_VARKEY *tmp;
     402             : 
     403           0 :         tmp = gbt_var_leaf2node(newe, tinfo, flinfo);
     404           0 :         if (tmp != newe)
     405           0 :             nk = gbt_var_key_readable(tmp);
     406             :     }
     407           0 :     ok = gbt_var_key_readable(orge);
     408             : 
     409           0 :     if ((VARSIZE(ok.lower) - VARHDRSZ) == 0 && (VARSIZE(ok.upper) - VARHDRSZ) == 0)
     410           0 :         *res = 0.0;
     411           0 :     else if (!((tinfo->f_cmp(nk.lower, ok.lower, collation, flinfo) >= 0 ||
     412           0 :                 gbt_bytea_pf_match(ok.lower, nk.lower, tinfo)) &&
     413           0 :                (tinfo->f_cmp(nk.upper, ok.upper, collation, flinfo) <= 0 ||
     414           0 :                 gbt_bytea_pf_match(ok.upper, nk.upper, tinfo))))
     415             :     {
     416           0 :         Datum       d = PointerGetDatum(0);
     417             :         double      dres;
     418             :         int32       ol,
     419             :                     ul;
     420             : 
     421           0 :         gbt_var_bin_union(&d, orge, collation, tinfo, flinfo);
     422           0 :         ol = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
     423           0 :         gbt_var_bin_union(&d, newe, collation, tinfo, flinfo);
     424           0 :         ul = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
     425             : 
     426           0 :         if (ul < ol)
     427             :         {
     428           0 :             dres = (ol - ul);   /* reduction of common prefix len */
     429             :         }
     430             :         else
     431             :         {
     432           0 :             GBT_VARKEY_R uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(d));
     433             :             unsigned char tmp[4];
     434             : 
     435           0 :             tmp[0] = (unsigned char) (((VARSIZE(ok.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.lower)[ul]));
     436           0 :             tmp[1] = (unsigned char) (((VARSIZE(uk.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.lower)[ul]));
     437           0 :             tmp[2] = (unsigned char) (((VARSIZE(ok.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.upper)[ul]));
     438           0 :             tmp[3] = (unsigned char) (((VARSIZE(uk.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.upper)[ul]));
     439           0 :             dres = abs(tmp[0] - tmp[1]) + abs(tmp[3] - tmp[2]);
     440           0 :             dres /= 256.0;
     441             :         }
     442             : 
     443           0 :         *res += FLT_MIN;
     444           0 :         *res += (float) (dres / ((double) (ol + 1)));
     445           0 :         *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
     446             :     }
     447             : 
     448           0 :     return res;
     449             : }
     450             : 
     451             : 
     452             : static int
     453       46032 : gbt_vsrt_cmp(const void *a, const void *b, void *arg)
     454             : {
     455       46032 :     GBT_VARKEY_R ar = gbt_var_key_readable(((const Vsrt *) a)->t);
     456       46032 :     GBT_VARKEY_R br = gbt_var_key_readable(((const Vsrt *) b)->t);
     457       46032 :     const gbt_vsrt_arg *varg = (const gbt_vsrt_arg *) arg;
     458             :     int         res;
     459             : 
     460       46032 :     res = varg->tinfo->f_cmp(ar.lower, br.lower, varg->collation, varg->flinfo);
     461       46032 :     if (res == 0)
     462       15266 :         return varg->tinfo->f_cmp(ar.upper, br.upper, varg->collation, varg->flinfo);
     463             : 
     464       30766 :     return res;
     465             : }
     466             : 
     467             : GIST_SPLITVEC *
     468         102 : gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
     469             :                   Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     470             : {
     471             :     OffsetNumber i,
     472         102 :                 maxoff = entryvec->n - 1;
     473             :     Vsrt       *arr;
     474         102 :     int         svcntr = 0,
     475             :                 nbytes;
     476             :     char       *cur;
     477         102 :     GBT_VARKEY **sv = NULL;
     478             :     gbt_vsrt_arg varg;
     479             : 
     480         102 :     arr = palloc_array(Vsrt, maxoff + 1);
     481         102 :     nbytes = (maxoff + 2) * sizeof(OffsetNumber);
     482         102 :     v->spl_left = (OffsetNumber *) palloc(nbytes);
     483         102 :     v->spl_right = (OffsetNumber *) palloc(nbytes);
     484         102 :     v->spl_ldatum = PointerGetDatum(0);
     485         102 :     v->spl_rdatum = PointerGetDatum(0);
     486         102 :     v->spl_nleft = 0;
     487         102 :     v->spl_nright = 0;
     488             : 
     489         102 :     sv = palloc_array(GBT_VARKEY *, maxoff + 1);
     490             : 
     491             :     /* Sort entries */
     492             : 
     493       36144 :     for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
     494             :     {
     495             :         GBT_VARKEY_R ro;
     496             : 
     497       36042 :         cur = (char *) DatumGetPointer(entryvec->vector[i].key);
     498       36042 :         ro = gbt_var_key_readable((GBT_VARKEY *) cur);
     499       36042 :         if (ro.lower == ro.upper)   /* leaf */
     500             :         {
     501       36042 :             sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo, flinfo);
     502       36042 :             arr[i].t = sv[svcntr];
     503       36042 :             if (sv[svcntr] != (GBT_VARKEY *) cur)
     504        4800 :                 svcntr++;
     505             :         }
     506             :         else
     507           0 :             arr[i].t = (GBT_VARKEY *) cur;
     508       36042 :         arr[i].i = i;
     509             :     }
     510             : 
     511             :     /* sort */
     512         102 :     varg.tinfo = tinfo;
     513         102 :     varg.collation = collation;
     514         102 :     varg.flinfo = flinfo;
     515         102 :     qsort_arg(&arr[FirstOffsetNumber],
     516             :               maxoff - FirstOffsetNumber + 1,
     517             :               sizeof(Vsrt),
     518             :               gbt_vsrt_cmp,
     519             :               &varg);
     520             : 
     521             :     /* We do simply create two parts */
     522             : 
     523       36144 :     for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
     524             :     {
     525       36042 :         if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
     526             :         {
     527       18010 :             gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo, flinfo);
     528       18010 :             v->spl_left[v->spl_nleft] = arr[i].i;
     529       18010 :             v->spl_nleft++;
     530             :         }
     531             :         else
     532             :         {
     533       18032 :             gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo, flinfo);
     534       18032 :             v->spl_right[v->spl_nright] = arr[i].i;
     535       18032 :             v->spl_nright++;
     536             :         }
     537             :     }
     538             : 
     539             :     /* Truncate (=compress) key */
     540         102 :     if (tinfo->trnc)
     541             :     {
     542          22 :         int32       ll = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), tinfo);
     543          22 :         int32       lr = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), tinfo);
     544             :         GBT_VARKEY *dl;
     545             :         GBT_VARKEY *dr;
     546             : 
     547          22 :         ll = Max(ll, lr);
     548          22 :         ll++;
     549             : 
     550          22 :         dl = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), ll, tinfo);
     551          22 :         dr = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), ll, tinfo);
     552          22 :         v->spl_ldatum = PointerGetDatum(dl);
     553          22 :         v->spl_rdatum = PointerGetDatum(dr);
     554             :     }
     555             : 
     556         102 :     return v;
     557             : }
     558             : 
     559             : 
     560             : /*
     561             :  * The GiST consistent method
     562             :  */
     563             : bool
     564       49782 : gbt_var_consistent(GBT_VARKEY_R *key,
     565             :                    const void *query,
     566             :                    StrategyNumber strategy,
     567             :                    Oid collation,
     568             :                    bool is_leaf,
     569             :                    const gbtree_vinfo *tinfo,
     570             :                    FmgrInfo *flinfo)
     571             : {
     572       49782 :     bool        retval = false;
     573             : 
     574       49782 :     switch (strategy)
     575             :     {
     576       11406 :         case BTLessEqualStrategyNumber:
     577       11406 :             if (is_leaf)
     578       11268 :                 retval = tinfo->f_ge(query, key->lower, collation, flinfo);
     579             :             else
     580         276 :                 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
     581         138 :                     || gbt_var_node_pf_match(key, query, tinfo);
     582       11406 :             break;
     583       11220 :         case BTLessStrategyNumber:
     584       11220 :             if (is_leaf)
     585       11110 :                 retval = tinfo->f_gt(query, key->lower, collation, flinfo);
     586             :             else
     587         220 :                 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
     588         110 :                     || gbt_var_node_pf_match(key, query, tinfo);
     589       11220 :             break;
     590        5378 :         case BTEqualStrategyNumber:
     591        5378 :             if (is_leaf)
     592        5244 :                 retval = tinfo->f_eq(query, key->lower, collation, flinfo);
     593             :             else
     594         134 :                 retval =
     595         210 :                     (tinfo->f_cmp(key->lower, query, collation, flinfo) <= 0 &&
     596         268 :                      tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0) ||
     597         112 :                     gbt_var_node_pf_match(key, query, tinfo);
     598        5378 :             break;
     599       10932 :         case BTGreaterStrategyNumber:
     600       10932 :             if (is_leaf)
     601       10810 :                 retval = tinfo->f_lt(query, key->upper, collation, flinfo);
     602             :             else
     603         244 :                 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
     604         122 :                     || gbt_var_node_pf_match(key, query, tinfo);
     605       10932 :             break;
     606       10824 :         case BTGreaterEqualStrategyNumber:
     607       10824 :             if (is_leaf)
     608       10672 :                 retval = tinfo->f_le(query, key->upper, collation, flinfo);
     609             :             else
     610         304 :                 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
     611         152 :                     || gbt_var_node_pf_match(key, query, tinfo);
     612       10824 :             break;
     613          22 :         case BtreeGistNotEqualStrategyNumber:
     614          34 :             retval = !(tinfo->f_eq(query, key->lower, collation, flinfo) &&
     615          12 :                        tinfo->f_eq(query, key->upper, collation, flinfo));
     616          22 :             break;
     617           0 :         default:
     618           0 :             retval = false;
     619             :     }
     620             : 
     621       49782 :     return retval;
     622             : }

Generated by: LCOV version 1.16