LCOV - code coverage report
Current view: top level - src/backend/utils/adt - tsgistidx.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 415 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 23 0
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 0.0 % 206 0

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * tsgistidx.c
       4                 :             :  *        GiST support functions for tsvector_ops
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *        src/backend/utils/adt/tsgistidx.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : 
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include "access/gist.h"
      18                 :             : #include "access/heaptoast.h"
      19                 :             : #include "access/reloptions.h"
      20                 :             : #include "common/int.h"
      21                 :             : #include "lib/qunique.h"
      22                 :             : #include "port/pg_bitutils.h"
      23                 :             : #include "tsearch/ts_utils.h"
      24                 :             : #include "utils/fmgrprotos.h"
      25                 :             : #include "utils/pg_crc.h"
      26                 :             : 
      27                 :             : 
      28                 :             : /* tsvector_ops opclass options */
      29                 :             : typedef struct
      30                 :             : {
      31                 :             :         int32           vl_len_;                /* varlena header (do not touch directly!) */
      32                 :             :         int                     siglen;                 /* signature length */
      33                 :             : } GistTsVectorOptions;
      34                 :             : 
      35                 :             : #define SIGLEN_DEFAULT  (31 * 4)
      36                 :             : #define SIGLEN_MAX              GISTMaxIndexKeySize
      37                 :             : #define GET_SIGLEN()    (PG_HAS_OPCLASS_OPTIONS() ? \
      38                 :             :                                                  ((GistTsVectorOptions *) PG_GET_OPCLASS_OPTIONS())->siglen : \
      39                 :             :                                                  SIGLEN_DEFAULT)
      40                 :             : 
      41                 :             : #define SIGLENBIT(siglen) ((siglen) * BITS_PER_BYTE)
      42                 :             : 
      43                 :             : typedef char *BITVECP;
      44                 :             : 
      45                 :             : #define LOOPBYTE(siglen) \
      46                 :             :                         for (i = 0; i < siglen; i++)
      47                 :             : 
      48                 :             : #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
      49                 :             : #define GETBITBYTE(x,i) ( ((char)(x)) >> (i) & 0x01 )
      50                 :             : #define CLRBIT(x,i)   GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
      51                 :             : #define SETBIT(x,i)   GETBYTE(x,i) |=  ( 0x01 << ( (i) % BITS_PER_BYTE ) )
      52                 :             : #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
      53                 :             : 
      54                 :             : #define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen))
      55                 :             : #define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen))
      56                 :             : 
      57                 :             : #define GETENTRY(vec,pos) ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))
      58                 :             : 
      59                 :             : /*
      60                 :             :  * type of GiST index key
      61                 :             :  */
      62                 :             : 
      63                 :             : typedef struct
      64                 :             : {
      65                 :             :         int32           vl_len_;                /* varlena header (do not touch directly!) */
      66                 :             :         int32           flag;
      67                 :             :         char            data[FLEXIBLE_ARRAY_MEMBER];
      68                 :             : } SignTSVector;
      69                 :             : 
      70                 :             : #define ARRKEY          0x01
      71                 :             : #define SIGNKEY         0x02
      72                 :             : #define ALLISTRUE       0x04
      73                 :             : 
      74                 :             : #define ISARRKEY(x) ( ((SignTSVector*)(x))->flag & ARRKEY )
      75                 :             : #define ISSIGNKEY(x)    ( ((SignTSVector*)(x))->flag & SIGNKEY )
      76                 :             : #define ISALLTRUE(x)    ( ((SignTSVector*)(x))->flag & ALLISTRUE )
      77                 :             : 
      78                 :             : #define GTHDRSIZE       ( VARHDRSZ + sizeof(int32) )
      79                 :             : #define CALCGTSIZE(flag, len) ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )
      80                 :             : 
      81                 :             : #define GETSIGN(x)      ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
      82                 :             : #define GETSIGLEN(x)( VARSIZE(x) - GTHDRSIZE )
      83                 :             : #define GETARR(x)       ( (int32*)( (char*)(x)+GTHDRSIZE ) )
      84                 :             : #define ARRNELEM(x) ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )
      85                 :             : 
      86                 :             : static int32 sizebitvec(BITVECP sign, int siglen);
      87                 :             : 
      88                 :             : Datum
      89                 :           0 : gtsvectorin(PG_FUNCTION_ARGS)
      90                 :             : {
      91                 :             :         /* There's no need to support input of gtsvectors */
      92   [ #  #  #  # ]:           0 :         ereport(ERROR,
      93                 :             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
      94                 :             :                          errmsg("cannot accept a value of type %s", "gtsvector")));
      95                 :             : 
      96                 :           0 :         PG_RETURN_VOID();                       /* keep compiler quiet */
      97                 :             : }
      98                 :             : 
      99                 :             : Datum
     100                 :           0 : gtsvectorout(PG_FUNCTION_ARGS)
     101                 :             : {
     102                 :           0 :         SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
     103                 :           0 :         char       *outbuf;
     104                 :             : 
     105         [ #  # ]:           0 :         if (ISARRKEY(key))
     106                 :           0 :                 outbuf = psprintf("%d unique words", (int) ARRNELEM(key));
     107                 :             :         else
     108                 :             :         {
     109         [ #  # ]:           0 :                 if (ISALLTRUE(key))
     110                 :           0 :                         outbuf = pstrdup("all true bits");
     111                 :             :                 else
     112                 :             :                 {
     113                 :           0 :                         int                     siglen = GETSIGLEN(key);
     114                 :           0 :                         int                     cnttrue = sizebitvec(GETSIGN(key), siglen);
     115                 :             : 
     116                 :           0 :                         outbuf = psprintf("%d true bits, %d false bits",
     117                 :           0 :                                                           cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
     118                 :           0 :                 }
     119                 :             :         }
     120                 :             : 
     121         [ #  # ]:           0 :         PG_FREE_IF_COPY(key, 0);
     122                 :           0 :         PG_RETURN_POINTER(outbuf);
     123                 :           0 : }
     124                 :             : 
     125                 :             : static int
     126                 :           0 : compareint(const void *va, const void *vb)
     127                 :             : {
     128                 :           0 :         int32           a = *((const int32 *) va);
     129                 :           0 :         int32           b = *((const int32 *) vb);
     130                 :             : 
     131                 :           0 :         return pg_cmp_s32(a, b);
     132                 :           0 : }
     133                 :             : 
     134                 :             : static void
     135                 :           0 : makesign(BITVECP sign, SignTSVector *a, int siglen)
     136                 :             : {
     137                 :           0 :         int32           k,
     138                 :           0 :                                 len = ARRNELEM(a);
     139                 :           0 :         int32      *ptr = GETARR(a);
     140                 :             : 
     141   [ #  #  #  #  :           0 :         MemSet(sign, 0, siglen);
          #  #  #  #  #  
                      # ]
     142         [ #  # ]:           0 :         for (k = 0; k < len; k++)
     143                 :           0 :                 HASH(sign, ptr[k], siglen);
     144                 :           0 : }
     145                 :             : 
     146                 :             : static SignTSVector *
     147                 :           0 : gtsvector_alloc(int flag, int len, BITVECP sign)
     148                 :             : {
     149   [ #  #  #  # ]:           0 :         int                     size = CALCGTSIZE(flag, len);
     150                 :           0 :         SignTSVector *res = palloc(size);
     151                 :             : 
     152                 :           0 :         SET_VARSIZE(res, size);
     153                 :           0 :         res->flag = flag;
     154                 :             : 
     155   [ #  #  #  # ]:           0 :         if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
     156                 :           0 :                 memcpy(GETSIGN(res), sign, len);
     157                 :             : 
     158                 :           0 :         return res;
     159                 :           0 : }
     160                 :             : 
     161                 :             : 
     162                 :             : Datum
     163                 :           0 : gtsvector_compress(PG_FUNCTION_ARGS)
     164                 :             : {
     165                 :           0 :         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     166         [ #  # ]:           0 :         int                     siglen = GET_SIGLEN();
     167                 :           0 :         GISTENTRY  *retval = entry;
     168                 :             : 
     169         [ #  # ]:           0 :         if (entry->leafkey)
     170                 :             :         {                                                       /* tsvector */
     171                 :           0 :                 TSVector        val = DatumGetTSVector(entry->key);
     172                 :           0 :                 SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
     173                 :           0 :                 int32           len;
     174                 :           0 :                 int32      *arr;
     175                 :           0 :                 WordEntry  *ptr = ARRPTR(val);
     176                 :           0 :                 char       *words = STRPTR(val);
     177                 :             : 
     178                 :           0 :                 arr = GETARR(res);
     179                 :           0 :                 len = val->size;
     180         [ #  # ]:           0 :                 while (len--)
     181                 :             :                 {
     182                 :           0 :                         pg_crc32        c;
     183                 :             : 
     184                 :           0 :                         INIT_LEGACY_CRC32(c);
     185         [ #  # ]:           0 :                         COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
     186                 :           0 :                         FIN_LEGACY_CRC32(c);
     187                 :             : 
     188                 :           0 :                         *arr = *(int32 *) &c;
     189                 :           0 :                         arr++;
     190                 :           0 :                         ptr++;
     191                 :           0 :                 }
     192                 :             : 
     193                 :           0 :                 qsort(GETARR(res), val->size, sizeof(int), compareint);
     194                 :           0 :                 len = qunique(GETARR(res), val->size, sizeof(int), compareint);
     195         [ #  # ]:           0 :                 if (len != val->size)
     196                 :             :                 {
     197                 :             :                         /*
     198                 :             :                          * there is a collision of hash-function; len is always less than
     199                 :             :                          * val->size
     200                 :             :                          */
     201                 :           0 :                         len = CALCGTSIZE(ARRKEY, len);
     202                 :           0 :                         res = (SignTSVector *) repalloc(res, len);
     203                 :           0 :                         SET_VARSIZE(res, len);
     204                 :           0 :                 }
     205                 :             : 
     206                 :             :                 /* make signature, if array is too long */
     207         [ #  # ]:           0 :                 if (VARSIZE(res) > TOAST_INDEX_TARGET)
     208                 :             :                 {
     209                 :           0 :                         SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
     210                 :             : 
     211                 :           0 :                         makesign(GETSIGN(ressign), res, siglen);
     212                 :           0 :                         res = ressign;
     213                 :           0 :                 }
     214                 :             : 
     215                 :           0 :                 retval = palloc_object(GISTENTRY);
     216                 :           0 :                 gistentryinit(*retval, PointerGetDatum(res),
     217                 :             :                                           entry->rel, entry->page,
     218                 :             :                                           entry->offset, false);
     219                 :           0 :         }
     220   [ #  #  #  # ]:           0 :         else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
     221                 :           0 :                          !ISALLTRUE(DatumGetPointer(entry->key)))
     222                 :             :         {
     223                 :           0 :                 int32           i;
     224                 :           0 :                 SignTSVector *res;
     225                 :           0 :                 BITVECP         sign = GETSIGN(DatumGetPointer(entry->key));
     226                 :             : 
     227         [ #  # ]:           0 :                 LOOPBYTE(siglen)
     228                 :             :                 {
     229         [ #  # ]:           0 :                         if ((sign[i] & 0xff) != 0xff)
     230                 :           0 :                                 PG_RETURN_POINTER(retval);
     231                 :           0 :                 }
     232                 :             : 
     233                 :           0 :                 res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
     234                 :           0 :                 retval = palloc_object(GISTENTRY);
     235                 :           0 :                 gistentryinit(*retval, PointerGetDatum(res),
     236                 :             :                                           entry->rel, entry->page,
     237                 :             :                                           entry->offset, false);
     238         [ #  # ]:           0 :         }
     239                 :           0 :         PG_RETURN_POINTER(retval);
     240                 :           0 : }
     241                 :             : 
     242                 :             : Datum
     243                 :           0 : gtsvector_decompress(PG_FUNCTION_ARGS)
     244                 :             : {
     245                 :             :         /*
     246                 :             :          * We need to detoast the stored value, because the other gtsvector
     247                 :             :          * support functions don't cope with toasted values.
     248                 :             :          */
     249                 :           0 :         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     250                 :           0 :         SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(entry->key);
     251                 :             : 
     252         [ #  # ]:           0 :         if (key != (SignTSVector *) DatumGetPointer(entry->key))
     253                 :             :         {
     254                 :           0 :                 GISTENTRY  *retval = palloc_object(GISTENTRY);
     255                 :             : 
     256                 :           0 :                 gistentryinit(*retval, PointerGetDatum(key),
     257                 :             :                                           entry->rel, entry->page,
     258                 :             :                                           entry->offset, false);
     259                 :             : 
     260                 :           0 :                 PG_RETURN_POINTER(retval);
     261                 :           0 :         }
     262                 :             : 
     263                 :           0 :         PG_RETURN_POINTER(entry);
     264                 :           0 : }
     265                 :             : 
     266                 :             : typedef struct
     267                 :             : {
     268                 :             :         int32      *arrb;
     269                 :             :         int32      *arre;
     270                 :             : } CHKVAL;
     271                 :             : 
     272                 :             : /*
     273                 :             :  * TS_execute callback for matching a tsquery operand to GIST leaf-page data
     274                 :             :  */
     275                 :             : static TSTernaryValue
     276                 :           0 : checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
     277                 :             : {
     278                 :           0 :         int32      *StopLow = ((CHKVAL *) checkval)->arrb;
     279                 :           0 :         int32      *StopHigh = ((CHKVAL *) checkval)->arre;
     280                 :           0 :         int32      *StopMiddle;
     281                 :             : 
     282                 :             :         /* Loop invariant: StopLow <= val < StopHigh */
     283                 :             : 
     284                 :             :         /*
     285                 :             :          * we are not able to find a prefix by hash value
     286                 :             :          */
     287         [ #  # ]:           0 :         if (val->prefix)
     288                 :           0 :                 return TS_MAYBE;
     289                 :             : 
     290         [ #  # ]:           0 :         while (StopLow < StopHigh)
     291                 :             :         {
     292                 :           0 :                 StopMiddle = StopLow + (StopHigh - StopLow) / 2;
     293         [ #  # ]:           0 :                 if (*StopMiddle == val->valcrc)
     294                 :           0 :                         return TS_MAYBE;
     295         [ #  # ]:           0 :                 else if (*StopMiddle < val->valcrc)
     296                 :           0 :                         StopLow = StopMiddle + 1;
     297                 :             :                 else
     298                 :           0 :                         StopHigh = StopMiddle;
     299                 :             :         }
     300                 :             : 
     301                 :           0 :         return TS_NO;
     302                 :           0 : }
     303                 :             : 
     304                 :             : /*
     305                 :             :  * TS_execute callback for matching a tsquery operand to GIST non-leaf data
     306                 :             :  */
     307                 :             : static TSTernaryValue
     308                 :           0 : checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
     309                 :             : {
     310                 :           0 :         void       *key = (SignTSVector *) checkval;
     311                 :             : 
     312                 :             :         /*
     313                 :             :          * we are not able to find a prefix in signature tree
     314                 :             :          */
     315         [ #  # ]:           0 :         if (val->prefix)
     316                 :           0 :                 return TS_MAYBE;
     317                 :             : 
     318         [ #  # ]:           0 :         if (GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key))))
     319                 :           0 :                 return TS_MAYBE;
     320                 :             :         else
     321                 :           0 :                 return TS_NO;
     322                 :           0 : }
     323                 :             : 
     324                 :             : Datum
     325                 :           0 : gtsvector_consistent(PG_FUNCTION_ARGS)
     326                 :             : {
     327                 :           0 :         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     328                 :           0 :         TSQuery         query = PG_GETARG_TSQUERY(1);
     329                 :             : #ifdef NOT_USED
     330                 :             :         StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     331                 :             :         Oid                     subtype = PG_GETARG_OID(3);
     332                 :             : #endif
     333                 :           0 :         bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     334                 :           0 :         SignTSVector *key = (SignTSVector *) DatumGetPointer(entry->key);
     335                 :             : 
     336                 :             :         /* All cases served by this function are inexact */
     337                 :           0 :         *recheck = true;
     338                 :             : 
     339         [ #  # ]:           0 :         if (!query->size)
     340                 :           0 :                 PG_RETURN_BOOL(false);
     341                 :             : 
     342         [ #  # ]:           0 :         if (ISSIGNKEY(key))
     343                 :             :         {
     344         [ #  # ]:           0 :                 if (ISALLTRUE(key))
     345                 :           0 :                         PG_RETURN_BOOL(true);
     346                 :             : 
     347                 :           0 :                 PG_RETURN_BOOL(TS_execute(GETQUERY(query),
     348                 :             :                                                                   key,
     349                 :             :                                                                   TS_EXEC_PHRASE_NO_POS,
     350                 :             :                                                                   checkcondition_bit));
     351                 :             :         }
     352                 :             :         else
     353                 :             :         {                                                       /* only leaf pages */
     354                 :           0 :                 CHKVAL          chkval;
     355                 :             : 
     356                 :           0 :                 chkval.arrb = GETARR(key);
     357                 :           0 :                 chkval.arre = chkval.arrb + ARRNELEM(key);
     358                 :           0 :                 PG_RETURN_BOOL(TS_execute(GETQUERY(query),
     359                 :             :                                                                   &chkval,
     360                 :             :                                                                   TS_EXEC_PHRASE_NO_POS,
     361                 :             :                                                                   checkcondition_arr));
     362                 :           0 :         }
     363                 :           0 : }
     364                 :             : 
     365                 :             : static int32
     366                 :           0 : unionkey(BITVECP sbase, SignTSVector *add, int siglen)
     367                 :             : {
     368                 :           0 :         int32           i;
     369                 :             : 
     370         [ #  # ]:           0 :         if (ISSIGNKEY(add))
     371                 :             :         {
     372                 :           0 :                 BITVECP         sadd = GETSIGN(add);
     373                 :             : 
     374         [ #  # ]:           0 :                 if (ISALLTRUE(add))
     375                 :           0 :                         return 1;
     376                 :             : 
     377         [ #  # ]:           0 :                 Assert(GETSIGLEN(add) == siglen);
     378                 :             : 
     379         [ #  # ]:           0 :                 LOOPBYTE(siglen)
     380                 :           0 :                         sbase[i] |= sadd[i];
     381         [ #  # ]:           0 :         }
     382                 :             :         else
     383                 :             :         {
     384                 :           0 :                 int32      *ptr = GETARR(add);
     385                 :             : 
     386         [ #  # ]:           0 :                 for (i = 0; i < ARRNELEM(add); i++)
     387                 :           0 :                         HASH(sbase, ptr[i], siglen);
     388                 :           0 :         }
     389                 :           0 :         return 0;
     390                 :           0 : }
     391                 :             : 
     392                 :             : 
     393                 :             : Datum
     394                 :           0 : gtsvector_union(PG_FUNCTION_ARGS)
     395                 :             : {
     396                 :           0 :         GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     397                 :           0 :         int                *size = (int *) PG_GETARG_POINTER(1);
     398         [ #  # ]:           0 :         int                     siglen = GET_SIGLEN();
     399                 :           0 :         SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
     400                 :           0 :         BITVECP         base = GETSIGN(result);
     401                 :           0 :         int32           i;
     402                 :             : 
     403                 :           0 :         memset(base, 0, siglen);
     404                 :             : 
     405         [ #  # ]:           0 :         for (i = 0; i < entryvec->n; i++)
     406                 :             :         {
     407         [ #  # ]:           0 :                 if (unionkey(base, GETENTRY(entryvec, i), siglen))
     408                 :             :                 {
     409                 :           0 :                         result->flag |= ALLISTRUE;
     410   [ #  #  #  # ]:           0 :                         SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
     411                 :           0 :                         break;
     412                 :             :                 }
     413                 :           0 :         }
     414                 :             : 
     415                 :           0 :         *size = VARSIZE(result);
     416                 :             : 
     417                 :           0 :         PG_RETURN_POINTER(result);
     418                 :           0 : }
     419                 :             : 
     420                 :             : Datum
     421                 :           0 : gtsvector_same(PG_FUNCTION_ARGS)
     422                 :             : {
     423                 :           0 :         SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
     424                 :           0 :         SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
     425                 :           0 :         bool       *result = (bool *) PG_GETARG_POINTER(2);
     426         [ #  # ]:           0 :         int                     siglen = GET_SIGLEN();
     427                 :             : 
     428         [ #  # ]:           0 :         if (ISSIGNKEY(a))
     429                 :             :         {                                                       /* then b also ISSIGNKEY */
     430   [ #  #  #  # ]:           0 :                 if (ISALLTRUE(a) && ISALLTRUE(b))
     431                 :           0 :                         *result = true;
     432         [ #  # ]:           0 :                 else if (ISALLTRUE(a))
     433                 :           0 :                         *result = false;
     434         [ #  # ]:           0 :                 else if (ISALLTRUE(b))
     435                 :           0 :                         *result = false;
     436                 :             :                 else
     437                 :             :                 {
     438                 :           0 :                         int32           i;
     439                 :           0 :                         BITVECP         sa = GETSIGN(a),
     440                 :           0 :                                                 sb = GETSIGN(b);
     441                 :             : 
     442         [ #  # ]:           0 :                         Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
     443                 :             : 
     444                 :           0 :                         *result = true;
     445         [ #  # ]:           0 :                         LOOPBYTE(siglen)
     446                 :             :                         {
     447         [ #  # ]:           0 :                                 if (sa[i] != sb[i])
     448                 :             :                                 {
     449                 :           0 :                                         *result = false;
     450                 :           0 :                                         break;
     451                 :             :                                 }
     452                 :           0 :                         }
     453                 :           0 :                 }
     454                 :           0 :         }
     455                 :             :         else
     456                 :             :         {                                                       /* a and b ISARRKEY */
     457                 :           0 :                 int32           lena = ARRNELEM(a),
     458                 :           0 :                                         lenb = ARRNELEM(b);
     459                 :             : 
     460         [ #  # ]:           0 :                 if (lena != lenb)
     461                 :           0 :                         *result = false;
     462                 :             :                 else
     463                 :             :                 {
     464                 :           0 :                         int32      *ptra = GETARR(a),
     465                 :           0 :                                            *ptrb = GETARR(b);
     466                 :           0 :                         int32           i;
     467                 :             : 
     468                 :           0 :                         *result = true;
     469         [ #  # ]:           0 :                         for (i = 0; i < lena; i++)
     470         [ #  # ]:           0 :                                 if (ptra[i] != ptrb[i])
     471                 :             :                                 {
     472                 :           0 :                                         *result = false;
     473                 :           0 :                                         break;
     474                 :             :                                 }
     475                 :           0 :                 }
     476                 :           0 :         }
     477                 :             : 
     478                 :           0 :         PG_RETURN_POINTER(result);
     479                 :           0 : }
     480                 :             : 
     481                 :             : static int32
     482                 :           0 : sizebitvec(BITVECP sign, int siglen)
     483                 :             : {
     484                 :           0 :         return pg_popcount(sign, siglen);
     485                 :             : }
     486                 :             : 
     487                 :             : static int
     488                 :           0 : hemdistsign(BITVECP a, BITVECP b, int siglen)
     489                 :             : {
     490                 :           0 :         int                     i,
     491                 :             :                                 diff,
     492                 :           0 :                                 dist = 0;
     493                 :             : 
     494         [ #  # ]:           0 :         LOOPBYTE(siglen)
     495                 :             :         {
     496                 :           0 :                 diff = (unsigned char) (a[i] ^ b[i]);
     497                 :             :                 /* Using the popcount functions here isn't likely to win */
     498                 :           0 :                 dist += pg_number_of_ones[diff];
     499                 :           0 :         }
     500                 :           0 :         return dist;
     501                 :           0 : }
     502                 :             : 
     503                 :             : static int
     504                 :           0 : hemdist(SignTSVector *a, SignTSVector *b)
     505                 :             : {
     506                 :           0 :         int                     siglena = GETSIGLEN(a);
     507                 :           0 :         int                     siglenb = GETSIGLEN(b);
     508                 :             : 
     509         [ #  # ]:           0 :         if (ISALLTRUE(a))
     510                 :             :         {
     511         [ #  # ]:           0 :                 if (ISALLTRUE(b))
     512                 :           0 :                         return 0;
     513                 :             :                 else
     514                 :           0 :                         return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
     515                 :             :         }
     516         [ #  # ]:           0 :         else if (ISALLTRUE(b))
     517                 :           0 :                 return SIGLENBIT(siglena) - sizebitvec(GETSIGN(a), siglena);
     518                 :             : 
     519         [ #  # ]:           0 :         Assert(siglena == siglenb);
     520                 :             : 
     521                 :           0 :         return hemdistsign(GETSIGN(a), GETSIGN(b), siglena);
     522                 :           0 : }
     523                 :             : 
     524                 :             : Datum
     525                 :           0 : gtsvector_penalty(PG_FUNCTION_ARGS)
     526                 :             : {
     527                 :           0 :         GISTENTRY  *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
     528                 :           0 :         GISTENTRY  *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
     529                 :           0 :         float      *penalty = (float *) PG_GETARG_POINTER(2);
     530         [ #  # ]:           0 :         int                     siglen = GET_SIGLEN();
     531                 :           0 :         SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
     532                 :           0 :         SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
     533                 :           0 :         BITVECP         orig = GETSIGN(origval);
     534                 :             : 
     535                 :           0 :         *penalty = 0.0;
     536                 :             : 
     537         [ #  # ]:           0 :         if (ISARRKEY(newval))
     538                 :             :         {
     539                 :           0 :                 BITVECP         sign = palloc(siglen);
     540                 :             : 
     541                 :           0 :                 makesign(sign, newval, siglen);
     542                 :             : 
     543         [ #  # ]:           0 :                 if (ISALLTRUE(origval))
     544                 :             :                 {
     545                 :           0 :                         int                     siglenbit = SIGLENBIT(siglen);
     546                 :             : 
     547                 :           0 :                         *penalty =
     548                 :           0 :                                 (float) (siglenbit - sizebitvec(sign, siglen)) /
     549                 :           0 :                                 (float) (siglenbit + 1);
     550                 :           0 :                 }
     551                 :             :                 else
     552                 :           0 :                         *penalty = hemdistsign(sign, orig, siglen);
     553                 :             : 
     554                 :           0 :                 pfree(sign);
     555                 :           0 :         }
     556                 :             :         else
     557                 :           0 :                 *penalty = hemdist(origval, newval);
     558                 :           0 :         PG_RETURN_POINTER(penalty);
     559                 :           0 : }
     560                 :             : 
     561                 :             : typedef struct
     562                 :             : {
     563                 :             :         bool            allistrue;
     564                 :             :         BITVECP         sign;
     565                 :             : } CACHESIGN;
     566                 :             : 
     567                 :             : static void
     568                 :           0 : fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
     569                 :             : {
     570                 :           0 :         item->allistrue = false;
     571         [ #  # ]:           0 :         if (ISARRKEY(key))
     572                 :           0 :                 makesign(item->sign, key, siglen);
     573         [ #  # ]:           0 :         else if (ISALLTRUE(key))
     574                 :           0 :                 item->allistrue = true;
     575                 :             :         else
     576                 :           0 :                 memcpy(item->sign, GETSIGN(key), siglen);
     577                 :           0 : }
     578                 :             : 
     579                 :             : #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
     580                 :             : typedef struct
     581                 :             : {
     582                 :             :         OffsetNumber pos;
     583                 :             :         int32           cost;
     584                 :             : } SPLITCOST;
     585                 :             : 
     586                 :             : static int
     587                 :           0 : comparecost(const void *va, const void *vb)
     588                 :             : {
     589                 :           0 :         const SPLITCOST *a = (const SPLITCOST *) va;
     590                 :           0 :         const SPLITCOST *b = (const SPLITCOST *) vb;
     591                 :             : 
     592                 :           0 :         return pg_cmp_s32(a->cost, b->cost);
     593                 :           0 : }
     594                 :             : 
     595                 :             : 
     596                 :             : static int
     597                 :           0 : hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
     598                 :             : {
     599         [ #  # ]:           0 :         if (a->allistrue)
     600                 :             :         {
     601         [ #  # ]:           0 :                 if (b->allistrue)
     602                 :           0 :                         return 0;
     603                 :             :                 else
     604                 :           0 :                         return SIGLENBIT(siglen) - sizebitvec(b->sign, siglen);
     605                 :             :         }
     606         [ #  # ]:           0 :         else if (b->allistrue)
     607                 :           0 :                 return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
     608                 :             : 
     609                 :           0 :         return hemdistsign(a->sign, b->sign, siglen);
     610                 :           0 : }
     611                 :             : 
     612                 :             : Datum
     613                 :           0 : gtsvector_picksplit(PG_FUNCTION_ARGS)
     614                 :             : {
     615                 :           0 :         GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     616                 :           0 :         GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
     617         [ #  # ]:           0 :         int                     siglen = GET_SIGLEN();
     618                 :           0 :         OffsetNumber k,
     619                 :             :                                 j;
     620                 :           0 :         SignTSVector *datum_l,
     621                 :             :                            *datum_r;
     622                 :           0 :         BITVECP         union_l,
     623                 :             :                                 union_r;
     624                 :           0 :         int32           size_alpha,
     625                 :             :                                 size_beta;
     626                 :           0 :         int32           size_waste,
     627                 :           0 :                                 waste = -1;
     628                 :           0 :         int32           nbytes;
     629                 :           0 :         OffsetNumber seed_1 = 0,
     630                 :           0 :                                 seed_2 = 0;
     631                 :           0 :         OffsetNumber *left,
     632                 :             :                            *right;
     633                 :           0 :         OffsetNumber maxoff;
     634                 :           0 :         BITVECP         ptr;
     635                 :           0 :         int                     i;
     636                 :           0 :         CACHESIGN  *cache;
     637                 :           0 :         char       *cache_sign;
     638                 :           0 :         SPLITCOST  *costvector;
     639                 :             : 
     640                 :           0 :         maxoff = entryvec->n - 2;
     641                 :           0 :         nbytes = (maxoff + 2) * sizeof(OffsetNumber);
     642                 :           0 :         v->spl_left = (OffsetNumber *) palloc(nbytes);
     643                 :           0 :         v->spl_right = (OffsetNumber *) palloc(nbytes);
     644                 :             : 
     645                 :           0 :         cache = palloc_array(CACHESIGN, maxoff + 2);
     646                 :           0 :         cache_sign = palloc(siglen * (maxoff + 2));
     647                 :             : 
     648         [ #  # ]:           0 :         for (j = 0; j < maxoff + 2; j++)
     649                 :           0 :                 cache[j].sign = &cache_sign[siglen * j];
     650                 :             : 
     651                 :           0 :         fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber),
     652                 :           0 :                           siglen);
     653                 :             : 
     654         [ #  # ]:           0 :         for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
     655                 :             :         {
     656         [ #  # ]:           0 :                 for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
     657                 :             :                 {
     658         [ #  # ]:           0 :                         if (k == FirstOffsetNumber)
     659                 :           0 :                                 fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
     660                 :             : 
     661                 :           0 :                         size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
     662         [ #  # ]:           0 :                         if (size_waste > waste)
     663                 :             :                         {
     664                 :           0 :                                 waste = size_waste;
     665                 :           0 :                                 seed_1 = k;
     666                 :           0 :                                 seed_2 = j;
     667                 :           0 :                         }
     668                 :           0 :                 }
     669                 :           0 :         }
     670                 :             : 
     671                 :           0 :         left = v->spl_left;
     672                 :           0 :         v->spl_nleft = 0;
     673                 :           0 :         right = v->spl_right;
     674                 :           0 :         v->spl_nright = 0;
     675                 :             : 
     676   [ #  #  #  # ]:           0 :         if (seed_1 == 0 || seed_2 == 0)
     677                 :             :         {
     678                 :           0 :                 seed_1 = 1;
     679                 :           0 :                 seed_2 = 2;
     680                 :           0 :         }
     681                 :             : 
     682                 :             :         /* form initial .. */
     683                 :           0 :         datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
     684                 :           0 :                                                           siglen, cache[seed_1].sign);
     685                 :           0 :         datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
     686                 :           0 :                                                           siglen, cache[seed_2].sign);
     687                 :           0 :         union_l = GETSIGN(datum_l);
     688                 :           0 :         union_r = GETSIGN(datum_r);
     689                 :           0 :         maxoff = OffsetNumberNext(maxoff);
     690                 :           0 :         fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
     691                 :             :         /* sort before ... */
     692                 :           0 :         costvector = palloc_array(SPLITCOST, maxoff);
     693         [ #  # ]:           0 :         for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
     694                 :             :         {
     695                 :           0 :                 costvector[j - 1].pos = j;
     696                 :           0 :                 size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
     697                 :           0 :                 size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
     698                 :           0 :                 costvector[j - 1].cost = abs(size_alpha - size_beta);
     699                 :           0 :         }
     700                 :           0 :         qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
     701                 :             : 
     702         [ #  # ]:           0 :         for (k = 0; k < maxoff; k++)
     703                 :             :         {
     704                 :           0 :                 j = costvector[k].pos;
     705         [ #  # ]:           0 :                 if (j == seed_1)
     706                 :             :                 {
     707                 :           0 :                         *left++ = j;
     708                 :           0 :                         v->spl_nleft++;
     709                 :           0 :                         continue;
     710                 :             :                 }
     711         [ #  # ]:           0 :                 else if (j == seed_2)
     712                 :             :                 {
     713                 :           0 :                         *right++ = j;
     714                 :           0 :                         v->spl_nright++;
     715                 :           0 :                         continue;
     716                 :             :                 }
     717                 :             : 
     718   [ #  #  #  # ]:           0 :                 if (ISALLTRUE(datum_l) || cache[j].allistrue)
     719                 :             :                 {
     720   [ #  #  #  # ]:           0 :                         if (ISALLTRUE(datum_l) && cache[j].allistrue)
     721                 :           0 :                                 size_alpha = 0;
     722                 :             :                         else
     723                 :           0 :                                 size_alpha = SIGLENBIT(siglen) -
     724         [ #  # ]:           0 :                                         sizebitvec((cache[j].allistrue) ?
     725                 :           0 :                                                            GETSIGN(datum_l) :
     726                 :           0 :                                                            cache[j].sign,
     727                 :           0 :                                                            siglen);
     728                 :           0 :                 }
     729                 :             :                 else
     730                 :           0 :                         size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
     731                 :             : 
     732   [ #  #  #  # ]:           0 :                 if (ISALLTRUE(datum_r) || cache[j].allistrue)
     733                 :             :                 {
     734   [ #  #  #  # ]:           0 :                         if (ISALLTRUE(datum_r) && cache[j].allistrue)
     735                 :           0 :                                 size_beta = 0;
     736                 :             :                         else
     737                 :           0 :                                 size_beta = SIGLENBIT(siglen) -
     738         [ #  # ]:           0 :                                         sizebitvec((cache[j].allistrue) ?
     739                 :           0 :                                                            GETSIGN(datum_r) :
     740                 :           0 :                                                            cache[j].sign,
     741                 :           0 :                                                            siglen);
     742                 :           0 :                 }
     743                 :             :                 else
     744                 :           0 :                         size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
     745                 :             : 
     746         [ #  # ]:           0 :                 if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
     747                 :             :                 {
     748   [ #  #  #  # ]:           0 :                         if (ISALLTRUE(datum_l) || cache[j].allistrue)
     749                 :             :                         {
     750         [ #  # ]:           0 :                                 if (!ISALLTRUE(datum_l))
     751                 :           0 :                                         memset(GETSIGN(datum_l), 0xff, siglen);
     752                 :           0 :                         }
     753                 :             :                         else
     754                 :             :                         {
     755                 :           0 :                                 ptr = cache[j].sign;
     756         [ #  # ]:           0 :                                 LOOPBYTE(siglen)
     757                 :           0 :                                         union_l[i] |= ptr[i];
     758                 :             :                         }
     759                 :           0 :                         *left++ = j;
     760                 :           0 :                         v->spl_nleft++;
     761                 :           0 :                 }
     762                 :             :                 else
     763                 :             :                 {
     764   [ #  #  #  # ]:           0 :                         if (ISALLTRUE(datum_r) || cache[j].allistrue)
     765                 :             :                         {
     766         [ #  # ]:           0 :                                 if (!ISALLTRUE(datum_r))
     767                 :           0 :                                         memset(GETSIGN(datum_r), 0xff, siglen);
     768                 :           0 :                         }
     769                 :             :                         else
     770                 :             :                         {
     771                 :           0 :                                 ptr = cache[j].sign;
     772         [ #  # ]:           0 :                                 LOOPBYTE(siglen)
     773                 :           0 :                                         union_r[i] |= ptr[i];
     774                 :             :                         }
     775                 :           0 :                         *right++ = j;
     776                 :           0 :                         v->spl_nright++;
     777                 :             :                 }
     778                 :           0 :         }
     779                 :             : 
     780                 :           0 :         *right = *left = FirstOffsetNumber;
     781                 :           0 :         v->spl_ldatum = PointerGetDatum(datum_l);
     782                 :           0 :         v->spl_rdatum = PointerGetDatum(datum_r);
     783                 :             : 
     784                 :           0 :         PG_RETURN_POINTER(v);
     785                 :           0 : }
     786                 :             : 
     787                 :             : /*
     788                 :             :  * Formerly, gtsvector_consistent was declared in pg_proc.h with arguments
     789                 :             :  * that did not match the documented conventions for GiST support functions.
     790                 :             :  * We fixed that, but we still need a pg_proc entry with the old signature
     791                 :             :  * to support reloading pre-9.6 contrib/tsearch2 opclass declarations.
     792                 :             :  * This compatibility function should go away eventually.
     793                 :             :  */
     794                 :             : Datum
     795                 :           0 : gtsvector_consistent_oldsig(PG_FUNCTION_ARGS)
     796                 :             : {
     797                 :           0 :         return gtsvector_consistent(fcinfo);
     798                 :             : }
     799                 :             : 
     800                 :             : Datum
     801                 :           0 : gtsvector_options(PG_FUNCTION_ARGS)
     802                 :             : {
     803                 :           0 :         local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
     804                 :             : 
     805                 :           0 :         init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
     806                 :           0 :         add_local_int_reloption(relopts, "siglen", "signature length",
     807                 :             :                                                         SIGLEN_DEFAULT, 1, SIGLEN_MAX,
     808                 :             :                                                         offsetof(GistTsVectorOptions, siglen));
     809                 :             : 
     810                 :           0 :         PG_RETURN_VOID();
     811                 :           0 : }
        

Generated by: LCOV version 2.3.2-1