LCOV - differential code coverage report
Current view: top level - contrib/seg - seg.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: ba12a202ce1b5581dc0ed149cf3f637d7897ad5d vs 2866d8c7dbfc9d882a7d80fef93fbbe763709932 Lines: 74.9 % 431 323 108 3 320 3
Current Date: 2026-08-27 14:31:44 +0300 Functions: 87.9 % 66 58 8 1 57
Baseline: lcov-20260827-baseline Branches: 75.1 % 201 151 50 151
Baseline Date: 2026-08-27 14:31:58 +0300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 3 3 3
(30,360] days: 83.3 % 6 5 1 5
(360..) days: 74.6 % 422 315 107 315
Function coverage date bins:
(360..) days: 87.9 % 66 58 8 1 57
Branch coverage date bins:
(30,360] days: 100.0 % 6 6 6
(360..) days: 74.4 % 195 145 50 145

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * contrib/seg/seg.c
                                  3                 :                :  *
                                  4                 :                :  *
                                  5                 :                :  * This file contains routines that can be bound to a Postgres backend and
                                  6                 :                :  * called by the backend in the process of processing queries.  The calling
                                  7                 :                :  * format for these routines is dictated by Postgres architecture.
                                  8                 :                :  */
                                  9                 :                : 
                                 10                 :                : #include "postgres.h"
                                 11                 :                : 
                                 12                 :                : #include <float.h>
                                 13                 :                : #include <math.h>
                                 14                 :                : 
                                 15                 :                : #include "access/gist.h"
                                 16                 :                : #include "access/stratnum.h"
                                 17                 :                : #include "fmgr.h"
                                 18                 :                : 
                                 19                 :                : #include "segdata.h"
                                 20                 :                : 
                                 21                 :                : 
                                 22                 :                : #define DatumGetSegP(X) ((SEG *) DatumGetPointer(X))
                                 23                 :                : #define PG_GETARG_SEG_P(n) DatumGetSegP(PG_GETARG_DATUM(n))
                                 24                 :                : 
                                 25                 :                : 
                                 26                 :                : /*
                                 27                 :                :  * #define GIST_DEBUG
                                 28                 :                :  * #define GIST_QUERY_DEBUG
                                 29                 :                :  */
                                 30                 :                : 
  519 tgl@sss.pgh.pa.us          31                 :CBC           3 : PG_MODULE_MAGIC_EXT(
                                 32                 :                :                     .name = "seg",
                                 33                 :                :                     .version = PG_VERSION
                                 34                 :                : );
                                 35                 :                : 
                                 36                 :                : /*
                                 37                 :                :  * Auxiliary data structure for picksplit method.
                                 38                 :                :  */
                                 39                 :                : typedef struct
                                 40                 :                : {
                                 41                 :                :     float       center;
                                 42                 :                :     OffsetNumber index;
                                 43                 :                :     SEG        *data;
                                 44                 :                : } gseg_picksplit_item;
                                 45                 :                : 
                                 46                 :                : /*
                                 47                 :                :  * Input/Output routines
                                 48                 :                :  */
 6705 alvherre@alvh.no-ip.       49                 :              4 : PG_FUNCTION_INFO_V1(seg_in);
                                 50                 :              3 : PG_FUNCTION_INFO_V1(seg_out);
 6704 tgl@sss.pgh.pa.us          51                 :              2 : PG_FUNCTION_INFO_V1(seg_size);
 6705 alvherre@alvh.no-ip.       52                 :              3 : PG_FUNCTION_INFO_V1(seg_lower);
                                 53                 :              3 : PG_FUNCTION_INFO_V1(seg_upper);
                                 54                 :              3 : PG_FUNCTION_INFO_V1(seg_center);
                                 55                 :                : 
                                 56                 :                : /*
                                 57                 :                :  * GiST support methods
                                 58                 :                :  */
 3438 andres@anarazel.de         59                 :              3 : PG_FUNCTION_INFO_V1(gseg_consistent);
                                 60                 :              2 : PG_FUNCTION_INFO_V1(gseg_compress);
                                 61                 :              2 : PG_FUNCTION_INFO_V1(gseg_decompress);
                                 62                 :              3 : PG_FUNCTION_INFO_V1(gseg_picksplit);
                                 63                 :              3 : PG_FUNCTION_INFO_V1(gseg_penalty);
                                 64                 :              3 : PG_FUNCTION_INFO_V1(gseg_union);
                                 65                 :              3 : PG_FUNCTION_INFO_V1(gseg_same);
                                 66                 :                : static Datum gseg_leaf_consistent(Datum key, Datum query, StrategyNumber strategy);
                                 67                 :                : static Datum gseg_internal_consistent(Datum key, Datum query, StrategyNumber strategy);
                                 68                 :                : static Datum gseg_binary_union(Datum r1, Datum r2, int *sizep);
                                 69                 :                : 
                                 70                 :                : 
                                 71                 :                : /*
                                 72                 :                :  * R-tree support functions
                                 73                 :                :  */
                                 74                 :              3 : PG_FUNCTION_INFO_V1(seg_same);
                                 75                 :              3 : PG_FUNCTION_INFO_V1(seg_contains);
                                 76                 :              3 : PG_FUNCTION_INFO_V1(seg_contained);
                                 77                 :              3 : PG_FUNCTION_INFO_V1(seg_overlap);
                                 78                 :              3 : PG_FUNCTION_INFO_V1(seg_left);
                                 79                 :              3 : PG_FUNCTION_INFO_V1(seg_over_left);
                                 80                 :              3 : PG_FUNCTION_INFO_V1(seg_right);
                                 81                 :              3 : PG_FUNCTION_INFO_V1(seg_over_right);
                                 82                 :              2 : PG_FUNCTION_INFO_V1(seg_union);
                                 83                 :              2 : PG_FUNCTION_INFO_V1(seg_inter);
                                 84                 :                : static void rt_seg_size(SEG *a, float *size);
                                 85                 :                : 
                                 86                 :                : /*
                                 87                 :                :  * Various operators
                                 88                 :                :  */
                                 89                 :              4 : PG_FUNCTION_INFO_V1(seg_cmp);
                                 90                 :              2 : PG_FUNCTION_INFO_V1(seg_lt);
                                 91                 :              2 : PG_FUNCTION_INFO_V1(seg_le);
                                 92                 :              2 : PG_FUNCTION_INFO_V1(seg_gt);
                                 93                 :              2 : PG_FUNCTION_INFO_V1(seg_ge);
                                 94                 :              3 : PG_FUNCTION_INFO_V1(seg_different);
                                 95                 :                : 
                                 96                 :                : /*
                                 97                 :                :  * Auxiliary functions
                                 98                 :                :  */
                                 99                 :                : static int  restore(char *result, float val, int n);
                                100                 :                : 
                                101                 :                : 
                                102                 :                : /*****************************************************************************
                                103                 :                :  * Input/Output functions
                                104                 :                :  *****************************************************************************/
                                105                 :                : 
                                106                 :                : Datum
 6705 alvherre@alvh.no-ip.      107                 :           2832 : seg_in(PG_FUNCTION_ARGS)
                                108                 :                : {
                                109                 :           2832 :     char       *str = PG_GETARG_CSTRING(0);
  265 michael@paquier.xyz       110                 :           2832 :     SEG        *result = palloc_object(SEG);
                                111                 :                :     yyscan_t    scanner;
                                112                 :                : 
  617 peter@eisentraut.org      113                 :           2832 :     seg_scanner_init(str, &scanner);
                                114                 :                : 
                                115         [ +  + ]:           2832 :     if (seg_yyparse(result, fcinfo->context, scanner) != 0)
                                116                 :              8 :         seg_yyerror(result, fcinfo->context, scanner, "bogus input");
                                117                 :                : 
                                118                 :           2823 :     seg_scanner_finish(scanner);
                                119                 :                : 
 6705 alvherre@alvh.no-ip.      120                 :           2823 :     PG_RETURN_POINTER(result);
                                121                 :                : }
                                122                 :                : 
                                123                 :                : Datum
                                124                 :            216 : seg_out(PG_FUNCTION_ARGS)
                                125                 :                : {
 3438 andres@anarazel.de        126                 :            216 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                127                 :                :     char       *result;
                                128                 :                :     char       *p;
                                129                 :                : 
 9289 bruce@momjian.us          130                 :            216 :     p = result = (char *) palloc(40);
                                131                 :                : 
                                132   [ +  +  +  +  :            216 :     if (seg->l_ext == '>' || seg->l_ext == '<' || seg->l_ext == '~')
                                              +  + ]
                                133                 :             26 :         p += sprintf(p, "%c", seg->l_ext);
                                134                 :                : 
                                135   [ +  +  +  - ]:            216 :     if (seg->lower == seg->upper && seg->l_ext == seg->u_ext)
                                136                 :                :     {
                                137                 :                :         /*
                                138                 :                :          * indicates that this interval was built by seg_in off a single point
                                139                 :                :          */
                                140                 :             47 :         p += restore(p, seg->lower, seg->l_sigd);
                                141                 :                :     }
                                142                 :                :     else
                                143                 :                :     {
                                144         [ +  + ]:            169 :         if (seg->l_ext != '-')
                                145                 :                :         {
                                146                 :                :             /* print the lower boundary if exists */
                                147                 :            162 :             p += restore(p, seg->lower, seg->l_sigd);
                                148                 :            162 :             p += sprintf(p, " ");
                                149                 :                :         }
                                150                 :            169 :         p += sprintf(p, "..");
                                151         [ +  + ]:            169 :         if (seg->u_ext != '-')
                                152                 :                :         {
                                153                 :                :             /* print the upper boundary if exists */
                                154                 :            130 :             p += sprintf(p, " ");
   77 heikki.linnakangas@i      155   [ +  +  +  +  :            130 :             if (seg->u_ext == '>' || seg->u_ext == '<' || seg->u_ext == '~')
                                              +  + ]
 9289 bruce@momjian.us          156                 :             36 :                 p += sprintf(p, "%c", seg->u_ext);
                                157                 :            130 :             p += restore(p, seg->upper, seg->u_sigd);
                                158                 :                :         }
                                159                 :                :     }
                                160                 :                : 
 6705 alvherre@alvh.no-ip.      161                 :            216 :     PG_RETURN_CSTRING(result);
                                162                 :                : }
                                163                 :                : 
                                164                 :                : Datum
                                165                 :            143 : seg_center(PG_FUNCTION_ARGS)
                                166                 :                : {
 3438 andres@anarazel.de        167                 :            143 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                168                 :                : 
 6705 alvherre@alvh.no-ip.      169                 :            143 :     PG_RETURN_FLOAT4(((float) seg->lower + (float) seg->upper) / 2.0);
                                170                 :                : }
                                171                 :                : 
                                172                 :                : Datum
                                173                 :            143 : seg_lower(PG_FUNCTION_ARGS)
                                174                 :                : {
 3438 andres@anarazel.de        175                 :            143 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                176                 :                : 
 6705 alvherre@alvh.no-ip.      177                 :            143 :     PG_RETURN_FLOAT4(seg->lower);
                                178                 :                : }
                                179                 :                : 
                                180                 :                : Datum
                                181                 :            143 : seg_upper(PG_FUNCTION_ARGS)
                                182                 :                : {
 3438 andres@anarazel.de        183                 :            143 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                184                 :                : 
 6705 alvherre@alvh.no-ip.      185                 :            143 :     PG_RETURN_FLOAT4(seg->upper);
                                186                 :                : }
                                187                 :                : 
                                188                 :                : 
                                189                 :                : /*****************************************************************************
                                190                 :                :  *                         GiST functions
                                191                 :                :  *****************************************************************************/
                                192                 :                : 
                                193                 :                : /*
                                194                 :                :  * The GiST Consistent method for segments
                                195                 :                :  * Should return false if for all data items x below entry,
                                196                 :                :  * the predicate x op query == false, where op is the oper
                                197                 :                :  * corresponding to strategy in the pg_amop table.
                                198                 :                :  */
                                199                 :                : Datum
 3438 andres@anarazel.de        200                 :           5724 : gseg_consistent(PG_FUNCTION_ARGS)
                                201                 :                : {
                                202                 :           5724 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
                                203                 :           5724 :     Datum       query = PG_GETARG_DATUM(1);
                                204                 :           5724 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
                                205                 :                : #ifdef NOT_USED
                                206                 :                :     Oid         subtype = PG_GETARG_OID(3);
                                207                 :                : #endif
                                208                 :           5724 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
                                209                 :                : 
                                210                 :                :     /* All cases served by this function are exact */
 6709 tgl@sss.pgh.pa.us         211                 :           5724 :     *recheck = false;
                                212                 :                : 
                                213                 :                :     /*
                                214                 :                :      * if entry is not leaf, use gseg_internal_consistent, else use
                                215                 :                :      * gseg_leaf_consistent
                                216                 :                :      */
 9289 bruce@momjian.us          217         [ +  + ]:           5724 :     if (GIST_LEAF(entry))
 3438 andres@anarazel.de        218                 :           5652 :         return gseg_leaf_consistent(entry->key, query, strategy);
                                219                 :                :     else
                                220                 :             72 :         return gseg_internal_consistent(entry->key, query, strategy);
                                221                 :                : }
                                222                 :                : 
                                223                 :                : /*
                                224                 :                :  * The GiST Union method for segments
                                225                 :                :  * returns the minimal bounding seg that encloses all the entries in entryvec
                                226                 :                :  */
                                227                 :                : Datum
                                228                 :           2316 : gseg_union(PG_FUNCTION_ARGS)
                                229                 :                : {
                                230                 :           2316 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
                                231                 :           2316 :     int        *sizep = (int *) PG_GETARG_POINTER(1);
                                232                 :                :     int         numranges,
                                233                 :                :                 i;
                                234                 :           2316 :     Datum       out = 0;
                                235                 :                :     Datum       tmp;
                                236                 :                : 
                                237                 :                : #ifdef GIST_DEBUG
                                238                 :                :     fprintf(stderr, "union\n");
                                239                 :                : #endif
                                240                 :                : 
 8185 teodor@sigaev.ru          241                 :           2316 :     numranges = entryvec->n;
 3438 andres@anarazel.de        242                 :           2316 :     tmp = entryvec->vector[0].key;
 9289 bruce@momjian.us          243                 :           2316 :     *sizep = sizeof(SEG);
                                244                 :                : 
                                245         [ +  + ]:           4632 :     for (i = 1; i < numranges; i++)
                                246                 :                :     {
 3438 andres@anarazel.de        247                 :           2316 :         out = gseg_binary_union(tmp, entryvec->vector[i].key, sizep);
 9289 bruce@momjian.us          248                 :           2316 :         tmp = out;
                                249                 :                :     }
                                250                 :                : 
 3438 andres@anarazel.de        251                 :           2316 :     PG_RETURN_DATUM(out);
                                252                 :                : }
                                253                 :                : 
                                254                 :                : /*
                                255                 :                :  * GiST Compress and Decompress methods for segments
                                256                 :                :  * do not do anything.
                                257                 :                :  */
                                258                 :                : Datum
 3438 andres@anarazel.de        259                 :UBC           0 : gseg_compress(PG_FUNCTION_ARGS)
                                260                 :                : {
                                261                 :              0 :     PG_RETURN_POINTER(PG_GETARG_POINTER(0));
                                262                 :                : }
                                263                 :                : 
                                264                 :                : Datum
                                265                 :              0 : gseg_decompress(PG_FUNCTION_ARGS)
                                266                 :                : {
                                267                 :              0 :     PG_RETURN_POINTER(PG_GETARG_POINTER(0));
                                268                 :                : }
                                269                 :                : 
                                270                 :                : /*
                                271                 :                :  * The GiST Penalty method for segments
                                272                 :                :  * As in the R-tree paper, we use change in area as our penalty metric
                                273                 :                :  */
                                274                 :                : Datum
 3438 andres@anarazel.de        275                 :CBC        5128 : gseg_penalty(PG_FUNCTION_ARGS)
                                276                 :                : {
                                277                 :           5128 :     GISTENTRY  *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
                                278                 :           5128 :     GISTENTRY  *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
                                279                 :           5128 :     float      *result = (float *) PG_GETARG_POINTER(2);
                                280                 :                :     SEG        *ud;
                                281                 :                :     float       tmp1,
                                282                 :                :                 tmp2;
                                283                 :                : 
                                284                 :           5128 :     ud = DatumGetSegP(DirectFunctionCall2(seg_union,
                                285                 :                :                                           origentry->key,
                                286                 :                :                                           newentry->key));
 9219 tgl@sss.pgh.pa.us         287                 :           5128 :     rt_seg_size(ud, &tmp1);
 3438 andres@anarazel.de        288                 :           5128 :     rt_seg_size(DatumGetSegP(origentry->key), &tmp2);
 9289 bruce@momjian.us          289                 :           5128 :     *result = tmp1 - tmp2;
                                290                 :                : 
                                291                 :                : #ifdef GIST_DEBUG
                                292                 :                :     fprintf(stderr, "penalty\n");
                                293                 :                :     fprintf(stderr, "\t%g\n", *result);
                                294                 :                : #endif
                                295                 :                : 
 3438 andres@anarazel.de        296                 :           5128 :     PG_RETURN_POINTER(result);
                                297                 :                : }
                                298                 :                : 
                                299                 :                : /*
                                300                 :                :  * Compare function for gseg_picksplit_item: sort by center.
                                301                 :                :  */
                                302                 :                : static int
 5734 tgl@sss.pgh.pa.us         303                 :          29509 : gseg_picksplit_item_cmp(const void *a, const void *b)
                                304                 :                : {
                                305                 :          29509 :     const gseg_picksplit_item *i1 = (const gseg_picksplit_item *) a;
                                306                 :          29509 :     const gseg_picksplit_item *i2 = (const gseg_picksplit_item *) b;
                                307                 :                : 
                                308         [ +  + ]:          29509 :     if (i1->center < i2->center)
                                309                 :          12554 :         return -1;
                                310         [ +  + ]:          16955 :     else if (i1->center == i2->center)
                                311                 :           5333 :         return 0;
                                312                 :                :     else
                                313                 :          11622 :         return 1;
                                314                 :                : }
                                315                 :                : 
                                316                 :                : /*
                                317                 :                :  * The GiST PickSplit method for segments
                                318                 :                :  *
                                319                 :                :  * We used to use Guttman's split algorithm here, but since the data is 1-D
                                320                 :                :  * it's easier and more robust to just sort the segments by center-point and
                                321                 :                :  * split at the middle.
                                322                 :                :  */
                                323                 :                : Datum
 3438 andres@anarazel.de        324                 :             17 : gseg_picksplit(PG_FUNCTION_ARGS)
                                325                 :                : {
                                326                 :             17 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
                                327                 :             17 :     GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
                                328                 :                :     int         i;
                                329                 :                :     SEG        *seg,
                                330                 :                :                *seg_l,
                                331                 :                :                *seg_r;
                                332                 :                :     gseg_picksplit_item *sort_items;
                                333                 :                :     OffsetNumber *left,
                                334                 :                :                *right;
                                335                 :                :     OffsetNumber maxoff;
                                336                 :                :     OffsetNumber firstright;
                                337                 :                : 
                                338                 :                : #ifdef GIST_DEBUG
                                339                 :                :     fprintf(stderr, "picksplit\n");
                                340                 :                : #endif
                                341                 :                : 
                                342                 :                :     /* Valid items in entryvec->vector[] are indexed 1..maxoff */
 5734 tgl@sss.pgh.pa.us         343                 :             17 :     maxoff = entryvec->n - 1;
                                344                 :                : 
                                345                 :                :     /*
                                346                 :                :      * Prepare the auxiliary array and sort it.
                                347                 :                :      */
   10 michael@paquier.xyz       348                 :GNC          17 :     sort_items = palloc_array(gseg_picksplit_item, maxoff);
 5734 tgl@sss.pgh.pa.us         349         [ +  + ]:CBC        4471 :     for (i = 1; i <= maxoff; i++)
                                350                 :                :     {
 3438 andres@anarazel.de        351                 :           4454 :         seg = DatumGetSegP(entryvec->vector[i].key);
                                352                 :                :         /* center calculation is done this way to avoid possible overflow */
 5618 bruce@momjian.us          353                 :           4454 :         sort_items[i - 1].center = seg->lower * 0.5f + seg->upper * 0.5f;
 5734 tgl@sss.pgh.pa.us         354                 :           4454 :         sort_items[i - 1].index = i;
                                355                 :           4454 :         sort_items[i - 1].data = seg;
                                356                 :                :     }
                                357                 :             17 :     qsort(sort_items, maxoff, sizeof(gseg_picksplit_item),
                                358                 :                :           gseg_picksplit_item_cmp);
                                359                 :                : 
                                360                 :                :     /* sort items below "firstright" will go into the left side */
                                361                 :             17 :     firstright = maxoff / 2;
                                362                 :                : 
   10 michael@paquier.xyz       363                 :GNC          17 :     v->spl_left = palloc_array(OffsetNumber, maxoff);
                                364                 :             17 :     v->spl_right = palloc_array(OffsetNumber, maxoff);
 9289 bruce@momjian.us          365                 :CBC          17 :     left = v->spl_left;
                                366                 :             17 :     v->spl_nleft = 0;
                                367                 :             17 :     right = v->spl_right;
                                368                 :             17 :     v->spl_nright = 0;
                                369                 :                : 
                                370                 :                :     /*
                                371                 :                :      * Emit segments to the left output page, and compute its bounding box.
                                372                 :                :      */
  265 michael@paquier.xyz       373                 :             17 :     seg_l = palloc_object(SEG);
 3438 andres@anarazel.de        374                 :             17 :     memcpy(seg_l, sort_items[0].data, sizeof(SEG));
 5734 tgl@sss.pgh.pa.us         375                 :             17 :     *left++ = sort_items[0].index;
                                376                 :             17 :     v->spl_nleft++;
                                377         [ +  + ]:           2227 :     for (i = 1; i < firstright; i++)
                                378                 :                :     {
 3438 andres@anarazel.de        379                 :           2210 :         Datum       sortitem = PointerGetDatum(sort_items[i].data);
                                380                 :                : 
                                381                 :           2210 :         seg_l = DatumGetSegP(DirectFunctionCall2(seg_union,
                                382                 :                :                                                  PointerGetDatum(seg_l),
                                383                 :                :                                                  sortitem));
 5734 tgl@sss.pgh.pa.us         384                 :           2210 :         *left++ = sort_items[i].index;
                                385                 :           2210 :         v->spl_nleft++;
                                386                 :                :     }
                                387                 :                : 
                                388                 :                :     /*
                                389                 :                :      * Likewise for the right page.
                                390                 :                :      */
  265 michael@paquier.xyz       391                 :             17 :     seg_r = palloc_object(SEG);
 3438 andres@anarazel.de        392                 :             17 :     memcpy(seg_r, sort_items[firstright].data, sizeof(SEG));
 5734 tgl@sss.pgh.pa.us         393                 :             17 :     *right++ = sort_items[firstright].index;
                                394                 :             17 :     v->spl_nright++;
                                395         [ +  + ]:           2227 :     for (i = firstright + 1; i < maxoff; i++)
                                396                 :                :     {
 3438 andres@anarazel.de        397                 :           2210 :         Datum       sortitem = PointerGetDatum(sort_items[i].data);
                                398                 :                : 
                                399                 :           2210 :         seg_r = DatumGetSegP(DirectFunctionCall2(seg_union,
                                400                 :                :                                                  PointerGetDatum(seg_r),
                                401                 :                :                                                  sortitem));
 5734 tgl@sss.pgh.pa.us         402                 :           2210 :         *right++ = sort_items[i].index;
                                403                 :           2210 :         v->spl_nright++;
                                404                 :                :     }
                                405                 :                : 
 3438 andres@anarazel.de        406                 :             17 :     v->spl_ldatum = PointerGetDatum(seg_l);
                                407                 :             17 :     v->spl_rdatum = PointerGetDatum(seg_r);
                                408                 :                : 
                                409                 :             17 :     PG_RETURN_POINTER(v);
                                410                 :                : }
                                411                 :                : 
                                412                 :                : /*
                                413                 :                :  * Equality methods
                                414                 :                :  */
                                415                 :                : Datum
                                416                 :           2315 : gseg_same(PG_FUNCTION_ARGS)
                                417                 :                : {
                                418                 :           2315 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
                                419                 :                : 
  384 peter@eisentraut.org      420         [ +  + ]:           2315 :     if (DatumGetBool(DirectFunctionCall2(seg_same, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1))))
 3298 peter_e@gmx.net           421                 :           2276 :         *result = true;
                                422                 :                :     else
                                423                 :             39 :         *result = false;
                                424                 :                : 
                                425                 :                : #ifdef GIST_DEBUG
                                426                 :                :     fprintf(stderr, "same: %s\n", (*result ? "TRUE" : "FALSE"));
                                427                 :                : #endif
                                428                 :                : 
 3438 andres@anarazel.de        429                 :           2315 :     PG_RETURN_POINTER(result);
                                430                 :                : }
                                431                 :                : 
                                432                 :                : /*
                                433                 :                :  * SUPPORT ROUTINES
                                434                 :                :  */
                                435                 :                : static Datum
                                436                 :           5652 : gseg_leaf_consistent(Datum key, Datum query, StrategyNumber strategy)
                                437                 :                : {
                                438                 :                :     Datum       retval;
                                439                 :                : 
                                440                 :                : #ifdef GIST_QUERY_DEBUG
                                441                 :                :     fprintf(stderr, "leaf_consistent, %d\n", strategy);
                                442                 :                : #endif
                                443                 :                : 
 9289 bruce@momjian.us          444   [ -  -  -  -  :           5652 :     switch (strategy)
                                        -  -  +  -  
                                                 - ]
                                445                 :                :     {
 9289 bruce@momjian.us          446                 :UBC           0 :         case RTLeftStrategyNumber:
 3438 andres@anarazel.de        447                 :              0 :             retval = DirectFunctionCall2(seg_left, key, query);
 9289 bruce@momjian.us          448                 :              0 :             break;
                                449                 :              0 :         case RTOverLeftStrategyNumber:
 3438 andres@anarazel.de        450                 :              0 :             retval = DirectFunctionCall2(seg_over_left, key, query);
 9289 bruce@momjian.us          451                 :              0 :             break;
                                452                 :              0 :         case RTOverlapStrategyNumber:
 3438 andres@anarazel.de        453                 :              0 :             retval = DirectFunctionCall2(seg_overlap, key, query);
 9289 bruce@momjian.us          454                 :              0 :             break;
                                455                 :              0 :         case RTOverRightStrategyNumber:
 3438 andres@anarazel.de        456                 :              0 :             retval = DirectFunctionCall2(seg_over_right, key, query);
 9289 bruce@momjian.us          457                 :              0 :             break;
                                458                 :              0 :         case RTRightStrategyNumber:
 3438 andres@anarazel.de        459                 :              0 :             retval = DirectFunctionCall2(seg_right, key, query);
 9289 bruce@momjian.us          460                 :              0 :             break;
                                461                 :              0 :         case RTSameStrategyNumber:
 3438 andres@anarazel.de        462                 :              0 :             retval = DirectFunctionCall2(seg_same, key, query);
 9289 bruce@momjian.us          463                 :              0 :             break;
 9289 bruce@momjian.us          464                 :CBC        5652 :         case RTContainsStrategyNumber:
                                465                 :                :         case RTOldContainsStrategyNumber:
 3438 andres@anarazel.de        466                 :           5652 :             retval = DirectFunctionCall2(seg_contains, key, query);
 9289 bruce@momjian.us          467                 :           5652 :             break;
 9289 bruce@momjian.us          468                 :UBC           0 :         case RTContainedByStrategyNumber:
                                469                 :                :         case RTOldContainedByStrategyNumber:
 3438 andres@anarazel.de        470                 :              0 :             retval = DirectFunctionCall2(seg_contained, key, query);
 9289 bruce@momjian.us          471                 :              0 :             break;
                                472                 :              0 :         default:
  384 peter@eisentraut.org      473                 :              0 :             retval = BoolGetDatum(false);
                                474                 :                :     }
                                475                 :                : 
 3438 andres@anarazel.de        476                 :CBC        5652 :     PG_RETURN_DATUM(retval);
                                477                 :                : }
                                478                 :                : 
                                479                 :                : static Datum
                                480                 :             72 : gseg_internal_consistent(Datum key, Datum query, StrategyNumber strategy)
                                481                 :                : {
                                482                 :                :     bool        retval;
                                483                 :                : 
                                484                 :                : #ifdef GIST_QUERY_DEBUG
                                485                 :                :     fprintf(stderr, "internal_consistent, %d\n", strategy);
                                486                 :                : #endif
                                487                 :                : 
 9289 bruce@momjian.us          488   [ -  -  -  -  :             72 :     switch (strategy)
                                        -  +  -  - ]
                                489                 :                :     {
 9289 bruce@momjian.us          490                 :UBC           0 :         case RTLeftStrategyNumber:
 3438 andres@anarazel.de        491                 :              0 :             retval =
                                492                 :              0 :                 !DatumGetBool(DirectFunctionCall2(seg_over_right, key, query));
 7731 tgl@sss.pgh.pa.us         493                 :              0 :             break;
 9289 bruce@momjian.us          494                 :              0 :         case RTOverLeftStrategyNumber:
 3438 andres@anarazel.de        495                 :              0 :             retval =
                                496                 :              0 :                 !DatumGetBool(DirectFunctionCall2(seg_right, key, query));
 9289 bruce@momjian.us          497                 :              0 :             break;
                                498                 :              0 :         case RTOverlapStrategyNumber:
                                499                 :                :             retval =
 3438 andres@anarazel.de        500                 :              0 :                 DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));
 9289 bruce@momjian.us          501                 :              0 :             break;
                                502                 :              0 :         case RTOverRightStrategyNumber:
 3438 andres@anarazel.de        503                 :              0 :             retval =
                                504                 :              0 :                 !DatumGetBool(DirectFunctionCall2(seg_left, key, query));
 7731 tgl@sss.pgh.pa.us         505                 :              0 :             break;
 9289 bruce@momjian.us          506                 :              0 :         case RTRightStrategyNumber:
 3438 andres@anarazel.de        507                 :              0 :             retval =
                                508                 :              0 :                 !DatumGetBool(DirectFunctionCall2(seg_over_left, key, query));
 9289 bruce@momjian.us          509                 :              0 :             break;
 9289 bruce@momjian.us          510                 :CBC          72 :         case RTSameStrategyNumber:
                                511                 :                :         case RTContainsStrategyNumber:
                                512                 :                :         case RTOldContainsStrategyNumber:
                                513                 :                :             retval =
 3438 andres@anarazel.de        514                 :             72 :                 DatumGetBool(DirectFunctionCall2(seg_contains, key, query));
 9289 bruce@momjian.us          515                 :             72 :             break;
 9289 bruce@momjian.us          516                 :UBC           0 :         case RTContainedByStrategyNumber:
                                517                 :                :         case RTOldContainedByStrategyNumber:
                                518                 :                :             retval =
 3438 andres@anarazel.de        519                 :              0 :                 DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));
 9289 bruce@momjian.us          520                 :              0 :             break;
                                521                 :              0 :         default:
 3298 peter_e@gmx.net           522                 :              0 :             retval = false;
                                523                 :                :     }
                                524                 :                : 
 3438 andres@anarazel.de        525                 :CBC          72 :     PG_RETURN_BOOL(retval);
                                526                 :                : }
                                527                 :                : 
                                528                 :                : static Datum
                                529                 :           2316 : gseg_binary_union(Datum r1, Datum r2, int *sizep)
                                530                 :                : {
                                531                 :                :     Datum       retval;
                                532                 :                : 
                                533                 :           2316 :     retval = DirectFunctionCall2(seg_union, r1, r2);
 9289 bruce@momjian.us          534                 :           2316 :     *sizep = sizeof(SEG);
                                535                 :                : 
 3297 peter_e@gmx.net           536                 :           2316 :     return retval;
                                537                 :                : }
                                538                 :                : 
                                539                 :                : 
                                540                 :                : Datum
 3438 andres@anarazel.de        541                 :           5739 : seg_contains(PG_FUNCTION_ARGS)
                                542                 :                : {
                                543                 :           5739 :     SEG        *a = PG_GETARG_SEG_P(0);
                                544                 :           5739 :     SEG        *b = PG_GETARG_SEG_P(1);
                                545                 :                : 
                                546   [ +  +  +  + ]:           5739 :     PG_RETURN_BOOL((a->lower <= b->lower) && (a->upper >= b->upper));
                                547                 :                : }
                                548                 :                : 
                                549                 :                : Datum
                                550                 :             14 : seg_contained(PG_FUNCTION_ARGS)
                                551                 :                : {
                                552                 :             14 :     Datum       a = PG_GETARG_DATUM(0);
                                553                 :             14 :     Datum       b = PG_GETARG_DATUM(1);
                                554                 :                : 
                                555                 :             14 :     PG_RETURN_DATUM(DirectFunctionCall2(seg_contains, b, a));
                                556                 :                : }
                                557                 :                : 
                                558                 :                : /*****************************************************************************
                                559                 :                :  * Operator class for R-tree indexing
                                560                 :                :  *****************************************************************************/
                                561                 :                : 
                                562                 :                : Datum
                                563                 :           2459 : seg_same(PG_FUNCTION_ARGS)
                                564                 :                : {
 2401 alvherre@alvh.no-ip.      565                 :           2459 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                566                 :                :                                                         PG_GETARG_DATUM(0),
                                567                 :                :                                                         PG_GETARG_DATUM(1)));
                                568                 :                : 
 3438 andres@anarazel.de        569                 :           2459 :     PG_RETURN_BOOL(cmp == 0);
                                570                 :                : }
                                571                 :                : 
                                572                 :                : /*
                                573                 :                :  * seg_overlap -- does a overlap b?
                                574                 :                :  */
                                575                 :                : Datum
                                576                 :             15 : seg_overlap(PG_FUNCTION_ARGS)
                                577                 :                : {
                                578                 :             15 :     SEG        *a = PG_GETARG_SEG_P(0);
                                579                 :             15 :     SEG        *b = PG_GETARG_SEG_P(1);
                                580                 :                : 
                                581   [ +  +  +  +  :             15 :     PG_RETURN_BOOL(((a->upper >= b->upper) && (a->lower <= b->upper)) ||
                                        +  +  +  + ]
                                582                 :                :                    ((b->upper >= a->upper) && (b->lower <= a->upper)));
                                583                 :                : }
                                584                 :                : 
                                585                 :                : /*
                                586                 :                :  * seg_over_left -- is the right edge of (a) located at or left of the right edge of (b)?
                                587                 :                :  */
                                588                 :                : Datum
                                589                 :             11 : seg_over_left(PG_FUNCTION_ARGS)
                                590                 :                : {
                                591                 :             11 :     SEG        *a = PG_GETARG_SEG_P(0);
                                592                 :             11 :     SEG        *b = PG_GETARG_SEG_P(1);
                                593                 :                : 
                                594                 :             11 :     PG_RETURN_BOOL(a->upper <= b->upper);
                                595                 :                : }
                                596                 :                : 
                                597                 :                : /*
                                598                 :                :  * seg_left -- is (a) entirely on the left of (b)?
                                599                 :                :  */
                                600                 :                : Datum
                                601                 :             11 : seg_left(PG_FUNCTION_ARGS)
                                602                 :                : {
                                603                 :             11 :     SEG        *a = PG_GETARG_SEG_P(0);
                                604                 :             11 :     SEG        *b = PG_GETARG_SEG_P(1);
                                605                 :                : 
                                606                 :             11 :     PG_RETURN_BOOL(a->upper < b->lower);
                                607                 :                : }
                                608                 :                : 
                                609                 :                : /*
                                610                 :                :  * seg_right -- is (a) entirely on the right of (b)?
                                611                 :                :  */
                                612                 :                : Datum
                                613                 :             11 : seg_right(PG_FUNCTION_ARGS)
                                614                 :                : {
                                615                 :             11 :     SEG        *a = PG_GETARG_SEG_P(0);
                                616                 :             11 :     SEG        *b = PG_GETARG_SEG_P(1);
                                617                 :                : 
                                618                 :             11 :     PG_RETURN_BOOL(a->lower > b->upper);
                                619                 :                : }
                                620                 :                : 
                                621                 :                : /*
                                622                 :                :  * seg_over_right -- is the left edge of (a) located at or right of the left edge of (b)?
                                623                 :                :  */
                                624                 :                : Datum
                                625                 :             11 : seg_over_right(PG_FUNCTION_ARGS)
                                626                 :                : {
                                627                 :             11 :     SEG        *a = PG_GETARG_SEG_P(0);
                                628                 :             11 :     SEG        *b = PG_GETARG_SEG_P(1);
                                629                 :                : 
                                630                 :             11 :     PG_RETURN_BOOL(a->lower >= b->lower);
                                631                 :                : }
                                632                 :                : 
                                633                 :                : Datum
                                634                 :          11864 : seg_union(PG_FUNCTION_ARGS)
                                635                 :                : {
                                636                 :          11864 :     SEG        *a = PG_GETARG_SEG_P(0);
                                637                 :          11864 :     SEG        *b = PG_GETARG_SEG_P(1);
                                638                 :                :     SEG        *n;
                                639                 :                : 
  265 michael@paquier.xyz       640                 :          11864 :     n = palloc_object(SEG);
                                641                 :                : 
                                642                 :                :     /* take max of upper endpoints */
 9289 bruce@momjian.us          643         [ +  + ]:          11864 :     if (a->upper > b->upper)
                                644                 :                :     {
                                645                 :          10626 :         n->upper = a->upper;
                                646                 :          10626 :         n->u_sigd = a->u_sigd;
                                647                 :          10626 :         n->u_ext = a->u_ext;
                                648                 :                :     }
                                649                 :                :     else
                                650                 :                :     {
                                651                 :           1238 :         n->upper = b->upper;
                                652                 :           1238 :         n->u_sigd = b->u_sigd;
                                653                 :           1238 :         n->u_ext = b->u_ext;
                                654                 :                :     }
                                655                 :                : 
                                656                 :                :     /* take min of lower endpoints */
                                657         [ +  + ]:          11864 :     if (a->lower < b->lower)
                                658                 :                :     {
                                659                 :          11504 :         n->lower = a->lower;
                                660                 :          11504 :         n->l_sigd = a->l_sigd;
                                661                 :          11504 :         n->l_ext = a->l_ext;
                                662                 :                :     }
                                663                 :                :     else
                                664                 :                :     {
                                665                 :            360 :         n->lower = b->lower;
                                666                 :            360 :         n->l_sigd = b->l_sigd;
                                667                 :            360 :         n->l_ext = b->l_ext;
                                668                 :                :     }
                                669                 :                : 
 3438 andres@anarazel.de        670                 :          11864 :     PG_RETURN_POINTER(n);
                                671                 :                : }
                                672                 :                : 
                                673                 :                : Datum
 3438 andres@anarazel.de        674                 :UBC           0 : seg_inter(PG_FUNCTION_ARGS)
                                675                 :                : {
                                676                 :              0 :     SEG        *a = PG_GETARG_SEG_P(0);
                                677                 :              0 :     SEG        *b = PG_GETARG_SEG_P(1);
                                678                 :                :     SEG        *n;
                                679                 :                : 
  265 michael@paquier.xyz       680                 :              0 :     n = palloc_object(SEG);
                                681                 :                : 
                                682                 :                :     /* take min of upper endpoints */
 9289 bruce@momjian.us          683         [ #  # ]:              0 :     if (a->upper < b->upper)
                                684                 :                :     {
                                685                 :              0 :         n->upper = a->upper;
                                686                 :              0 :         n->u_sigd = a->u_sigd;
                                687                 :              0 :         n->u_ext = a->u_ext;
                                688                 :                :     }
                                689                 :                :     else
                                690                 :                :     {
                                691                 :              0 :         n->upper = b->upper;
                                692                 :              0 :         n->u_sigd = b->u_sigd;
                                693                 :              0 :         n->u_ext = b->u_ext;
                                694                 :                :     }
                                695                 :                : 
                                696                 :                :     /* take max of lower endpoints */
                                697         [ #  # ]:              0 :     if (a->lower > b->lower)
                                698                 :                :     {
                                699                 :              0 :         n->lower = a->lower;
                                700                 :              0 :         n->l_sigd = a->l_sigd;
                                701                 :              0 :         n->l_ext = a->l_ext;
                                702                 :                :     }
                                703                 :                :     else
                                704                 :                :     {
                                705                 :              0 :         n->lower = b->lower;
                                706                 :              0 :         n->l_sigd = b->l_sigd;
                                707                 :              0 :         n->l_ext = b->l_ext;
                                708                 :                :     }
                                709                 :                : 
 3438 andres@anarazel.de        710                 :              0 :     PG_RETURN_POINTER(n);
                                711                 :                : }
                                712                 :                : 
                                713                 :                : static void
 6286 bruce@momjian.us          714                 :CBC       10256 : rt_seg_size(SEG *a, float *size)
                                715                 :                : {
 9289                           716   [ +  -  -  + ]:          10256 :     if (a == (SEG *) NULL || a->upper <= a->lower)
 9289 bruce@momjian.us          717                 :UBC           0 :         *size = 0.0;
                                718                 :                :     else
 1419 peter@eisentraut.org      719                 :CBC       10256 :         *size = fabsf(a->upper - a->lower);
 9390 tgl@sss.pgh.pa.us         720                 :          10256 : }
                                721                 :                : 
                                722                 :                : Datum
 6704 tgl@sss.pgh.pa.us         723                 :UBC           0 : seg_size(PG_FUNCTION_ARGS)
                                724                 :                : {
 3438 andres@anarazel.de        725                 :              0 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                726                 :                : 
 1419 peter@eisentraut.org      727                 :              0 :     PG_RETURN_FLOAT4(fabsf(seg->upper - seg->lower));
                                728                 :                : }
                                729                 :                : 
                                730                 :                : 
                                731                 :                : /*****************************************************************************
                                732                 :                :  *                 Miscellaneous operators
                                733                 :                :  *****************************************************************************/
                                734                 :                : Datum
 3438 andres@anarazel.de        735                 :CBC        4433 : seg_cmp(PG_FUNCTION_ARGS)
                                736                 :                : {
                                737                 :           4433 :     SEG        *a = PG_GETARG_SEG_P(0);
                                738                 :           4433 :     SEG        *b = PG_GETARG_SEG_P(1);
                                739                 :                : 
                                740                 :                :     /*
                                741                 :                :      * First compare on lower boundary position
                                742                 :                :      */
 9289 bruce@momjian.us          743         [ +  + ]:           4433 :     if (a->lower < b->lower)
 3438 andres@anarazel.de        744                 :            904 :         PG_RETURN_INT32(-1);
 9289 bruce@momjian.us          745         [ +  + ]:           3529 :     if (a->lower > b->lower)
 3438 andres@anarazel.de        746                 :            800 :         PG_RETURN_INT32(1);
                                747                 :                : 
                                748                 :                :     /*
                                749                 :                :      * a->lower == b->lower, so consider type of boundary.
                                750                 :                :      *
                                751                 :                :      * A '-' lower bound is < any other kind (this could only be relevant if
                                752                 :                :      * -HUGE_VAL is used as a regular data value). A '<' lower bound is < any
                                753                 :                :      * other kind except '-'. A '>' lower bound is > any other kind.
                                754                 :                :      */
 9289 bruce@momjian.us          755         [ +  + ]:           2729 :     if (a->l_ext != b->l_ext)
                                756                 :                :     {
                                757         [ -  + ]:             66 :         if (a->l_ext == '-')
 3438 andres@anarazel.de        758                 :UBC           0 :             PG_RETURN_INT32(-1);
 9289 bruce@momjian.us          759         [ -  + ]:CBC          66 :         if (b->l_ext == '-')
 3438 andres@anarazel.de        760                 :UBC           0 :             PG_RETURN_INT32(1);
 9289 bruce@momjian.us          761         [ +  + ]:CBC          66 :         if (a->l_ext == '<')
 3438 andres@anarazel.de        762                 :             26 :             PG_RETURN_INT32(-1);
 9289 bruce@momjian.us          763         [ +  + ]:             40 :         if (b->l_ext == '<')
 3438 andres@anarazel.de        764                 :             21 :             PG_RETURN_INT32(1);
 9289 bruce@momjian.us          765         [ +  + ]:             19 :         if (a->l_ext == '>')
 3438 andres@anarazel.de        766                 :             13 :             PG_RETURN_INT32(1);
 9289 bruce@momjian.us          767         [ +  - ]:              6 :         if (b->l_ext == '>')
 3438 andres@anarazel.de        768                 :              6 :             PG_RETURN_INT32(-1);
                                769                 :                :     }
                                770                 :                : 
                                771                 :                :     /*
                                772                 :                :      * For other boundary types, consider # of significant digits first.
                                773                 :                :      */
 7621 bruce@momjian.us          774         [ +  + ]:           2663 :     if (a->l_sigd < b->l_sigd) /* (a) is blurred and is likely to include (b) */
 3438 andres@anarazel.de        775                 :             18 :         PG_RETURN_INT32(-1);
 9289 bruce@momjian.us          776         [ +  + ]:           2645 :     if (a->l_sigd > b->l_sigd) /* (a) is less blurred and is likely to be
                                777                 :                :                                  * included in (b) */
 3438 andres@anarazel.de        778                 :             18 :         PG_RETURN_INT32(1);
                                779                 :                : 
                                780                 :                :     /*
                                781                 :                :      * For same # of digits, an approximate boundary is more blurred than
                                782                 :                :      * exact.
                                783                 :                :      */
 9289 bruce@momjian.us          784         [ -  + ]:           2627 :     if (a->l_ext != b->l_ext)
                                785                 :                :     {
 9289 bruce@momjian.us          786         [ #  # ]:UBC           0 :         if (a->l_ext == '~') /* (a) is approximate, while (b) is exact */
 3438 andres@anarazel.de        787                 :              0 :             PG_RETURN_INT32(-1);
 9289 bruce@momjian.us          788         [ #  # ]:              0 :         if (b->l_ext == '~')
 3438 andres@anarazel.de        789                 :              0 :             PG_RETURN_INT32(1);
                                790                 :                :         /* can't get here unless data is corrupt */
 8435 tgl@sss.pgh.pa.us         791         [ #  # ]:              0 :         elog(ERROR, "bogus lower boundary types %d %d",
                                792                 :                :              (int) a->l_ext, (int) b->l_ext);
                                793                 :                :     }
                                794                 :                : 
                                795                 :                :     /* at this point, the lower boundaries are identical */
                                796                 :                : 
                                797                 :                :     /*
                                798                 :                :      * First compare on upper boundary position
                                799                 :                :      */
 9289 bruce@momjian.us          800         [ +  + ]:CBC        2627 :     if (a->upper < b->upper)
 3438 andres@anarazel.de        801                 :            163 :         PG_RETURN_INT32(-1);
 9289 bruce@momjian.us          802         [ +  + ]:           2464 :     if (a->upper > b->upper)
 3438 andres@anarazel.de        803                 :            126 :         PG_RETURN_INT32(1);
                                804                 :                : 
                                805                 :                :     /*
                                806                 :                :      * a->upper == b->upper, so consider type of boundary.
                                807                 :                :      *
                                808                 :                :      * A '-' upper bound is > any other kind (this could only be relevant if
                                809                 :                :      * HUGE_VAL is used as a regular data value). A '<' upper bound is < any
                                810                 :                :      * other kind. A '>' upper bound is > any other kind except '-'.
                                811                 :                :      */
 9289 bruce@momjian.us          812         [ +  + ]:           2338 :     if (a->u_ext != b->u_ext)
                                813                 :                :     {
                                814         [ -  + ]:             46 :         if (a->u_ext == '-')
 3438 andres@anarazel.de        815                 :UBC           0 :             PG_RETURN_INT32(1);
 9289 bruce@momjian.us          816         [ -  + ]:CBC          46 :         if (b->u_ext == '-')
 3438 andres@anarazel.de        817                 :UBC           0 :             PG_RETURN_INT32(-1);
 9289 bruce@momjian.us          818         [ +  + ]:CBC          46 :         if (a->u_ext == '<')
 3438 andres@anarazel.de        819                 :              3 :             PG_RETURN_INT32(-1);
 9289 bruce@momjian.us          820         [ +  + ]:             43 :         if (b->u_ext == '<')
 3438 andres@anarazel.de        821                 :              1 :             PG_RETURN_INT32(1);
 9289 bruce@momjian.us          822         [ +  + ]:             42 :         if (a->u_ext == '>')
 3438 andres@anarazel.de        823                 :             24 :             PG_RETURN_INT32(1);
 9289 bruce@momjian.us          824         [ +  - ]:             18 :         if (b->u_ext == '>')
 3438 andres@anarazel.de        825                 :             18 :             PG_RETURN_INT32(-1);
                                826                 :                :     }
                                827                 :                : 
                                828                 :                :     /*
                                829                 :                :      * For other boundary types, consider # of significant digits first. Note
                                830                 :                :      * result here is converse of the lower-boundary case.
                                831                 :                :      */
 7621 bruce@momjian.us          832         [ +  + ]:           2292 :     if (a->u_sigd < b->u_sigd) /* (a) is blurred and is likely to include (b) */
 3438 andres@anarazel.de        833                 :              6 :         PG_RETURN_INT32(1);
 9289 bruce@momjian.us          834         [ +  + ]:           2286 :     if (a->u_sigd > b->u_sigd) /* (a) is less blurred and is likely to be
                                835                 :                :                                  * included in (b) */
 3438 andres@anarazel.de        836                 :              8 :         PG_RETURN_INT32(-1);
                                837                 :                : 
                                838                 :                :     /*
                                839                 :                :      * For same # of digits, an approximate boundary is more blurred than
                                840                 :                :      * exact.  Again, result is converse of lower-boundary case.
                                841                 :                :      */
 9289 bruce@momjian.us          842         [ -  + ]:           2278 :     if (a->u_ext != b->u_ext)
                                843                 :                :     {
 9289 bruce@momjian.us          844         [ #  # ]:UBC           0 :         if (a->u_ext == '~') /* (a) is approximate, while (b) is exact */
 3438 andres@anarazel.de        845                 :              0 :             PG_RETURN_INT32(1);
 9289 bruce@momjian.us          846         [ #  # ]:              0 :         if (b->u_ext == '~')
 3438 andres@anarazel.de        847                 :              0 :             PG_RETURN_INT32(-1);
                                848                 :                :         /* can't get here unless data is corrupt */
 8435 tgl@sss.pgh.pa.us         849         [ #  # ]:              0 :         elog(ERROR, "bogus upper boundary types %d %d",
                                850                 :                :              (int) a->u_ext, (int) b->u_ext);
                                851                 :                :     }
                                852                 :                : 
 3438 andres@anarazel.de        853                 :CBC        2278 :     PG_RETURN_INT32(0);
                                854                 :                : }
                                855                 :                : 
                                856                 :                : Datum
 3438 andres@anarazel.de        857                 :UBC           0 : seg_lt(PG_FUNCTION_ARGS)
                                858                 :                : {
 2401 alvherre@alvh.no-ip.      859                 :              0 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                860                 :                :                                                         PG_GETARG_DATUM(0),
                                861                 :                :                                                         PG_GETARG_DATUM(1)));
                                862                 :                : 
 3438 andres@anarazel.de        863                 :              0 :     PG_RETURN_BOOL(cmp < 0);
                                864                 :                : }
                                865                 :                : 
                                866                 :                : Datum
                                867                 :              0 : seg_le(PG_FUNCTION_ARGS)
                                868                 :                : {
 2401 alvherre@alvh.no-ip.      869                 :              0 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                870                 :                :                                                         PG_GETARG_DATUM(0),
                                871                 :                :                                                         PG_GETARG_DATUM(1)));
                                872                 :                : 
 3438 andres@anarazel.de        873                 :              0 :     PG_RETURN_BOOL(cmp <= 0);
                                874                 :                : }
                                875                 :                : 
                                876                 :                : Datum
                                877                 :              0 : seg_gt(PG_FUNCTION_ARGS)
                                878                 :                : {
 2401 alvherre@alvh.no-ip.      879                 :              0 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                880                 :                :                                                         PG_GETARG_DATUM(0),
                                881                 :                :                                                         PG_GETARG_DATUM(1)));
                                882                 :                : 
 3438 andres@anarazel.de        883                 :              0 :     PG_RETURN_BOOL(cmp > 0);
                                884                 :                : }
                                885                 :                : 
                                886                 :                : Datum
                                887                 :              0 : seg_ge(PG_FUNCTION_ARGS)
                                888                 :                : {
 2401 alvherre@alvh.no-ip.      889                 :              0 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                890                 :                :                                                         PG_GETARG_DATUM(0),
                                891                 :                :                                                         PG_GETARG_DATUM(1)));
                                892                 :                : 
 3438 andres@anarazel.de        893                 :              0 :     PG_RETURN_BOOL(cmp >= 0);
                                894                 :                : }
                                895                 :                : 
                                896                 :                : 
                                897                 :                : Datum
 3438 andres@anarazel.de        898                 :CBC           2 : seg_different(PG_FUNCTION_ARGS)
                                899                 :                : {
 2401 alvherre@alvh.no-ip.      900                 :              2 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                901                 :                :                                                         PG_GETARG_DATUM(0),
                                902                 :                :                                                         PG_GETARG_DATUM(1)));
                                903                 :                : 
 3438 andres@anarazel.de        904                 :              2 :     PG_RETURN_BOOL(cmp != 0);
                                905                 :                : }
                                906                 :                : 
                                907                 :                : 
                                908                 :                : 
                                909                 :                : /*****************************************************************************
                                910                 :                :  *                 Auxiliary functions
                                911                 :                :  *****************************************************************************/
                                912                 :                : 
                                913                 :                : /*
                                914                 :                :  * The purpose of this routine is to print the given floating point
                                915                 :                :  * value with exactly n significant digits.  Its behaviour
                                916                 :                :  * is similar to %.ng except it prints 8.00 where %.ng would
                                917                 :                :  * print 8.  Returns the length of the string written at "result".
                                918                 :                :  *
                                919                 :                :  * Caller must provide a sufficiently large result buffer; 16 bytes
                                920                 :                :  * should be enough for all known float implementations.
                                921                 :                :  */
                                922                 :                : static int
 9289 bruce@momjian.us          923                 :            339 : restore(char *result, float val, int n)
                                924                 :                : {
                                925                 :            339 :     char        buf[25] = {
                                926                 :                :         '0', '0', '0', '0', '0',
                                927                 :                :         '0', '0', '0', '0', '0',
                                928                 :                :         '0', '0', '0', '0', '0',
                                929                 :                :         '0', '0', '0', '0', '0',
                                930                 :                :         '0', '0', '0', '0', '\0'
                                931                 :                :     };
                                932                 :                :     char       *p;
                                933                 :                :     int         exp;
                                934                 :                :     int         i,
                                935                 :                :                 dp,
                                936                 :                :                 sign;
                                937                 :                : 
                                938                 :                :     /*
                                939                 :                :      * Put a cap on the number of significant digits to avoid garbage in the
                                940                 :                :      * output and ensure we don't overrun the result buffer.  (n should not be
                                941                 :                :      * negative, but check to protect ourselves against corrupted data.)
                                942                 :                :      */
 1345 tgl@sss.pgh.pa.us         943         [ -  + ]:            339 :     if (n <= 0)
 1345 tgl@sss.pgh.pa.us         944                 :UBC           0 :         n = FLT_DIG;
                                945                 :                :     else
 1345 tgl@sss.pgh.pa.us         946                 :CBC         339 :         n = Min(n, FLT_DIG);
                                947                 :                : 
                                948                 :                :     /* remember the sign */
 9289 bruce@momjian.us          949                 :            339 :     sign = (val < 0 ? 1 : 0);
                                950                 :                : 
                                951                 :                :     /* print, in %e style to start with */
 3798 tgl@sss.pgh.pa.us         952                 :            339 :     sprintf(result, "%.*e", n - 1, val);
                                953                 :                : 
                                954                 :                :     /* find the exponent */
                                955                 :            339 :     p = strchr(result, 'e');
                                956                 :                : 
                                957                 :                :     /* punt if we have 'inf' or similar */
                                958         [ -  + ]:            339 :     if (p == NULL)
 3798 tgl@sss.pgh.pa.us         959                 :UBC           0 :         return strlen(result);
                                960                 :                : 
 3798 tgl@sss.pgh.pa.us         961                 :CBC         339 :     exp = atoi(p + 1);
 9289 bruce@momjian.us          962         [ +  + ]:            339 :     if (exp == 0)
                                963                 :                :     {
                                964                 :                :         /* just truncate off the 'e+00' */
 3798 tgl@sss.pgh.pa.us         965                 :            183 :         *p = '\0';
                                966                 :                :     }
                                967                 :                :     else
                                968                 :                :     {
 1420 peter@eisentraut.org      969   [ +  +  +  + ]:            156 :         if (abs(exp) <= 4)
                                970                 :                :         {
                                971                 :                :             /*
                                972                 :                :              * remove the decimal point from the mantissa and write the digits
                                973                 :                :              * to the buf array
                                974                 :                :              */
 9289 bruce@momjian.us          975         [ +  + ]:            637 :             for (p = result + sign, i = 10, dp = 0; *p != 'e'; p++, i++)
                                976                 :                :             {
                                977                 :            499 :                 buf[i] = *p;
                                978         [ +  + ]:            499 :                 if (*p == '.')
                                979                 :                :                 {
                                980                 :            130 :                     dp = i--;   /* skip the decimal point */
                                981                 :                :                 }
                                982                 :                :             }
                                983         [ +  + ]:            138 :             if (dp == 0)
                                984                 :              8 :                 dp = i--;       /* no decimal point was found in the above
                                985                 :                :                                  * for() loop */
                                986                 :                : 
                                987         [ +  + ]:            138 :             if (exp > 0)
                                988                 :                :             {
                                989         [ +  + ]:            133 :                 if (dp - 10 + exp >= n)
                                990                 :                :                 {
                                991                 :                :                     /*
                                992                 :                :                      * the decimal point is behind the last significant digit;
                                993                 :                :                      * the digits in between must be converted to the exponent
                                994                 :                :                      * and the decimal point placed after the first digit
                                995                 :                :                      */
                                996                 :             43 :                     exp = dp - 10 + exp - n;
                                997                 :             43 :                     buf[10 + n] = '\0';
                                998                 :                : 
                                999                 :                :                     /* insert the decimal point */
                               1000         [ +  + ]:             43 :                     if (n > 1)
                               1001                 :                :                     {
                               1002                 :             39 :                         dp = 11;
                               1003         [ +  + ]:            507 :                         for (i = 23; i > dp; i--)
                               1004                 :            468 :                             buf[i] = buf[i - 1];
                               1005                 :             39 :                         buf[dp] = '.';
                               1006                 :                :                     }
                               1007                 :                : 
                               1008                 :                :                     /*
                               1009                 :                :                      * adjust the exponent by the number of digits after the
                               1010                 :                :                      * decimal point
                               1011                 :                :                      */
                               1012         [ +  + ]:             43 :                     if (n > 1)
                               1013                 :             39 :                         sprintf(&buf[11 + n], "e%d", exp + n - 1);
                               1014                 :                :                     else
                               1015                 :              4 :                         sprintf(&buf[11], "e%d", exp + n - 1);
                               1016                 :                : 
                               1017         [ -  + ]:             43 :                     if (sign)
                               1018                 :                :                     {
 9289 bruce@momjian.us         1019                 :UBC           0 :                         buf[9] = '-';
                               1020                 :              0 :                         strcpy(result, &buf[9]);
                               1021                 :                :                     }
                               1022                 :                :                     else
 9289 bruce@momjian.us         1023                 :CBC          43 :                         strcpy(result, &buf[10]);
                               1024                 :                :                 }
                               1025                 :                :                 else
                               1026                 :                :                 {               /* insert the decimal point */
                               1027                 :             90 :                     dp += exp;
                               1028         [ +  + ]:           1080 :                     for (i = 23; i > dp; i--)
                               1029                 :            990 :                         buf[i] = buf[i - 1];
                               1030                 :             90 :                     buf[11 + n] = '\0';
                               1031                 :             90 :                     buf[dp] = '.';
                               1032         [ -  + ]:             90 :                     if (sign)
                               1033                 :                :                     {
 9289 bruce@momjian.us         1034                 :UBC           0 :                         buf[9] = '-';
                               1035                 :              0 :                         strcpy(result, &buf[9]);
                               1036                 :                :                     }
                               1037                 :                :                     else
 9289 bruce@momjian.us         1038                 :CBC          90 :                         strcpy(result, &buf[10]);
                               1039                 :                :                 }
                               1040                 :                :             }
                               1041                 :                :             else
                               1042                 :                :             {                   /* exp <= 0 */
                               1043                 :              5 :                 dp += exp - 1;
                               1044                 :              5 :                 buf[10 + n] = '\0';
                               1045                 :              5 :                 buf[dp] = '.';
                               1046         [ -  + ]:              5 :                 if (sign)
                               1047                 :                :                 {
 9289 bruce@momjian.us         1048                 :UBC           0 :                     buf[dp - 2] = '-';
                               1049                 :              0 :                     strcpy(result, &buf[dp - 2]);
                               1050                 :                :                 }
                               1051                 :                :                 else
 9289 bruce@momjian.us         1052                 :CBC           5 :                     strcpy(result, &buf[dp - 1]);
                               1053                 :                :             }
                               1054                 :                :         }
                               1055                 :                : 
                               1056                 :                :         /* do nothing for abs(exp) > 4; %e must be OK */
                               1057                 :                :         /* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
                               1058                 :                : 
                               1059                 :                :         /* ... this is not done yet. */
                               1060                 :                :     }
 3297 peter_e@gmx.net          1061                 :            339 :     return strlen(result);
                               1062                 :                : }
                               1063                 :                : 
                               1064                 :                : 
                               1065                 :                : /*
                               1066                 :                :  * Miscellany
                               1067                 :                :  */
                               1068                 :                : 
                               1069                 :                : /*
                               1070                 :                :  * find out the number of significant digits in a string representing
                               1071                 :                :  * a floating point number
                               1072                 :                :  */
                               1073                 :                : int
 3222                          1074                 :           5205 : significant_digits(const char *s)
                               1075                 :                : {
                               1076                 :           5205 :     const char *p = s;
                               1077                 :                :     int         n,
                               1078                 :                :                 c,
                               1079                 :                :                 zeroes;
                               1080                 :                : 
 9289 bruce@momjian.us         1081                 :           5205 :     zeroes = 1;
                               1082                 :                :     /* skip leading zeroes and sign */
                               1083   [ +  +  +  -  :           5344 :     for (c = *p; (c == '0' || c == '+' || c == '-') && c != 0; c = *(++p));
                                        +  +  +  - ]
                               1084                 :                : 
                               1085                 :                :     /* skip decimal point and following zeroes */
                               1086   [ +  +  +  +  :           5226 :     for (c = *p; (c == '0' || c == '.') && c != 0; c = *(++p))
                                              +  - ]
                               1087                 :                :     {
                               1088         [ +  + ]:             21 :         if (c != '.')
                               1089                 :             11 :             zeroes++;
                               1090                 :                :     }
                               1091                 :                : 
                               1092                 :                :     /* count significant digits (n) */
                               1093         [ +  + ]:          20409 :     for (c = *p, n = 0; c != 0; c = *(++p))
                               1094                 :                :     {
                               1095   [ +  +  +  +  :          15231 :         if (!((c >= '0' && c <= '9') || (c == '.')))
                                              +  + ]
                               1096                 :             27 :             break;
                               1097         [ +  + ]:          15204 :         if (c != '.')
                               1098                 :          10645 :             n++;
                               1099                 :                :     }
                               1100                 :                : 
                               1101         [ +  + ]:           5205 :     if (!n)
 3297 peter_e@gmx.net          1102                 :             98 :         return zeroes;
                               1103                 :                : 
                               1104                 :           5107 :     return n;
                               1105                 :                : }
        

Generated by: LCOV version 2.0-1