LCOV - differential code coverage report
Current view: top level - src/backend/access/spgist - spgtextproc.c (source / functions) Coverage Total Hit LBC UBC GBC GNC CBC EUB ECB DCB
Current: c3df85756ceb0246958ef2b72c04aba51e52de13 vs 167cb26718e3eae4fef470900b4cd1d434f15649 Lines: 97.6 % 289 282 7 10 272 1 11
Current Date: 2025-12-18 07:33:40 +0900 Functions: 100.0 % 9 9 5 4 1
Baseline: lcov-20251218-005734-baseline Branches: 89.7 % 145 130 1 14 5 125 146 50
Baseline Date: 2025-12-17 11:55:04 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 9 9 9
(30,360] days: 100.0 % 1 1 1
(360..) days: 97.5 % 279 272 7 272 1
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 8 8 4 4
Branch coverage date bins:
(360..) days: 38.1 % 341 130 1 14 5 125 146 50

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * spgtextproc.c
                                  4                 :                :  *    implementation of radix tree (compressed trie) over text
                                  5                 :                :  *
                                  6                 :                :  * In a text_ops SPGiST index, inner tuples can have a prefix which is the
                                  7                 :                :  * common prefix of all strings indexed under that tuple.  The node labels
                                  8                 :                :  * represent the next byte of the string(s) after the prefix.  Assuming we
                                  9                 :                :  * always use the longest possible prefix, we will get more than one node
                                 10                 :                :  * label unless the prefix length is restricted by SPGIST_MAX_PREFIX_LENGTH.
                                 11                 :                :  *
                                 12                 :                :  * To reconstruct the indexed string for any index entry, concatenate the
                                 13                 :                :  * inner-tuple prefixes and node labels starting at the root and working
                                 14                 :                :  * down to the leaf entry, then append the datum in the leaf entry.
                                 15                 :                :  * (While descending the tree, "level" is the number of bytes reconstructed
                                 16                 :                :  * so far.)
                                 17                 :                :  *
                                 18                 :                :  * However, there are two special cases for node labels: -1 indicates that
                                 19                 :                :  * there are no more bytes after the prefix-so-far, and -2 indicates that we
                                 20                 :                :  * had to split an existing allTheSame tuple (in such a case we have to create
                                 21                 :                :  * a node label that doesn't correspond to any string byte).  In either case,
                                 22                 :                :  * the node label does not contribute anything to the reconstructed string.
                                 23                 :                :  *
                                 24                 :                :  * Previously, we used a node label of zero for both special cases, but
                                 25                 :                :  * this was problematic because one can't tell whether a string ending at
                                 26                 :                :  * the current level can be pushed down into such a child node.  For
                                 27                 :                :  * backwards compatibility, we still support such node labels for reading;
                                 28                 :                :  * but no new entries will ever be pushed down into a zero-labeled child.
                                 29                 :                :  * No new entries ever get pushed into a -2-labeled child, either.
                                 30                 :                :  *
                                 31                 :                :  *
                                 32                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 33                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 34                 :                :  *
                                 35                 :                :  * IDENTIFICATION
                                 36                 :                :  *          src/backend/access/spgist/spgtextproc.c
                                 37                 :                :  *
                                 38                 :                :  *-------------------------------------------------------------------------
                                 39                 :                :  */
                                 40                 :                : #include "postgres.h"
                                 41                 :                : 
                                 42                 :                : #include "access/spgist.h"
                                 43                 :                : #include "catalog/pg_type.h"
                                 44                 :                : #include "common/int.h"
                                 45                 :                : #include "mb/pg_wchar.h"
                                 46                 :                : #include "utils/datum.h"
                                 47                 :                : #include "utils/fmgrprotos.h"
                                 48                 :                : #include "utils/pg_locale.h"
                                 49                 :                : #include "utils/varlena.h"
                                 50                 :                : #include "varatt.h"
                                 51                 :                : 
                                 52                 :                : 
                                 53                 :                : /*
                                 54                 :                :  * In the worst case, an inner tuple in a text radix tree could have as many
                                 55                 :                :  * as 258 nodes (one for each possible byte value, plus the two special
                                 56                 :                :  * cases).  Each node can take 16 bytes on MAXALIGN=8 machines.  The inner
                                 57                 :                :  * tuple must fit on an index page of size BLCKSZ.  Rather than assuming we
                                 58                 :                :  * know the exact amount of overhead imposed by page headers, tuple headers,
                                 59                 :                :  * etc, we leave 100 bytes for that (the actual overhead should be no more
                                 60                 :                :  * than 56 bytes at this writing, so there is slop in this number).
                                 61                 :                :  * So we can safely create prefixes up to BLCKSZ - 258 * 16 - 100 bytes long.
                                 62                 :                :  * Unfortunately, because 258 * 16 is over 4K, there is no safe prefix length
                                 63                 :                :  * when BLCKSZ is less than 8K; it is always possible to get "SPGiST inner
                                 64                 :                :  * tuple size exceeds maximum" if there are too many distinct next-byte values
                                 65                 :                :  * at a given place in the tree.  Since use of nonstandard block sizes appears
                                 66                 :                :  * to be negligible in the field, we just live with that fact for now,
                                 67                 :                :  * choosing a max prefix size of 32 bytes when BLCKSZ is configured smaller
                                 68                 :                :  * than default.
                                 69                 :                :  */
                                 70                 :                : #define SPGIST_MAX_PREFIX_LENGTH    Max((int) (BLCKSZ - 258 * 16 - 100), 32)
                                 71                 :                : 
                                 72                 :                : /*
                                 73                 :                :  * Strategy for collation aware operator on text is equal to btree strategy
                                 74                 :                :  * plus value of 10.
                                 75                 :                :  *
                                 76                 :                :  * Current collation aware strategies and their corresponding btree strategies:
                                 77                 :                :  * 11 BTLessStrategyNumber
                                 78                 :                :  * 12 BTLessEqualStrategyNumber
                                 79                 :                :  * 14 BTGreaterEqualStrategyNumber
                                 80                 :                :  * 15 BTGreaterStrategyNumber
                                 81                 :                :  */
                                 82                 :                : #define SPG_STRATEGY_ADDITION   (10)
                                 83                 :                : #define SPG_IS_COLLATION_AWARE_STRATEGY(s) ((s) > SPG_STRATEGY_ADDITION \
                                 84                 :                :                                          && (s) != RTPrefixStrategyNumber)
                                 85                 :                : 
                                 86                 :                : /* Struct for sorting values in picksplit */
                                 87                 :                : typedef struct spgNodePtr
                                 88                 :                : {
                                 89                 :                :     Datum       d;
                                 90                 :                :     int         i;
                                 91                 :                :     int16       c;
                                 92                 :                : } spgNodePtr;
                                 93                 :                : 
                                 94                 :                : 
                                 95                 :                : Datum
 5115 tgl@sss.pgh.pa.us          96                 :CBC          42 : spg_text_config(PG_FUNCTION_ARGS)
                                 97                 :                : {
                                 98                 :                :     /* spgConfigIn *cfgin = (spgConfigIn *) PG_GETARG_POINTER(0); */
                                 99                 :             42 :     spgConfigOut *cfg = (spgConfigOut *) PG_GETARG_POINTER(1);
                                100                 :                : 
                                101                 :             42 :     cfg->prefixType = TEXTOID;
 4210                           102                 :             42 :     cfg->labelType = INT2OID;
 5113                           103                 :             42 :     cfg->canReturnData = true;
 5115                           104                 :             42 :     cfg->longValuesOK = true;    /* suffixing will shorten long values */
                                105                 :             42 :     PG_RETURN_VOID();
                                106                 :                : }
                                107                 :                : 
                                108                 :                : /*
                                109                 :                :  * Form a text datum from the given not-necessarily-null-terminated string,
                                110                 :                :  * using short varlena header format if possible
                                111                 :                :  */
                                112                 :                : static Datum
                                113                 :         129480 : formTextDatum(const char *data, int datalen)
                                114                 :                : {
                                115                 :                :     char       *p;
                                116                 :                : 
                                117                 :         129480 :     p = (char *) palloc(datalen + VARHDRSZ);
                                118                 :                : 
                                119         [ +  - ]:         129480 :     if (datalen + VARHDRSZ_SHORT <= VARATT_SHORT_MAX)
                                120                 :                :     {
                                121                 :         129480 :         SET_VARSIZE_SHORT(p, datalen + VARHDRSZ_SHORT);
                                122         [ +  + ]:         129480 :         if (datalen)
                                123                 :         121396 :             memcpy(p + VARHDRSZ_SHORT, data, datalen);
                                124                 :                :     }
                                125                 :                :     else
                                126                 :                :     {
 5115 tgl@sss.pgh.pa.us         127                 :UBC           0 :         SET_VARSIZE(p, datalen + VARHDRSZ);
                                128                 :              0 :         memcpy(p + VARHDRSZ, data, datalen);
                                129                 :                :     }
                                130                 :                : 
 5115 tgl@sss.pgh.pa.us         131                 :CBC      129480 :     return PointerGetDatum(p);
                                132                 :                : }
                                133                 :                : 
                                134                 :                : /*
                                135                 :                :  * Find the length of the common prefix of a and b
                                136                 :                :  */
                                137                 :                : static int
                                138                 :          47550 : commonPrefix(const char *a, const char *b, int lena, int lenb)
                                139                 :                : {
                                140                 :          47550 :     int         i = 0;
                                141                 :                : 
                                142   [ +  +  +  +  :        3318449 :     while (i < lena && i < lenb && *a == *b)
                                              +  + ]
                                143                 :                :     {
                                144                 :        3270899 :         a++;
                                145                 :        3270899 :         b++;
                                146                 :        3270899 :         i++;
                                147                 :                :     }
                                148                 :                : 
                                149                 :          47550 :     return i;
                                150                 :                : }
                                151                 :                : 
                                152                 :                : /*
                                153                 :                :  * Binary search an array of int16 datums for a match to c
                                154                 :                :  *
                                155                 :                :  * On success, *i gets the match location; on failure, it gets where to insert
                                156                 :                :  */
                                157                 :                : static bool
   48 peter@eisentraut.org      158                 :GNC      104410 : searchChar(const Datum *nodeLabels, int nNodes, int16 c, int *i)
                                159                 :                : {
 5115 tgl@sss.pgh.pa.us         160                 :CBC      104410 :     int         StopLow = 0,
                                161                 :         104410 :                 StopHigh = nNodes;
                                162                 :                : 
                                163         [ +  + ]:         286571 :     while (StopLow < StopHigh)
                                164                 :                :     {
                                165                 :         285888 :         int         StopMiddle = (StopLow + StopHigh) >> 1;
 4210                           166                 :         285888 :         int16       middle = DatumGetInt16(nodeLabels[StopMiddle]);
                                167                 :                : 
 5115                           168         [ +  + ]:         285888 :         if (c < middle)
                                169                 :          90453 :             StopHigh = StopMiddle;
                                170         [ +  + ]:         195435 :         else if (c > middle)
                                171                 :          91708 :             StopLow = StopMiddle + 1;
                                172                 :                :         else
                                173                 :                :         {
                                174                 :         103727 :             *i = StopMiddle;
                                175                 :         103727 :             return true;
                                176                 :                :         }
                                177                 :                :     }
                                178                 :                : 
                                179                 :            683 :     *i = StopHigh;
                                180                 :            683 :     return false;
                                181                 :                : }
                                182                 :                : 
                                183                 :                : Datum
                                184                 :         104724 : spg_text_choose(PG_FUNCTION_ARGS)
                                185                 :                : {
                                186                 :         104724 :     spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0);
                                187                 :         104724 :     spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1);
                                188                 :         104724 :     text       *inText = DatumGetTextPP(in->datum);
                                189         [ +  - ]:         104724 :     char       *inStr = VARDATA_ANY(inText);
                                190   [ -  +  -  -  :         104724 :     int         inSize = VARSIZE_ANY_EXHDR(inText);
                                     -  -  -  -  +  
                                                 - ]
 4210                           191                 :         104724 :     char       *prefixStr = NULL;
                                192                 :         104724 :     int         prefixSize = 0;
 5115                           193                 :         104724 :     int         commonLen = 0;
 4210                           194                 :         104724 :     int16       nodeChar = 0;
                                195                 :         104724 :     int         i = 0;
                                196                 :                : 
                                197                 :                :     /* Check for prefix match, set nodeChar to first byte after prefix */
 5115                           198         [ +  + ]:         104724 :     if (in->hasPrefix)
                                199                 :                :     {
                                200                 :          41410 :         text       *prefixText = DatumGetTextPP(in->prefixDatum);
                                201                 :                : 
 4210                           202         [ +  - ]:          41410 :         prefixStr = VARDATA_ANY(prefixText);
                                203   [ -  +  -  -  :          41410 :         prefixSize = VARSIZE_ANY_EXHDR(prefixText);
                                     -  -  -  -  +  
                                                 - ]
                                204                 :                : 
 5115                           205                 :          41410 :         commonLen = commonPrefix(inStr + in->level,
                                206                 :                :                                  prefixStr,
                                207                 :          41410 :                                  inSize - in->level,
                                208                 :                :                                  prefixSize);
                                209                 :                : 
                                210         [ +  + ]:          41410 :         if (commonLen == prefixSize)
                                211                 :                :         {
                                212         [ +  + ]:          41096 :             if (inSize - in->level > commonLen)
 4210                           213                 :          38018 :                 nodeChar = *(unsigned char *) (inStr + in->level + commonLen);
                                214                 :                :             else
                                215                 :           3078 :                 nodeChar = -1;
                                216                 :                :         }
                                217                 :                :         else
                                218                 :                :         {
                                219                 :                :             /* Must split tuple because incoming value doesn't match prefix */
 5115                           220                 :            314 :             out->resultType = spgSplitTuple;
                                221                 :                : 
                                222         [ +  + ]:            314 :             if (commonLen == 0)
                                223                 :                :             {
                                224                 :             11 :                 out->result.splitTuple.prefixHasPrefix = false;
                                225                 :                :             }
                                226                 :                :             else
                                227                 :                :             {
                                228                 :            303 :                 out->result.splitTuple.prefixHasPrefix = true;
                                229                 :            303 :                 out->result.splitTuple.prefixPrefixDatum =
                                230                 :            303 :                     formTextDatum(prefixStr, commonLen);
                                231                 :                :             }
 3404                           232                 :            314 :             out->result.splitTuple.prefixNNodes = 1;
    8 michael@paquier.xyz       233                 :GNC         314 :             out->result.splitTuple.prefixNodeLabels = palloc_object(Datum);
 3404 tgl@sss.pgh.pa.us         234                 :CBC         628 :             out->result.splitTuple.prefixNodeLabels[0] =
 4210                           235                 :            314 :                 Int16GetDatum(*(unsigned char *) (prefixStr + commonLen));
                                236                 :                : 
 3404                           237                 :            314 :             out->result.splitTuple.childNodeN = 0;
                                238                 :                : 
 5115                           239         [ +  + ]:            314 :             if (prefixSize - commonLen == 1)
                                240                 :                :             {
                                241                 :            308 :                 out->result.splitTuple.postfixHasPrefix = false;
                                242                 :                :             }
                                243                 :                :             else
                                244                 :                :             {
                                245                 :              6 :                 out->result.splitTuple.postfixHasPrefix = true;
                                246                 :              6 :                 out->result.splitTuple.postfixPrefixDatum =
                                247                 :              6 :                     formTextDatum(prefixStr + commonLen + 1,
                                248                 :              6 :                                   prefixSize - commonLen - 1);
                                249                 :                :             }
                                250                 :                : 
                                251                 :            314 :             PG_RETURN_VOID();
                                252                 :                :         }
                                253                 :                :     }
                                254         [ +  + ]:          63314 :     else if (inSize > in->level)
                                255                 :                :     {
 4210                           256                 :          62763 :         nodeChar = *(unsigned char *) (inStr + in->level);
                                257                 :                :     }
                                258                 :                :     else
                                259                 :                :     {
                                260                 :            551 :         nodeChar = -1;
                                261                 :                :     }
                                262                 :                : 
                                263                 :                :     /* Look up nodeChar in the node label array */
 5115                           264         [ +  + ]:         104410 :     if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i))
                                265                 :                :     {
                                266                 :                :         /*
                                267                 :                :          * Descend to existing node.  (If in->allTheSame, the core code will
                                268                 :                :          * ignore our nodeN specification here, but that's OK.  We still have
                                269                 :                :          * to provide the correct levelAdd and restDatum values, and those are
                                270                 :                :          * the same regardless of which node gets chosen by core.)
                                271                 :                :          */
                                272                 :                :         int         levelAdd;
                                273                 :                : 
                                274                 :         103727 :         out->resultType = spgMatchNode;
                                275                 :         103727 :         out->result.matchNode.nodeN = i;
 4210                           276                 :         103727 :         levelAdd = commonLen;
                                277         [ +  + ]:         103727 :         if (nodeChar >= 0)
                                278                 :         100101 :             levelAdd++;
                                279                 :         103727 :         out->result.matchNode.levelAdd = levelAdd;
                                280         [ +  + ]:         103727 :         if (inSize - in->level - levelAdd > 0)
 5115                           281                 :         100098 :             out->result.matchNode.restDatum =
 4210                           282                 :         100098 :                 formTextDatum(inStr + in->level + levelAdd,
                                283                 :         100098 :                               inSize - in->level - levelAdd);
                                284                 :                :         else
 5115                           285                 :           3629 :             out->result.matchNode.restDatum =
                                286                 :           3629 :                 formTextDatum(NULL, 0);
                                287                 :                :     }
                                288         [ +  + ]:            683 :     else if (in->allTheSame)
                                289                 :                :     {
                                290                 :                :         /*
                                291                 :                :          * Can't use AddNode action, so split the tuple.  The upper tuple has
                                292                 :                :          * the same prefix as before and uses a dummy node label -2 for the
                                293                 :                :          * lower tuple.  The lower tuple has no prefix and the same node
                                294                 :                :          * labels as the original tuple.
                                295                 :                :          *
                                296                 :                :          * Note: it might seem tempting to shorten the upper tuple's prefix,
                                297                 :                :          * if it has one, then use its last byte as label for the lower tuple.
                                298                 :                :          * But that doesn't win since we know the incoming value matches the
                                299                 :                :          * whole prefix: we'd just end up splitting the lower tuple again.
                                300                 :                :          */
                                301                 :              3 :         out->resultType = spgSplitTuple;
                                302                 :              3 :         out->result.splitTuple.prefixHasPrefix = in->hasPrefix;
                                303                 :              3 :         out->result.splitTuple.prefixPrefixDatum = in->prefixDatum;
 3404                           304                 :              3 :         out->result.splitTuple.prefixNNodes = 1;
    8 michael@paquier.xyz       305                 :GNC           3 :         out->result.splitTuple.prefixNodeLabels = palloc_object(Datum);
 3404 tgl@sss.pgh.pa.us         306                 :CBC           3 :         out->result.splitTuple.prefixNodeLabels[0] = Int16GetDatum(-2);
                                307                 :              3 :         out->result.splitTuple.childNodeN = 0;
 5115                           308                 :              3 :         out->result.splitTuple.postfixHasPrefix = false;
                                309                 :                :     }
                                310                 :                :     else
                                311                 :                :     {
                                312                 :                :         /* Add a node for the not-previously-seen nodeChar value */
                                313                 :            680 :         out->resultType = spgAddNode;
 4210                           314                 :            680 :         out->result.addNode.nodeLabel = Int16GetDatum(nodeChar);
 5115                           315                 :            680 :         out->result.addNode.nodeN = i;
                                316                 :                :     }
                                317                 :                : 
                                318                 :         104410 :     PG_RETURN_VOID();
                                319                 :                : }
                                320                 :                : 
                                321                 :                : /* qsort comparator to sort spgNodePtr structs by "c" */
                                322                 :                : static int
                                323                 :          57672 : cmpNodePtr(const void *a, const void *b)
                                324                 :                : {
                                325                 :          57672 :     const spgNodePtr *aa = (const spgNodePtr *) a;
                                326                 :          57672 :     const spgNodePtr *bb = (const spgNodePtr *) b;
                                327                 :                : 
  671 nathan@postgresql.or      328                 :          57672 :     return pg_cmp_s16(aa->c, bb->c);
                                329                 :                : }
                                330                 :                : 
                                331                 :                : Datum
 5115 tgl@sss.pgh.pa.us         332                 :            261 : spg_text_picksplit(PG_FUNCTION_ARGS)
                                333                 :                : {
                                334                 :            261 :     spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0);
                                335                 :            261 :     spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1);
                                336                 :            261 :     text       *text0 = DatumGetTextPP(in->datums[0]);
                                337                 :                :     int         i,
                                338                 :                :                 commonLen;
                                339                 :                :     spgNodePtr *nodes;
                                340                 :                : 
                                341                 :                :     /* Identify longest common prefix, if any */
                                342   [ -  +  -  -  :            261 :     commonLen = VARSIZE_ANY_EXHDR(text0);
                                     -  -  -  -  +  
                                                 - ]
                                343   [ +  +  +  + ]:           6401 :     for (i = 1; i < in->nTuples && commonLen > 0; i++)
                                344                 :                :     {
                                345                 :           6140 :         text       *texti = DatumGetTextPP(in->datums[i]);
                                346         [ +  - ]:           6140 :         int         tmp = commonPrefix(VARDATA_ANY(text0),
                                347         [ +  - ]:           6140 :                                        VARDATA_ANY(texti),
                                348   [ -  +  -  -  :           6140 :                                        VARSIZE_ANY_EXHDR(text0),
                                     -  -  -  -  +  
                                                 - ]
                                349   [ -  +  -  -  :           6140 :                                        VARSIZE_ANY_EXHDR(texti));
                                     -  -  -  -  +  
                                                 - ]
                                350                 :                : 
                                351         [ +  + ]:           6140 :         if (tmp < commonLen)
                                352                 :            208 :             commonLen = tmp;
                                353                 :                :     }
                                354                 :                : 
                                355                 :                :     /*
                                356                 :                :      * Limit the prefix length, if necessary, to ensure that the resulting
                                357                 :                :      * inner tuple will fit on a page.
                                358                 :                :      */
                                359                 :            261 :     commonLen = Min(commonLen, SPGIST_MAX_PREFIX_LENGTH);
                                360                 :                : 
                                361                 :                :     /* Set node prefix to be that string, if it's not empty */
                                362         [ +  + ]:            261 :     if (commonLen == 0)
                                363                 :                :     {
                                364                 :            216 :         out->hasPrefix = false;
                                365                 :                :     }
                                366                 :                :     else
                                367                 :                :     {
                                368                 :             45 :         out->hasPrefix = true;
                                369         [ +  - ]:             45 :         out->prefixDatum = formTextDatum(VARDATA_ANY(text0), commonLen);
                                370                 :                :     }
                                371                 :                : 
                                372                 :                :     /* Extract the node label (first non-common byte) from each value */
    8 michael@paquier.xyz       373                 :GNC         261 :     nodes = palloc_array(spgNodePtr, in->nTuples);
                                374                 :                : 
 5115 tgl@sss.pgh.pa.us         375         [ +  + ]:CBC       25660 :     for (i = 0; i < in->nTuples; i++)
                                376                 :                :     {
                                377                 :          25399 :         text       *texti = DatumGetTextPP(in->datums[i]);
                                378                 :                : 
                                379   [ +  +  -  -  :          25399 :         if (commonLen < VARSIZE_ANY_EXHDR(texti))
                                     -  -  -  -  +  
                                           -  +  + ]
 4210                           380         [ +  - ]:          21812 :             nodes[i].c = *(unsigned char *) (VARDATA_ANY(texti) + commonLen);
                                381                 :                :         else
                                382                 :           3587 :             nodes[i].c = -1;    /* use -1 if string is all common */
 5115                           383                 :          25399 :         nodes[i].i = i;
                                384                 :          25399 :         nodes[i].d = in->datums[i];
                                385                 :                :     }
                                386                 :                : 
                                387                 :                :     /*
                                388                 :                :      * Sort by label values so that we can group the values into nodes.  This
                                389                 :                :      * also ensures that the nodes are ordered by label value, allowing the
                                390                 :                :      * use of binary search in searchChar.
                                391                 :                :      */
                                392                 :            261 :     qsort(nodes, in->nTuples, sizeof(*nodes), cmpNodePtr);
                                393                 :                : 
                                394                 :                :     /* And emit results */
                                395                 :            261 :     out->nNodes = 0;
    8 michael@paquier.xyz       396                 :GNC         261 :     out->nodeLabels = palloc_array(Datum, in->nTuples);
                                397                 :            261 :     out->mapTuplesToNodes = palloc_array(int, in->nTuples);
                                398                 :            261 :     out->leafTupleDatums = palloc_array(Datum, in->nTuples);
                                399                 :                : 
 5115 tgl@sss.pgh.pa.us         400         [ +  + ]:CBC       25660 :     for (i = 0; i < in->nTuples; i++)
                                401                 :                :     {
                                402                 :          25399 :         text       *texti = DatumGetTextPP(nodes[i].d);
                                403                 :                :         Datum       leafD;
                                404                 :                : 
                                405   [ +  +  +  + ]:          25399 :         if (i == 0 || nodes[i].c != nodes[i - 1].c)
                                406                 :                :         {
 4210                           407                 :           1599 :             out->nodeLabels[out->nNodes] = Int16GetDatum(nodes[i].c);
 5115                           408                 :           1599 :             out->nNodes++;
                                409                 :                :         }
                                410                 :                : 
                                411   [ +  +  -  -  :          25399 :         if (commonLen < VARSIZE_ANY_EXHDR(texti))
                                     -  -  -  -  +  
                                           -  +  + ]
                                412                 :          21812 :             leafD = formTextDatum(VARDATA_ANY(texti) + commonLen + 1,
                                413   [ -  +  -  -  :          21812 :                                   VARSIZE_ANY_EXHDR(texti) - commonLen - 1);
                                     -  -  -  -  +  
                                           -  +  - ]
                                414                 :                :         else
                                415                 :           3587 :             leafD = formTextDatum(NULL, 0);
                                416                 :                : 
                                417                 :          25399 :         out->leafTupleDatums[nodes[i].i] = leafD;
                                418                 :          25399 :         out->mapTuplesToNodes[nodes[i].i] = out->nNodes - 1;
                                419                 :                :     }
                                420                 :                : 
                                421                 :            261 :     PG_RETURN_VOID();
                                422                 :                : }
                                423                 :                : 
                                424                 :                : Datum
                                425                 :            898 : spg_text_inner_consistent(PG_FUNCTION_ARGS)
                                426                 :                : {
                                427                 :            898 :     spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0);
                                428                 :            898 :     spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1);
  470 jdavis@postgresql.or      429                 :            898 :     bool        collate_is_c = pg_newlocale_from_collation(PG_GET_COLLATION())->collate_is_c;
                                430                 :                :     text       *reconstructedValue;
                                431                 :                :     text       *reconstrText;
                                432                 :                :     int         maxReconstrLen;
 5115 tgl@sss.pgh.pa.us         433                 :            898 :     text       *prefixText = NULL;
                                434                 :            898 :     int         prefixSize = 0;
                                435                 :                :     int         i;
                                436                 :                : 
                                437                 :                :     /*
                                438                 :                :      * Reconstruct values represented at this tuple, including parent data,
                                439                 :                :      * prefix of this tuple if any, and the node label if it's non-dummy.
                                440                 :                :      * in->level should be the length of the previously reconstructed value,
                                441                 :                :      * and the number of bytes added here is prefixSize or prefixSize + 1.
                                442                 :                :      *
                                443                 :                :      * Note: we assume that in->reconstructedValue isn't toasted and doesn't
                                444                 :                :      * have a short varlena header.  This is okay because it must have been
                                445                 :                :      * created by a previous invocation of this routine, and we always emit
                                446                 :                :      * long-format reconstructed values.
                                447                 :                :      */
 3638                           448                 :            898 :     reconstructedValue = (text *) DatumGetPointer(in->reconstructedValue);
                                449   [ +  +  -  +  :            898 :     Assert(reconstructedValue == NULL ? in->level == 0 :
                                     -  -  -  -  -  
                                        -  -  +  -  
                                                 + ]
                                450                 :                :            VARSIZE_ANY_EXHDR(reconstructedValue) == in->level);
                                451                 :                : 
 5115                           452                 :            898 :     maxReconstrLen = in->level + 1;
                                453         [ +  + ]:            898 :     if (in->hasPrefix)
                                454                 :                :     {
                                455                 :            162 :         prefixText = DatumGetTextPP(in->prefixDatum);
                                456   [ -  +  -  -  :            162 :         prefixSize = VARSIZE_ANY_EXHDR(prefixText);
                                     -  -  -  -  +  
                                                 - ]
                                457                 :            162 :         maxReconstrLen += prefixSize;
                                458                 :                :     }
                                459                 :                : 
                                460                 :            898 :     reconstrText = palloc(VARHDRSZ + maxReconstrLen);
                                461                 :            898 :     SET_VARSIZE(reconstrText, VARHDRSZ + maxReconstrLen);
                                462                 :                : 
                                463         [ +  + ]:            898 :     if (in->level)
                                464                 :            808 :         memcpy(VARDATA(reconstrText),
 3638                           465                 :            808 :                VARDATA(reconstructedValue),
 5115                           466                 :            808 :                in->level);
                                467         [ +  + ]:            898 :     if (prefixSize)
                                468                 :            162 :         memcpy(((char *) VARDATA(reconstrText)) + in->level,
                                469         [ +  - ]:            162 :                VARDATA_ANY(prefixText),
                                470                 :                :                prefixSize);
                                471                 :                :     /* last byte of reconstrText will be filled in below */
                                472                 :                : 
                                473                 :                :     /*
                                474                 :                :      * Scan the child nodes.  For each one, complete the reconstructed value
                                475                 :                :      * and see if it's consistent with the query.  If so, emit an entry into
                                476                 :                :      * the output arrays.
                                477                 :                :      */
    8 michael@paquier.xyz       478                 :GNC         898 :     out->nodeNumbers = palloc_array(int, in->nNodes);
                                479                 :            898 :     out->levelAdds = palloc_array(int, in->nNodes);
                                480                 :            898 :     out->reconstructedValues = palloc_array(Datum, in->nNodes);
 5115 tgl@sss.pgh.pa.us         481                 :CBC         898 :     out->nNodes = 0;
                                482                 :                : 
                                483         [ +  + ]:           9440 :     for (i = 0; i < in->nNodes; i++)
                                484                 :                :     {
 4210                           485                 :           8542 :         int16       nodeChar = DatumGetInt16(in->nodeLabels[i]);
                                486                 :                :         int         thisLen;
 5031                           487                 :           8542 :         bool        res = true;
                                488                 :                :         int         j;
                                489                 :                : 
                                490                 :                :         /* If nodeChar is a dummy value, don't include it in data */
 4210                           491         [ +  + ]:           8542 :         if (nodeChar <= 0)
 5115                           492                 :           1918 :             thisLen = maxReconstrLen - 1;
                                493                 :                :         else
                                494                 :                :         {
 4210                           495                 :           6624 :             ((unsigned char *) VARDATA(reconstrText))[maxReconstrLen - 1] = nodeChar;
 5115                           496                 :           6624 :             thisLen = maxReconstrLen;
                                497                 :                :         }
                                498                 :                : 
 5031                           499         [ +  + ]:          14834 :         for (j = 0; j < in->nkeys; j++)
                                500                 :                :         {
                                501                 :           8542 :             StrategyNumber strategy = in->scankeys[j].sk_strategy;
                                502                 :                :             text       *inText;
                                503                 :                :             int         inSize;
                                504                 :                :             int         r;
                                505                 :                : 
                                506                 :                :             /*
                                507                 :                :              * If it's a collation-aware operator, but the collation is C, we
                                508                 :                :              * can treat it as non-collation-aware.  With non-C collation we
                                509                 :                :              * need to traverse whole tree :-( so there's no point in making
                                510                 :                :              * any check here.  (Note also that our reconstructed value may
                                511                 :                :              * well end with a partial multibyte character, so that applying
                                512                 :                :              * any encoding-sensitive test to it would be risky anyhow.)
                                513                 :                :              */
 2816 teodor@sigaev.ru          514   [ +  +  +  + ]:           8542 :             if (SPG_IS_COLLATION_AWARE_STRATEGY(strategy))
                                515                 :                :             {
 5031 tgl@sss.pgh.pa.us         516         [ +  + ]:           5336 :                 if (collate_is_c)
 2816 teodor@sigaev.ru          517                 :            312 :                     strategy -= SPG_STRATEGY_ADDITION;
                                518                 :                :                 else
 5031 tgl@sss.pgh.pa.us         519                 :           5024 :                     continue;
                                520                 :                :             }
                                521                 :                : 
                                522                 :           3518 :             inText = DatumGetTextPP(in->scankeys[j].sk_argument);
                                523   [ -  +  -  -  :           3518 :             inSize = VARSIZE_ANY_EXHDR(inText);
                                     -  -  -  -  -  
                                                 + ]
                                524                 :                : 
                                525                 :           3518 :             r = memcmp(VARDATA(reconstrText), VARDATA_ANY(inText),
                                526         [ -  + ]:           3518 :                        Min(inSize, thisLen));
                                527                 :                : 
                                528   [ +  +  +  +  :           3518 :             switch (strategy)
                                                 - ]
                                529                 :                :             {
                                530                 :            704 :                 case BTLessStrategyNumber:
                                531                 :                :                 case BTLessEqualStrategyNumber:
                                532         [ +  + ]:            704 :                     if (r > 0)
                                533                 :            400 :                         res = false;
                                534                 :            704 :                     break;
                                535                 :           1862 :                 case BTEqualStrategyNumber:
                                536   [ +  +  +  + ]:           1862 :                     if (r != 0 || inSize < thisLen)
                                537                 :           1050 :                         res = false;
                                538                 :           1862 :                     break;
                                539                 :            544 :                 case BTGreaterEqualStrategyNumber:
                                540                 :                :                 case BTGreaterStrategyNumber:
                                541         [ +  + ]:            544 :                     if (r < 0)
                                542                 :            416 :                         res = false;
                                543                 :            544 :                     break;
 2816 teodor@sigaev.ru          544                 :            408 :                 case RTPrefixStrategyNumber:
                                545         [ +  + ]:            408 :                     if (r != 0)
                                546                 :            384 :                         res = false;
                                547                 :            408 :                     break;
 5031 tgl@sss.pgh.pa.us         548                 :UBC           0 :                 default:
                                549         [ #  # ]:              0 :                     elog(ERROR, "unrecognized strategy number: %d",
                                550                 :                :                          in->scankeys[j].sk_strategy);
                                551                 :                :                     break;
                                552                 :                :             }
                                553                 :                : 
 5031 tgl@sss.pgh.pa.us         554         [ +  + ]:CBC        3518 :             if (!res)
                                555                 :           2250 :                 break;          /* no need to consider remaining conditions */
                                556                 :                :         }
                                557                 :                : 
 5115                           558         [ +  + ]:           8542 :         if (res)
                                559                 :                :         {
                                560                 :           6292 :             out->nodeNumbers[out->nNodes] = i;
                                561                 :           6292 :             out->levelAdds[out->nNodes] = thisLen - in->level;
                                562                 :           6292 :             SET_VARSIZE(reconstrText, VARHDRSZ + thisLen);
                                563                 :          12584 :             out->reconstructedValues[out->nNodes] =
                                564                 :           6292 :                 datumCopy(PointerGetDatum(reconstrText), false, -1);
                                565                 :           6292 :             out->nNodes++;
                                566                 :                :         }
                                567                 :                :     }
                                568                 :                : 
                                569                 :            898 :     PG_RETURN_VOID();
                                570                 :                : }
                                571                 :                : 
                                572                 :                : Datum
                                573                 :         117750 : spg_text_leaf_consistent(PG_FUNCTION_ARGS)
                                574                 :                : {
                                575                 :         117750 :     spgLeafConsistentIn *in = (spgLeafConsistentIn *) PG_GETARG_POINTER(0);
                                576                 :         117750 :     spgLeafConsistentOut *out = (spgLeafConsistentOut *) PG_GETARG_POINTER(1);
                                577                 :         117750 :     int         level = in->level;
                                578                 :                :     text       *leafValue,
                                579                 :         117750 :                *reconstrValue = NULL;
                                580                 :                :     char       *fullValue;
                                581                 :                :     int         fullLen;
                                582                 :                :     bool        res;
                                583                 :                :     int         j;
                                584                 :                : 
                                585                 :                :     /* all tests are exact */
                                586                 :         117750 :     out->recheck = false;
                                587                 :                : 
                                588                 :         117750 :     leafValue = DatumGetTextPP(in->leafDatum);
                                589                 :                : 
                                590                 :                :     /* As above, in->reconstructedValue isn't toasted or short. */
                                591         [ +  + ]:         117750 :     if (DatumGetPointer(in->reconstructedValue))
 3203 noah@leadboat.com         592                 :         117738 :         reconstrValue = (text *) DatumGetPointer(in->reconstructedValue);
                                593                 :                : 
 3638 tgl@sss.pgh.pa.us         594   [ +  +  -  +  :         117750 :     Assert(reconstrValue == NULL ? level == 0 :
                                     -  -  -  -  -  
                                        -  -  +  -  
                                                 + ]
                                595                 :                :            VARSIZE_ANY_EXHDR(reconstrValue) == level);
                                596                 :                : 
                                597                 :                :     /* Reconstruct the full string represented by this leaf tuple */
 5115                           598   [ -  +  -  -  :         117750 :     fullLen = level + VARSIZE_ANY_EXHDR(leafValue);
                                     -  -  -  -  +  
                                                 - ]
                                599   [ +  +  +  -  :         117750 :     if (VARSIZE_ANY_EXHDR(leafValue) == 0 && level > 0)
                                     -  -  -  -  +  
                                     -  +  +  -  -  
                                              +  - ]
                                600                 :                :     {
                                601                 :          37176 :         fullValue = VARDATA(reconstrValue);
 5113                           602                 :          37176 :         out->leafValue = PointerGetDatum(reconstrValue);
                                603                 :                :     }
                                604                 :                :     else
                                605                 :                :     {
 4939 bruce@momjian.us          606                 :          80574 :         text       *fullText = palloc(VARHDRSZ + fullLen);
                                607                 :                : 
 5113 tgl@sss.pgh.pa.us         608                 :          80574 :         SET_VARSIZE(fullText, VARHDRSZ + fullLen);
                                609                 :          80574 :         fullValue = VARDATA(fullText);
 5115                           610         [ +  + ]:          80574 :         if (level)
                                611                 :          80562 :             memcpy(fullValue, VARDATA(reconstrValue), level);
                                612   [ +  -  -  -  :          80574 :         if (VARSIZE_ANY_EXHDR(leafValue) > 0)
                                     -  -  -  -  +  
                                           -  +  - ]
                                613         [ +  - ]:          80574 :             memcpy(fullValue + level, VARDATA_ANY(leafValue),
 5115 tgl@sss.pgh.pa.us         614   [ -  +  -  -  :ECB     (80574) :                    VARSIZE_ANY_EXHDR(leafValue));
                                     -  -  -  -  +  
                                                 - ]
 5113 tgl@sss.pgh.pa.us         615                 :CBC       80574 :         out->leafValue = PointerGetDatum(fullText);
                                616                 :                :     }
                                617                 :                : 
                                618                 :                :     /* Perform the required comparison(s) */
 5031                           619                 :         117750 :     res = true;
                                620         [ +  + ]:         131523 :     for (j = 0; j < in->nkeys; j++)
                                621                 :                :     {
                                622                 :         117750 :         StrategyNumber strategy = in->scankeys[j].sk_strategy;
                                623                 :         117750 :         text       *query = DatumGetTextPP(in->scankeys[j].sk_argument);
                                624   [ -  +  -  -  :         117750 :         int         queryLen = VARSIZE_ANY_EXHDR(query);
                                     -  -  -  -  -  
                                                 + ]
                                625                 :                :         int         r;
                                626                 :                : 
 2816 teodor@sigaev.ru          627         [ +  + ]:         117750 :         if (strategy == RTPrefixStrategyNumber)
                                628                 :                :         {
                                629                 :                :             /*
                                630                 :                :              * if level >= length of query then reconstrValue must begin with
                                631                 :                :              * query (prefix) string, so we don't need to check it again.
                                632                 :                :              */
                                633   [ +  -  +  + ]:            384 :             res = (level >= queryLen) ||
 2463 peter@eisentraut.org      634                 :            192 :                 DatumGetBool(DirectFunctionCall2Coll(text_starts_with,
                                635                 :                :                                                      PG_GET_COLLATION(),
                                636                 :                :                                                      out->leafValue,
                                637                 :                :                                                      PointerGetDatum(query)));
                                638                 :                : 
 2803 tgl@sss.pgh.pa.us         639         [ +  + ]:            192 :             if (!res)           /* no need to consider remaining conditions */
 2816 teodor@sigaev.ru          640                 :            168 :                 break;
                                641                 :                : 
                                642                 :             24 :             continue;
                                643                 :                :         }
                                644                 :                : 
                                645   [ +  +  +  - ]:         117558 :         if (SPG_IS_COLLATION_AWARE_STRATEGY(strategy))
                                646                 :                :         {
                                647                 :                :             /* Collation-aware comparison */
                                648                 :         101364 :             strategy -= SPG_STRATEGY_ADDITION;
                                649                 :                : 
                                650                 :                :             /* If asserts enabled, verify encoding of reconstructed string */
 5031 tgl@sss.pgh.pa.us         651         [ -  + ]:         101364 :             Assert(pg_verifymbstr(fullValue, fullLen, false));
                                652                 :                : 
 2803                           653                 :         101364 :             r = varstr_cmp(fullValue, fullLen,
                                654         [ -  + ]:         101364 :                            VARDATA_ANY(query), queryLen,
                                655                 :                :                            PG_GET_COLLATION());
                                656                 :                :         }
                                657                 :                :         else
                                658                 :                :         {
                                659                 :                :             /* Non-collation-aware comparison */
 5031                           660         [ -  + ]:          16194 :             r = memcmp(fullValue, VARDATA_ANY(query), Min(queryLen, fullLen));
                                661                 :                : 
 2803                           662         [ +  + ]:          16194 :             if (r == 0)
                                663                 :                :             {
                                664         [ +  + ]:          12081 :                 if (queryLen > fullLen)
                                665                 :           6012 :                     r = -1;
                                666         [ -  + ]:           6069 :                 else if (queryLen < fullLen)
 2803 tgl@sss.pgh.pa.us         667                 :UBC           0 :                     r = 1;
                                668                 :                :             }
                                669                 :                :         }
                                670                 :                : 
 5031 tgl@sss.pgh.pa.us         671   [ +  +  +  +  :CBC      117558 :         switch (strategy)
                                              +  - ]
                                672                 :                :         {
                                673                 :          27188 :             case BTLessStrategyNumber:
                                674                 :          27188 :                 res = (r < 0);
                                675                 :          27188 :                 break;
                                676                 :          27188 :             case BTLessEqualStrategyNumber:
                                677                 :          27188 :                 res = (r <= 0);
                                678                 :          27188 :                 break;
                                679                 :          12150 :             case BTEqualStrategyNumber:
                                680                 :          12150 :                 res = (r == 0);
                                681                 :          12150 :                 break;
                                682                 :          25516 :             case BTGreaterEqualStrategyNumber:
                                683                 :          25516 :                 res = (r >= 0);
                                684                 :          25516 :                 break;
                                685                 :          25516 :             case BTGreaterStrategyNumber:
                                686                 :          25516 :                 res = (r > 0);
                                687                 :          25516 :                 break;
 5031 tgl@sss.pgh.pa.us         688                 :UBC           0 :             default:
                                689         [ #  # ]:              0 :                 elog(ERROR, "unrecognized strategy number: %d",
                                690                 :                :                      in->scankeys[j].sk_strategy);
                                691                 :                :                 res = false;
                                692                 :                :                 break;
                                693                 :                :         }
                                694                 :                : 
 5031 tgl@sss.pgh.pa.us         695         [ +  + ]:CBC      117558 :         if (!res)
                                696                 :         103809 :             break;              /* no need to consider remaining conditions */
                                697                 :                :     }
                                698                 :                : 
 5115                           699                 :         117750 :     PG_RETURN_BOOL(res);
                                700                 :                : }
        

Generated by: LCOV version 2.4-beta