LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_utils_var.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 83.1 % 284 236
Test Date: 2026-03-03 11:15:01 Functions: 95.0 % 20 19
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           11 : PG_FUNCTION_INFO_V1(gbt_var_decompress);
      31           11 : PG_FUNCTION_INFO_V1(gbt_var_fetch);
      32              : 
      33              : 
      34              : Datum
      35        64526 : gbt_var_decompress(PG_FUNCTION_ARGS)
      36              : {
      37        64526 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      38        64526 :     GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
      39              : 
      40        64526 :     if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
      41              :     {
      42        64496 :         GISTENTRY  *retval = palloc_object(GISTENTRY);
      43              : 
      44        64496 :         gistentryinit(*retval, PointerGetDatum(key),
      45              :                       entry->rel, entry->page,
      46              :                       entry->offset, false);
      47              : 
      48        64496 :         PG_RETURN_POINTER(retval);
      49              :     }
      50              : 
      51           30 :     PG_RETURN_POINTER(entry);
      52              : }
      53              : 
      54              : /* Returns a better readable representation of variable key ( sets pointer ) */
      55              : GBT_VARKEY_R
      56       306758 : gbt_var_key_readable(const GBT_VARKEY *k)
      57              : {
      58              :     GBT_VARKEY_R r;
      59              : 
      60       306758 :     r.lower = (bytea *) &(((char *) k)[VARHDRSZ]);
      61       306758 :     if (VARSIZE(k) > (VARHDRSZ + (VARSIZE(r.lower))))
      62        66267 :         r.upper = (bytea *) &(((char *) k)[VARHDRSZ + INTALIGN(VARSIZE(r.lower))]);
      63              :     else
      64       240491 :         r.upper = r.lower;
      65       306758 :     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         8262 : gbt_var_key_from_datum(const varlena *u)
      74              : {
      75         8262 :     int32       lowersize = VARSIZE(u);
      76              :     GBT_VARKEY *r;
      77              : 
      78         8262 :     r = (GBT_VARKEY *) palloc(lowersize + VARHDRSZ);
      79         8262 :     memcpy(VARDATA(r), u, lowersize);
      80         8262 :     SET_VARSIZE(r, lowersize + VARHDRSZ);
      81              : 
      82         8262 :     return r;
      83              : }
      84              : 
      85              : /*
      86              :  * Create an entry to store in the index, from lower and upper bound.
      87              :  */
      88              : GBT_VARKEY *
      89        21815 : gbt_var_key_copy(const GBT_VARKEY_R *u)
      90              : {
      91        21815 :     int32       lowersize = VARSIZE(u->lower);
      92        21815 :     int32       uppersize = VARSIZE(u->upper);
      93              :     GBT_VARKEY *r;
      94              : 
      95        21815 :     r = (GBT_VARKEY *) palloc0(INTALIGN(lowersize) + uppersize + VARHDRSZ);
      96        21815 :     memcpy(VARDATA(r), u->lower, lowersize);
      97        21815 :     memcpy(VARDATA(r) + INTALIGN(lowersize), u->upper, uppersize);
      98        21815 :     SET_VARSIZE(r, INTALIGN(lowersize) + uppersize + VARHDRSZ);
      99              : 
     100        21815 :     return r;
     101              : }
     102              : 
     103              : 
     104              : static GBT_VARKEY *
     105        47049 : gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     106              : {
     107        47049 :     GBT_VARKEY *out = leaf;
     108              : 
     109        47049 :     if (tinfo->f_l2n)
     110         3598 :         out = tinfo->f_l2n(leaf, flinfo);
     111              : 
     112        47049 :     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           25 : gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
     124              : {
     125           25 :     GBT_VARKEY_R r = gbt_var_key_readable(node);
     126           25 :     int32       i = 0;
     127           25 :     int32       l_left_to_match = 0;
     128           25 :     int32       l_total = 0;
     129           25 :     int32       t1len = VARSIZE(r.lower) - VARHDRSZ;
     130           25 :     int32       t2len = VARSIZE(r.upper) - VARHDRSZ;
     131           25 :     int32       ml = Min(t1len, t2len);
     132           25 :     char       *p1 = VARDATA(r.lower);
     133           25 :     char       *p2 = VARDATA(r.upper);
     134           25 :     const char *end1 = p1 + t1len;
     135           25 :     const char *end2 = p2 + t2len;
     136              : 
     137           25 :     if (ml == 0)
     138            4 :         return 0;
     139              : 
     140           21 :     while (i < ml)
     141              :     {
     142           21 :         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           21 :         if (*p1 != *p2)
     152              :         {
     153           21 :             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           21 :                 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           78 : gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
     180              : {
     181           78 :     bool        out = false;
     182           78 :     int32       qlen = VARSIZE(query) - VARHDRSZ;
     183           78 :     int32       nlen = VARSIZE(pf) - VARHDRSZ;
     184              : 
     185           78 :     if (nlen <= qlen)
     186              :     {
     187           78 :         char       *q = VARDATA(query);
     188           78 :         char       *n = VARDATA(pf);
     189              : 
     190           78 :         out = (memcmp(q, n, nlen) == 0);
     191              :     }
     192              : 
     193           78 :     return out;
     194              : }
     195              : 
     196              : 
     197              : /*
     198              :  * returns true, if query matches node using common prefix
     199              :  */
     200              : static bool
     201          172 : gbt_var_node_pf_match(const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
     202              : {
     203          250 :     return (tinfo->trnc &&
     204           78 :             (gbt_bytea_pf_match(node->lower, query, tinfo) ||
     205           34 :              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           25 : gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
     215              : {
     216           25 :     GBT_VARKEY *out = NULL;
     217           25 :     GBT_VARKEY_R r = gbt_var_key_readable(node);
     218           25 :     int32       len1 = VARSIZE(r.lower) - VARHDRSZ;
     219           25 :     int32       len2 = VARSIZE(r.upper) - VARHDRSZ;
     220              :     int32       si;
     221              :     char       *out2;
     222              : 
     223           25 :     len1 = Min(len1, (cpf_length + 1));
     224           25 :     len2 = Min(len2, (cpf_length + 1));
     225              : 
     226           25 :     si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2;
     227           25 :     out = (GBT_VARKEY *) palloc0(si);
     228           25 :     SET_VARSIZE(out, si);
     229              : 
     230           25 :     memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ);
     231           25 :     SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ);
     232              : 
     233           25 :     out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ);
     234           25 :     memcpy(out2, r.upper, len2 + VARHDRSZ);
     235           25 :     SET_VARSIZE(out2, len2 + VARHDRSZ);
     236              : 
     237           25 :     return out;
     238              : }
     239              : 
     240              : 
     241              : 
     242              : void
     243        31434 : gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation,
     244              :                   const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     245              : {
     246        31434 :     GBT_VARKEY_R eo = gbt_var_key_readable(e);
     247              :     GBT_VARKEY_R nr;
     248              : 
     249        31434 :     if (eo.lower == eo.upper)   /* leaf */
     250              :     {
     251              :         GBT_VARKEY *tmp;
     252              : 
     253        29028 :         tmp = gbt_var_leaf2node(e, tinfo, flinfo);
     254        29028 :         if (tmp != e)
     255         1198 :             eo = gbt_var_key_readable(tmp);
     256              :     }
     257              : 
     258        31434 :     if (DatumGetPointer(*u))
     259              :     {
     260        31332 :         GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u));
     261        31332 :         bool        update = false;
     262              : 
     263        31332 :         nr.lower = ro.lower;
     264        31332 :         nr.upper = ro.upper;
     265              : 
     266        31332 :         if (tinfo->f_cmp(ro.lower, eo.lower, collation, flinfo) > 0)
     267              :         {
     268           16 :             nr.lower = eo.lower;
     269           16 :             update = true;
     270              :         }
     271              : 
     272        31332 :         if (tinfo->f_cmp(ro.upper, eo.upper, collation, flinfo) < 0)
     273              :         {
     274        12683 :             nr.upper = eo.upper;
     275        12683 :             update = true;
     276              :         }
     277              : 
     278        31332 :         if (update)
     279        12699 :             *u = PointerGetDatum(gbt_var_key_copy(&nr));
     280              :     }
     281              :     else
     282              :     {
     283          102 :         nr.lower = eo.lower;
     284          102 :         nr.upper = eo.upper;
     285          102 :         *u = PointerGetDatum(gbt_var_key_copy(&nr));
     286              :     }
     287        31434 : }
     288              : 
     289              : 
     290              : GISTENTRY *
     291         8343 : gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
     292              : {
     293              :     GISTENTRY  *retval;
     294              : 
     295         8343 :     if (entry->leafkey)
     296              :     {
     297         8262 :         varlena    *leaf = PG_DETOAST_DATUM(entry->key);
     298              :         GBT_VARKEY *r;
     299              : 
     300         8262 :         r = gbt_var_key_from_datum(leaf);
     301              : 
     302         8262 :         retval = palloc_object(GISTENTRY);
     303         8262 :         gistentryinit(*retval, PointerGetDatum(r),
     304              :                       entry->rel, entry->page,
     305              :                       entry->offset, true);
     306              :     }
     307              :     else
     308           81 :         retval = entry;
     309              : 
     310         8343 :     return retval;
     311              : }
     312              : 
     313              : 
     314              : Datum
     315            6 : gbt_var_fetch(PG_FUNCTION_ARGS)
     316              : {
     317            6 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     318            6 :     GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
     319            6 :     GBT_VARKEY_R r = gbt_var_key_readable(key);
     320              :     GISTENTRY  *retval;
     321              : 
     322            6 :     retval = palloc_object(GISTENTRY);
     323            6 :     gistentryinit(*retval, PointerGetDatum(r.lower),
     324              :                   entry->rel, entry->page,
     325              :                   entry->offset, true);
     326              : 
     327            6 :     PG_RETURN_POINTER(retval);
     328              : }
     329              : 
     330              : 
     331              : GBT_VARKEY *
     332         1865 : gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation,
     333              :               const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     334              : {
     335         1865 :     int         i = 0,
     336         1865 :                 numranges = entryvec->n;
     337              :     GBT_VARKEY *cur;
     338              :     Datum       out;
     339              :     GBT_VARKEY_R rk;
     340              : 
     341         1865 :     *size = sizeof(GBT_VARKEY);
     342              : 
     343         1865 :     cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
     344         1865 :     rk = gbt_var_key_readable(cur);
     345         1865 :     out = PointerGetDatum(gbt_var_key_copy(&rk));
     346              : 
     347        11727 :     for (i = 1; i < numranges; i++)
     348              :     {
     349         9862 :         cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
     350         9862 :         gbt_var_bin_union(&out, cur, collation, tinfo, flinfo);
     351              :     }
     352              : 
     353              : 
     354              :     /* Truncate (=compress) key */
     355         1865 :     if (tinfo->trnc)
     356              :     {
     357              :         int32       plen;
     358            3 :         GBT_VARKEY *trc = NULL;
     359              : 
     360            3 :         plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
     361            3 :         trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);
     362              : 
     363            3 :         out = PointerGetDatum(trc);
     364              :     }
     365              : 
     366         1865 :     return ((GBT_VARKEY *) DatumGetPointer(out));
     367              : }
     368              : 
     369              : 
     370              : bool
     371         1814 : gbt_var_same(Datum d1, Datum d2, Oid collation,
     372              :              const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     373              : {
     374         1814 :     GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1);
     375         1814 :     GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2);
     376              :     GBT_VARKEY_R r1,
     377              :                 r2;
     378              : 
     379         1814 :     r1 = gbt_var_key_readable(t1);
     380         1814 :     r2 = gbt_var_key_readable(t2);
     381              : 
     382         3628 :     return (tinfo->f_cmp(r1.lower, r2.lower, collation, flinfo) == 0 &&
     383         1814 :             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        23016 : gbt_vsrt_cmp(const void *a, const void *b, void *arg)
     454              : {
     455        23016 :     GBT_VARKEY_R ar = gbt_var_key_readable(((const Vsrt *) a)->t);
     456        23016 :     GBT_VARKEY_R br = gbt_var_key_readable(((const Vsrt *) b)->t);
     457        23016 :     const gbt_vsrt_arg *varg = (const gbt_vsrt_arg *) arg;
     458              :     int         res;
     459              : 
     460        23016 :     res = varg->tinfo->f_cmp(ar.lower, br.lower, varg->collation, varg->flinfo);
     461        23016 :     if (res == 0)
     462         7633 :         return varg->tinfo->f_cmp(ar.upper, br.upper, varg->collation, varg->flinfo);
     463              : 
     464        15383 :     return res;
     465              : }
     466              : 
     467              : GIST_SPLITVEC *
     468           51 : gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
     469              :                   Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
     470              : {
     471              :     OffsetNumber i,
     472           51 :                 maxoff = entryvec->n - 1;
     473              :     Vsrt       *arr;
     474           51 :     int         svcntr = 0,
     475              :                 nbytes;
     476              :     char       *cur;
     477           51 :     GBT_VARKEY **sv = NULL;
     478              :     gbt_vsrt_arg varg;
     479              : 
     480           51 :     arr = palloc_array(Vsrt, maxoff + 1);
     481           51 :     nbytes = (maxoff + 2) * sizeof(OffsetNumber);
     482           51 :     v->spl_left = (OffsetNumber *) palloc(nbytes);
     483           51 :     v->spl_right = (OffsetNumber *) palloc(nbytes);
     484           51 :     v->spl_ldatum = PointerGetDatum(0);
     485           51 :     v->spl_rdatum = PointerGetDatum(0);
     486           51 :     v->spl_nleft = 0;
     487           51 :     v->spl_nright = 0;
     488              : 
     489           51 :     sv = palloc_array(GBT_VARKEY *, maxoff + 1);
     490              : 
     491              :     /* Sort entries */
     492              : 
     493        18072 :     for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
     494              :     {
     495              :         GBT_VARKEY_R ro;
     496              : 
     497        18021 :         cur = (char *) DatumGetPointer(entryvec->vector[i].key);
     498        18021 :         ro = gbt_var_key_readable((GBT_VARKEY *) cur);
     499        18021 :         if (ro.lower == ro.upper)   /* leaf */
     500              :         {
     501        18021 :             sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo, flinfo);
     502        18021 :             arr[i].t = sv[svcntr];
     503        18021 :             if (sv[svcntr] != (GBT_VARKEY *) cur)
     504         2400 :                 svcntr++;
     505              :         }
     506              :         else
     507            0 :             arr[i].t = (GBT_VARKEY *) cur;
     508        18021 :         arr[i].i = i;
     509              :     }
     510              : 
     511              :     /* sort */
     512           51 :     varg.tinfo = tinfo;
     513           51 :     varg.collation = collation;
     514           51 :     varg.flinfo = flinfo;
     515           51 :     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        18072 :     for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
     524              :     {
     525        18021 :         if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
     526              :         {
     527         9005 :             gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo, flinfo);
     528         9005 :             v->spl_left[v->spl_nleft] = arr[i].i;
     529         9005 :             v->spl_nleft++;
     530              :         }
     531              :         else
     532              :         {
     533         9016 :             gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo, flinfo);
     534         9016 :             v->spl_right[v->spl_nright] = arr[i].i;
     535         9016 :             v->spl_nright++;
     536              :         }
     537              :     }
     538              : 
     539              :     /* Truncate (=compress) key */
     540           51 :     if (tinfo->trnc)
     541              :     {
     542           11 :         int32       ll = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), tinfo);
     543           11 :         int32       lr = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), tinfo);
     544              :         GBT_VARKEY *dl;
     545              :         GBT_VARKEY *dr;
     546              : 
     547           11 :         ll = Max(ll, lr);
     548           11 :         ll++;
     549              : 
     550           11 :         dl = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), ll, tinfo);
     551           11 :         dr = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), ll, tinfo);
     552           11 :         v->spl_ldatum = PointerGetDatum(dl);
     553           11 :         v->spl_rdatum = PointerGetDatum(dr);
     554              :     }
     555              : 
     556           51 :     return v;
     557              : }
     558              : 
     559              : 
     560              : /*
     561              :  * The GiST consistent method
     562              :  */
     563              : bool
     564        24891 : 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        24891 :     bool        retval = false;
     573              : 
     574        24891 :     switch (strategy)
     575              :     {
     576         5703 :         case BTLessEqualStrategyNumber:
     577         5703 :             if (is_leaf)
     578         5634 :                 retval = tinfo->f_ge(query, key->lower, collation, flinfo);
     579              :             else
     580          138 :                 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
     581           69 :                     || gbt_var_node_pf_match(key, query, tinfo);
     582         5703 :             break;
     583         5610 :         case BTLessStrategyNumber:
     584         5610 :             if (is_leaf)
     585         5555 :                 retval = tinfo->f_gt(query, key->lower, collation, flinfo);
     586              :             else
     587          110 :                 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
     588           55 :                     || gbt_var_node_pf_match(key, query, tinfo);
     589         5610 :             break;
     590         2689 :         case BTEqualStrategyNumber:
     591         2689 :             if (is_leaf)
     592         2622 :                 retval = tinfo->f_eq(query, key->lower, collation, flinfo);
     593              :             else
     594           67 :                 retval =
     595          105 :                     (tinfo->f_cmp(key->lower, query, collation, flinfo) <= 0 &&
     596          134 :                      tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0) ||
     597           56 :                     gbt_var_node_pf_match(key, query, tinfo);
     598         2689 :             break;
     599         5466 :         case BTGreaterStrategyNumber:
     600         5466 :             if (is_leaf)
     601         5405 :                 retval = tinfo->f_lt(query, key->upper, collation, flinfo);
     602              :             else
     603          122 :                 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
     604           61 :                     || gbt_var_node_pf_match(key, query, tinfo);
     605         5466 :             break;
     606         5412 :         case BTGreaterEqualStrategyNumber:
     607         5412 :             if (is_leaf)
     608         5336 :                 retval = tinfo->f_le(query, key->upper, collation, flinfo);
     609              :             else
     610          152 :                 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
     611           76 :                     || gbt_var_node_pf_match(key, query, tinfo);
     612         5412 :             break;
     613           11 :         case BtreeGistNotEqualStrategyNumber:
     614           17 :             retval = !(tinfo->f_eq(query, key->lower, collation, flinfo) &&
     615            6 :                        tinfo->f_eq(query, key->upper, collation, flinfo));
     616           11 :             break;
     617            0 :         default:
     618            0 :             retval = false;
     619              :     }
     620              : 
     621        24891 :     return retval;
     622              : }
        

Generated by: LCOV version 2.0-1