LCOV - differential code coverage report
Current view: top level - contrib/seg - seg.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 74.9 % 431 323 2 106 5 318 2 5
Current Date: 2026-03-14 14:10:32 -0400 Functions: 87.9 % 66 58 1 7 6 52
Baseline: lcov-20260315-024220-baseline Branches: 74.6 % 201 150 51 2 148 2
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 75.0 % 8 6 2 5 1
(360..) days: 74.9 % 423 317 106 317
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 87.7 % 65 57 1 7 6 51
Branch coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 74.4 % 199 148 51 148

 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                 :                : 
  354 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                 :                : */
 6540 alvherre@alvh.no-ip.       49                 :              4 : PG_FUNCTION_INFO_V1(seg_in);
                                 50                 :              3 : PG_FUNCTION_INFO_V1(seg_out);
 6539 tgl@sss.pgh.pa.us          51                 :              2 : PG_FUNCTION_INFO_V1(seg_size);
 6540 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                 :                : */
 3273 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
 6540 alvherre@alvh.no-ip.      107                 :           2825 : seg_in(PG_FUNCTION_ARGS)
                                108                 :                : {
                                109                 :           2825 :     char       *str = PG_GETARG_CSTRING(0);
  100 michael@paquier.xyz       110                 :GNC        2825 :     SEG        *result = palloc_object(SEG);
                                111                 :                :     yyscan_t    scanner;
                                112                 :                : 
  452 peter@eisentraut.org      113                 :CBC        2825 :     seg_scanner_init(str, &scanner);
                                114                 :                : 
                                115         [ +  + ]:           2825 :     if (seg_yyparse(result, fcinfo->context, scanner) != 0)
                                116                 :              8 :         seg_yyerror(result, fcinfo->context, scanner, "bogus input");
                                117                 :                : 
                                118                 :           2816 :     seg_scanner_finish(scanner);
                                119                 :                : 
 6540 alvherre@alvh.no-ip.      120                 :           2816 :     PG_RETURN_POINTER(result);
                                121                 :                : }
                                122                 :                : 
                                123                 :                : Datum
                                124                 :            209 : seg_out(PG_FUNCTION_ARGS)
                                125                 :                : {
 3273 andres@anarazel.de        126                 :            209 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                127                 :                :     char       *result;
                                128                 :                :     char       *p;
                                129                 :                : 
 9124 bruce@momjian.us          130                 :            209 :     p = result = (char *) palloc(40);
                                131                 :                : 
                                132   [ +  +  +  +  :            209 :     if (seg->l_ext == '>' || seg->l_ext == '<' || seg->l_ext == '~')
                                              +  + ]
                                133                 :             22 :         p += sprintf(p, "%c", seg->l_ext);
                                134                 :                : 
                                135   [ +  +  +  - ]:            209 :     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         [ +  + ]:            162 :         if (seg->l_ext != '-')
                                145                 :                :         {
                                146                 :                :             /* print the lower boundary if exists */
                                147                 :            155 :             p += restore(p, seg->lower, seg->l_sigd);
                                148                 :            155 :             p += sprintf(p, " ");
                                149                 :                :         }
                                150                 :            162 :         p += sprintf(p, "..");
                                151         [ +  + ]:            162 :         if (seg->u_ext != '-')
                                152                 :                :         {
                                153                 :                :             /* print the upper boundary if exists */
                                154                 :            123 :             p += sprintf(p, " ");
                                155   [ +  +  +  +  :            123 :             if (seg->u_ext == '>' || seg->u_ext == '<' || seg->l_ext == '~')
                                              -  + ]
                                156                 :             32 :                 p += sprintf(p, "%c", seg->u_ext);
                                157                 :            123 :             p += restore(p, seg->upper, seg->u_sigd);
                                158                 :                :         }
                                159                 :                :     }
                                160                 :                : 
 6540 alvherre@alvh.no-ip.      161                 :            209 :     PG_RETURN_CSTRING(result);
                                162                 :                : }
                                163                 :                : 
                                164                 :                : Datum
                                165                 :            143 : seg_center(PG_FUNCTION_ARGS)
                                166                 :                : {
 3273 andres@anarazel.de        167                 :            143 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                168                 :                : 
 6540 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                 :                : {
 3273 andres@anarazel.de        175                 :            143 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                176                 :                : 
 6540 alvherre@alvh.no-ip.      177                 :            143 :     PG_RETURN_FLOAT4(seg->lower);
                                178                 :                : }
                                179                 :                : 
                                180                 :                : Datum
                                181                 :            143 : seg_upper(PG_FUNCTION_ARGS)
                                182                 :                : {
 3273 andres@anarazel.de        183                 :            143 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                184                 :                : 
 6540 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
 3273 andres@anarazel.de        200                 :           5760 : gseg_consistent(PG_FUNCTION_ARGS)
                                201                 :                : {
                                202                 :           5760 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
                                203                 :           5760 :     Datum       query = PG_GETARG_DATUM(1);
                                204                 :           5760 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
                                205                 :                : #ifdef NOT_USED
                                206                 :                :     Oid         subtype = PG_GETARG_OID(3);
                                207                 :                : #endif
                                208                 :           5760 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
                                209                 :                : 
                                210                 :                :     /* All cases served by this function are exact */
 6544 tgl@sss.pgh.pa.us         211                 :           5760 :     *recheck = false;
                                212                 :                : 
                                213                 :                :     /*
                                214                 :                :      * if entry is not leaf, use gseg_internal_consistent, else use
                                215                 :                :      * gseg_leaf_consistent
                                216                 :                :      */
 9124 bruce@momjian.us          217         [ +  + ]:           5760 :     if (GIST_LEAF(entry))
 3273 andres@anarazel.de        218                 :           5688 :         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                 :                : 
 8020 teodor@sigaev.ru          241                 :           2316 :     numranges = entryvec->n;
 3273 andres@anarazel.de        242                 :           2316 :     tmp = entryvec->vector[0].key;
 9124 bruce@momjian.us          243                 :           2316 :     *sizep = sizeof(SEG);
                                244                 :                : 
                                245         [ +  + ]:           4632 :     for (i = 1; i < numranges; i++)
                                246                 :                :     {
 3273 andres@anarazel.de        247                 :           2316 :         out = gseg_binary_union(tmp, entryvec->vector[i].key, sizep);
 9124 bruce@momjian.us          248                 :           2316 :         tmp = out;
                                249                 :                :     }
                                250                 :                : 
 3273 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
 3273 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
 3273 andres@anarazel.de        275                 :CBC        4697 : gseg_penalty(PG_FUNCTION_ARGS)
                                276                 :                : {
                                277                 :           4697 :     GISTENTRY  *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
                                278                 :           4697 :     GISTENTRY  *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
                                279                 :           4697 :     float      *result = (float *) PG_GETARG_POINTER(2);
                                280                 :                :     SEG        *ud;
                                281                 :                :     float       tmp1,
                                282                 :                :                 tmp2;
                                283                 :                : 
                                284                 :           4697 :     ud = DatumGetSegP(DirectFunctionCall2(seg_union,
                                285                 :                :                                           origentry->key,
                                286                 :                :                                           newentry->key));
 9054 tgl@sss.pgh.pa.us         287                 :           4697 :     rt_seg_size(ud, &tmp1);
 3273 andres@anarazel.de        288                 :           4697 :     rt_seg_size(DatumGetSegP(origentry->key), &tmp2);
 9124 bruce@momjian.us          289                 :           4697 :     *result = tmp1 - tmp2;
                                290                 :                : 
                                291                 :                : #ifdef GIST_DEBUG
                                292                 :                :     fprintf(stderr, "penalty\n");
                                293                 :                :     fprintf(stderr, "\t%g\n", *result);
                                294                 :                : #endif
                                295                 :                : 
 3273 andres@anarazel.de        296                 :           4697 :     PG_RETURN_POINTER(result);
                                297                 :                : }
                                298                 :                : 
                                299                 :                : /*
                                300                 :                :  * Compare function for gseg_picksplit_item: sort by center.
                                301                 :                :  */
                                302                 :                : static int
 5569 tgl@sss.pgh.pa.us         303                 :          29071 : gseg_picksplit_item_cmp(const void *a, const void *b)
                                304                 :                : {
                                305                 :          29071 :     const gseg_picksplit_item *i1 = (const gseg_picksplit_item *) a;
                                306                 :          29071 :     const gseg_picksplit_item *i2 = (const gseg_picksplit_item *) b;
                                307                 :                : 
                                308         [ +  + ]:          29071 :     if (i1->center < i2->center)
                                309                 :          12029 :         return -1;
                                310         [ +  + ]:          17042 :     else if (i1->center == i2->center)
                                311                 :           5118 :         return 0;
                                312                 :                :     else
                                313                 :          11924 :         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
 3273 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 */
 5569 tgl@sss.pgh.pa.us         343                 :             17 :     maxoff = entryvec->n - 1;
                                344                 :                : 
                                345                 :                :     /*
                                346                 :                :      * Prepare the auxiliary array and sort it.
                                347                 :                :      */
                                348                 :                :     sort_items = (gseg_picksplit_item *)
                                349                 :             17 :         palloc(maxoff * sizeof(gseg_picksplit_item));
                                350         [ +  + ]:           4471 :     for (i = 1; i <= maxoff; i++)
                                351                 :                :     {
 3273 andres@anarazel.de        352                 :           4454 :         seg = DatumGetSegP(entryvec->vector[i].key);
                                353                 :                :         /* center calculation is done this way to avoid possible overflow */
 5453 bruce@momjian.us          354                 :           4454 :         sort_items[i - 1].center = seg->lower * 0.5f + seg->upper * 0.5f;
 5569 tgl@sss.pgh.pa.us         355                 :           4454 :         sort_items[i - 1].index = i;
                                356                 :           4454 :         sort_items[i - 1].data = seg;
                                357                 :                :     }
                                358                 :             17 :     qsort(sort_items, maxoff, sizeof(gseg_picksplit_item),
                                359                 :                :           gseg_picksplit_item_cmp);
                                360                 :                : 
                                361                 :                :     /* sort items below "firstright" will go into the left side */
                                362                 :             17 :     firstright = maxoff / 2;
                                363                 :                : 
                                364                 :             17 :     v->spl_left = (OffsetNumber *) palloc(maxoff * sizeof(OffsetNumber));
                                365                 :             17 :     v->spl_right = (OffsetNumber *) palloc(maxoff * sizeof(OffsetNumber));
 9124 bruce@momjian.us          366                 :             17 :     left = v->spl_left;
                                367                 :             17 :     v->spl_nleft = 0;
                                368                 :             17 :     right = v->spl_right;
                                369                 :             17 :     v->spl_nright = 0;
                                370                 :                : 
                                371                 :                :     /*
                                372                 :                :      * Emit segments to the left output page, and compute its bounding box.
                                373                 :                :      */
  100 michael@paquier.xyz       374                 :GNC          17 :     seg_l = palloc_object(SEG);
 3273 andres@anarazel.de        375                 :CBC          17 :     memcpy(seg_l, sort_items[0].data, sizeof(SEG));
 5569 tgl@sss.pgh.pa.us         376                 :             17 :     *left++ = sort_items[0].index;
                                377                 :             17 :     v->spl_nleft++;
                                378         [ +  + ]:           2227 :     for (i = 1; i < firstright; i++)
                                379                 :                :     {
 3273 andres@anarazel.de        380                 :           2210 :         Datum       sortitem = PointerGetDatum(sort_items[i].data);
                                381                 :                : 
                                382                 :           2210 :         seg_l = DatumGetSegP(DirectFunctionCall2(seg_union,
                                383                 :                :                                                  PointerGetDatum(seg_l),
                                384                 :                :                                                  sortitem));
 5569 tgl@sss.pgh.pa.us         385                 :           2210 :         *left++ = sort_items[i].index;
                                386                 :           2210 :         v->spl_nleft++;
                                387                 :                :     }
                                388                 :                : 
                                389                 :                :     /*
                                390                 :                :      * Likewise for the right page.
                                391                 :                :      */
  100 michael@paquier.xyz       392                 :GNC          17 :     seg_r = palloc_object(SEG);
 3273 andres@anarazel.de        393                 :CBC          17 :     memcpy(seg_r, sort_items[firstright].data, sizeof(SEG));
 5569 tgl@sss.pgh.pa.us         394                 :             17 :     *right++ = sort_items[firstright].index;
                                395                 :             17 :     v->spl_nright++;
                                396         [ +  + ]:           2227 :     for (i = firstright + 1; i < maxoff; i++)
                                397                 :                :     {
 3273 andres@anarazel.de        398                 :           2210 :         Datum       sortitem = PointerGetDatum(sort_items[i].data);
                                399                 :                : 
                                400                 :           2210 :         seg_r = DatumGetSegP(DirectFunctionCall2(seg_union,
                                401                 :                :                                                  PointerGetDatum(seg_r),
                                402                 :                :                                                  sortitem));
 5569 tgl@sss.pgh.pa.us         403                 :           2210 :         *right++ = sort_items[i].index;
                                404                 :           2210 :         v->spl_nright++;
                                405                 :                :     }
                                406                 :                : 
 3273 andres@anarazel.de        407                 :             17 :     v->spl_ldatum = PointerGetDatum(seg_l);
                                408                 :             17 :     v->spl_rdatum = PointerGetDatum(seg_r);
                                409                 :                : 
                                410                 :             17 :     PG_RETURN_POINTER(v);
                                411                 :                : }
                                412                 :                : 
                                413                 :                : /*
                                414                 :                : ** Equality methods
                                415                 :                : */
                                416                 :                : Datum
                                417                 :           2315 : gseg_same(PG_FUNCTION_ARGS)
                                418                 :                : {
                                419                 :           2315 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
                                420                 :                : 
  219 peter@eisentraut.org      421         [ +  + ]:GNC        2315 :     if (DatumGetBool(DirectFunctionCall2(seg_same, PG_GETARG_DATUM(0), PG_GETARG_DATUM(1))))
 3133 peter_e@gmx.net           422                 :CBC        2294 :         *result = true;
                                423                 :                :     else
                                424                 :             21 :         *result = false;
                                425                 :                : 
                                426                 :                : #ifdef GIST_DEBUG
                                427                 :                :     fprintf(stderr, "same: %s\n", (*result ? "TRUE" : "FALSE"));
                                428                 :                : #endif
                                429                 :                : 
 3273 andres@anarazel.de        430                 :           2315 :     PG_RETURN_POINTER(result);
                                431                 :                : }
                                432                 :                : 
                                433                 :                : /*
                                434                 :                : ** SUPPORT ROUTINES
                                435                 :                : */
                                436                 :                : static Datum
                                437                 :           5688 : gseg_leaf_consistent(Datum key, Datum query, StrategyNumber strategy)
                                438                 :                : {
                                439                 :                :     Datum       retval;
                                440                 :                : 
                                441                 :                : #ifdef GIST_QUERY_DEBUG
                                442                 :                :     fprintf(stderr, "leaf_consistent, %d\n", strategy);
                                443                 :                : #endif
                                444                 :                : 
 9124 bruce@momjian.us          445   [ -  -  -  -  :           5688 :     switch (strategy)
                                        -  -  +  -  
                                                 - ]
                                446                 :                :     {
 9124 bruce@momjian.us          447                 :UBC           0 :         case RTLeftStrategyNumber:
 3273 andres@anarazel.de        448                 :              0 :             retval = DirectFunctionCall2(seg_left, key, query);
 9124 bruce@momjian.us          449                 :              0 :             break;
                                450                 :              0 :         case RTOverLeftStrategyNumber:
 3273 andres@anarazel.de        451                 :              0 :             retval = DirectFunctionCall2(seg_over_left, key, query);
 9124 bruce@momjian.us          452                 :              0 :             break;
                                453                 :              0 :         case RTOverlapStrategyNumber:
 3273 andres@anarazel.de        454                 :              0 :             retval = DirectFunctionCall2(seg_overlap, key, query);
 9124 bruce@momjian.us          455                 :              0 :             break;
                                456                 :              0 :         case RTOverRightStrategyNumber:
 3273 andres@anarazel.de        457                 :              0 :             retval = DirectFunctionCall2(seg_over_right, key, query);
 9124 bruce@momjian.us          458                 :              0 :             break;
                                459                 :              0 :         case RTRightStrategyNumber:
 3273 andres@anarazel.de        460                 :              0 :             retval = DirectFunctionCall2(seg_right, key, query);
 9124 bruce@momjian.us          461                 :              0 :             break;
                                462                 :              0 :         case RTSameStrategyNumber:
 3273 andres@anarazel.de        463                 :              0 :             retval = DirectFunctionCall2(seg_same, key, query);
 9124 bruce@momjian.us          464                 :              0 :             break;
 9124 bruce@momjian.us          465                 :CBC        5688 :         case RTContainsStrategyNumber:
                                466                 :                :         case RTOldContainsStrategyNumber:
 3273 andres@anarazel.de        467                 :           5688 :             retval = DirectFunctionCall2(seg_contains, key, query);
 9124 bruce@momjian.us          468                 :           5688 :             break;
 9124 bruce@momjian.us          469                 :UBC           0 :         case RTContainedByStrategyNumber:
                                470                 :                :         case RTOldContainedByStrategyNumber:
 3273 andres@anarazel.de        471                 :              0 :             retval = DirectFunctionCall2(seg_contained, key, query);
 9124 bruce@momjian.us          472                 :              0 :             break;
                                473                 :              0 :         default:
  219 peter@eisentraut.org      474                 :UNC           0 :             retval = BoolGetDatum(false);
                                475                 :                :     }
                                476                 :                : 
 3273 andres@anarazel.de        477                 :CBC        5688 :     PG_RETURN_DATUM(retval);
                                478                 :                : }
                                479                 :                : 
                                480                 :                : static Datum
                                481                 :             72 : gseg_internal_consistent(Datum key, Datum query, StrategyNumber strategy)
                                482                 :                : {
                                483                 :                :     bool        retval;
                                484                 :                : 
                                485                 :                : #ifdef GIST_QUERY_DEBUG
                                486                 :                :     fprintf(stderr, "internal_consistent, %d\n", strategy);
                                487                 :                : #endif
                                488                 :                : 
 9124 bruce@momjian.us          489   [ -  -  -  -  :             72 :     switch (strategy)
                                        -  +  -  - ]
                                490                 :                :     {
 9124 bruce@momjian.us          491                 :UBC           0 :         case RTLeftStrategyNumber:
 3273 andres@anarazel.de        492                 :              0 :             retval =
                                493                 :              0 :                 !DatumGetBool(DirectFunctionCall2(seg_over_right, key, query));
 7566 tgl@sss.pgh.pa.us         494                 :              0 :             break;
 9124 bruce@momjian.us          495                 :              0 :         case RTOverLeftStrategyNumber:
 3273 andres@anarazel.de        496                 :              0 :             retval =
                                497                 :              0 :                 !DatumGetBool(DirectFunctionCall2(seg_right, key, query));
 9124 bruce@momjian.us          498                 :              0 :             break;
                                499                 :              0 :         case RTOverlapStrategyNumber:
                                500                 :                :             retval =
 3273 andres@anarazel.de        501                 :              0 :                 DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));
 9124 bruce@momjian.us          502                 :              0 :             break;
                                503                 :              0 :         case RTOverRightStrategyNumber:
 3273 andres@anarazel.de        504                 :              0 :             retval =
                                505                 :              0 :                 !DatumGetBool(DirectFunctionCall2(seg_left, key, query));
 7566 tgl@sss.pgh.pa.us         506                 :              0 :             break;
 9124 bruce@momjian.us          507                 :              0 :         case RTRightStrategyNumber:
 3273 andres@anarazel.de        508                 :              0 :             retval =
                                509                 :              0 :                 !DatumGetBool(DirectFunctionCall2(seg_over_left, key, query));
 9124 bruce@momjian.us          510                 :              0 :             break;
 9124 bruce@momjian.us          511                 :CBC          72 :         case RTSameStrategyNumber:
                                512                 :                :         case RTContainsStrategyNumber:
                                513                 :                :         case RTOldContainsStrategyNumber:
                                514                 :                :             retval =
 3273 andres@anarazel.de        515                 :             72 :                 DatumGetBool(DirectFunctionCall2(seg_contains, key, query));
 9124 bruce@momjian.us          516                 :             72 :             break;
 9124 bruce@momjian.us          517                 :UBC           0 :         case RTContainedByStrategyNumber:
                                518                 :                :         case RTOldContainedByStrategyNumber:
                                519                 :                :             retval =
 3273 andres@anarazel.de        520                 :              0 :                 DatumGetBool(DirectFunctionCall2(seg_overlap, key, query));
 9124 bruce@momjian.us          521                 :              0 :             break;
                                522                 :              0 :         default:
 3133 peter_e@gmx.net           523                 :              0 :             retval = false;
                                524                 :                :     }
                                525                 :                : 
 3273 andres@anarazel.de        526                 :CBC          72 :     PG_RETURN_BOOL(retval);
                                527                 :                : }
                                528                 :                : 
                                529                 :                : static Datum
                                530                 :           2316 : gseg_binary_union(Datum r1, Datum r2, int *sizep)
                                531                 :                : {
                                532                 :                :     Datum       retval;
                                533                 :                : 
                                534                 :           2316 :     retval = DirectFunctionCall2(seg_union, r1, r2);
 9124 bruce@momjian.us          535                 :           2316 :     *sizep = sizeof(SEG);
                                536                 :                : 
 3132 peter_e@gmx.net           537                 :           2316 :     return retval;
                                538                 :                : }
                                539                 :                : 
                                540                 :                : 
                                541                 :                : Datum
 3273 andres@anarazel.de        542                 :           5775 : seg_contains(PG_FUNCTION_ARGS)
                                543                 :                : {
                                544                 :           5775 :     SEG        *a = PG_GETARG_SEG_P(0);
                                545                 :           5775 :     SEG        *b = PG_GETARG_SEG_P(1);
                                546                 :                : 
                                547   [ +  +  +  + ]:           5775 :     PG_RETURN_BOOL((a->lower <= b->lower) && (a->upper >= b->upper));
                                548                 :                : }
                                549                 :                : 
                                550                 :                : Datum
                                551                 :             14 : seg_contained(PG_FUNCTION_ARGS)
                                552                 :                : {
                                553                 :             14 :     Datum       a = PG_GETARG_DATUM(0);
                                554                 :             14 :     Datum       b = PG_GETARG_DATUM(1);
                                555                 :                : 
                                556                 :             14 :     PG_RETURN_DATUM(DirectFunctionCall2(seg_contains, b, a));
                                557                 :                : }
                                558                 :                : 
                                559                 :                : /*****************************************************************************
                                560                 :                :  * Operator class for R-tree indexing
                                561                 :                :  *****************************************************************************/
                                562                 :                : 
                                563                 :                : Datum
                                564                 :           2459 : seg_same(PG_FUNCTION_ARGS)
                                565                 :                : {
 2236 alvherre@alvh.no-ip.      566                 :           2459 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                567                 :                :                                                         PG_GETARG_DATUM(0),
                                568                 :                :                                                         PG_GETARG_DATUM(1)));
                                569                 :                : 
 3273 andres@anarazel.de        570                 :           2459 :     PG_RETURN_BOOL(cmp == 0);
                                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                 :                : /*  seg_over_left -- is the right edge of (a) located at or left of the right edge of (b)?
                                586                 :                :  */
                                587                 :                : Datum
                                588                 :             11 : seg_over_left(PG_FUNCTION_ARGS)
                                589                 :                : {
                                590                 :             11 :     SEG        *a = PG_GETARG_SEG_P(0);
                                591                 :             11 :     SEG        *b = PG_GETARG_SEG_P(1);
                                592                 :                : 
                                593                 :             11 :     PG_RETURN_BOOL(a->upper <= b->upper);
                                594                 :                : }
                                595                 :                : 
                                596                 :                : /*  seg_left -- is (a) entirely on the left of (b)?
                                597                 :                :  */
                                598                 :                : Datum
                                599                 :             11 : seg_left(PG_FUNCTION_ARGS)
                                600                 :                : {
                                601                 :             11 :     SEG        *a = PG_GETARG_SEG_P(0);
                                602                 :             11 :     SEG        *b = PG_GETARG_SEG_P(1);
                                603                 :                : 
                                604                 :             11 :     PG_RETURN_BOOL(a->upper < b->lower);
                                605                 :                : }
                                606                 :                : 
                                607                 :                : /*  seg_right -- is (a) entirely on the right of (b)?
                                608                 :                :  */
                                609                 :                : Datum
                                610                 :             11 : seg_right(PG_FUNCTION_ARGS)
                                611                 :                : {
                                612                 :             11 :     SEG        *a = PG_GETARG_SEG_P(0);
                                613                 :             11 :     SEG        *b = PG_GETARG_SEG_P(1);
                                614                 :                : 
                                615                 :             11 :     PG_RETURN_BOOL(a->lower > b->upper);
                                616                 :                : }
                                617                 :                : 
                                618                 :                : /*  seg_over_right -- is the left edge of (a) located at or right of the left edge of (b)?
                                619                 :                :  */
                                620                 :                : Datum
                                621                 :             11 : seg_over_right(PG_FUNCTION_ARGS)
                                622                 :                : {
                                623                 :             11 :     SEG        *a = PG_GETARG_SEG_P(0);
                                624                 :             11 :     SEG        *b = PG_GETARG_SEG_P(1);
                                625                 :                : 
                                626                 :             11 :     PG_RETURN_BOOL(a->lower >= b->lower);
                                627                 :                : }
                                628                 :                : 
                                629                 :                : Datum
                                630                 :          11433 : seg_union(PG_FUNCTION_ARGS)
                                631                 :                : {
                                632                 :          11433 :     SEG        *a = PG_GETARG_SEG_P(0);
                                633                 :          11433 :     SEG        *b = PG_GETARG_SEG_P(1);
                                634                 :                :     SEG        *n;
                                635                 :                : 
  100 michael@paquier.xyz       636                 :GNC       11433 :     n = palloc_object(SEG);
                                637                 :                : 
                                638                 :                :     /* take max of upper endpoints */
 9124 bruce@momjian.us          639         [ +  + ]:CBC       11433 :     if (a->upper > b->upper)
                                640                 :                :     {
                                641                 :          10489 :         n->upper = a->upper;
                                642                 :          10489 :         n->u_sigd = a->u_sigd;
                                643                 :          10489 :         n->u_ext = a->u_ext;
                                644                 :                :     }
                                645                 :                :     else
                                646                 :                :     {
                                647                 :            944 :         n->upper = b->upper;
                                648                 :            944 :         n->u_sigd = b->u_sigd;
                                649                 :            944 :         n->u_ext = b->u_ext;
                                650                 :                :     }
                                651                 :                : 
                                652                 :                :     /* take min of lower endpoints */
                                653         [ +  + ]:          11433 :     if (a->lower < b->lower)
                                654                 :                :     {
                                655                 :          11093 :         n->lower = a->lower;
                                656                 :          11093 :         n->l_sigd = a->l_sigd;
                                657                 :          11093 :         n->l_ext = a->l_ext;
                                658                 :                :     }
                                659                 :                :     else
                                660                 :                :     {
                                661                 :            340 :         n->lower = b->lower;
                                662                 :            340 :         n->l_sigd = b->l_sigd;
                                663                 :            340 :         n->l_ext = b->l_ext;
                                664                 :                :     }
                                665                 :                : 
 3273 andres@anarazel.de        666                 :          11433 :     PG_RETURN_POINTER(n);
                                667                 :                : }
                                668                 :                : 
                                669                 :                : Datum
 3273 andres@anarazel.de        670                 :UBC           0 : seg_inter(PG_FUNCTION_ARGS)
                                671                 :                : {
                                672                 :              0 :     SEG        *a = PG_GETARG_SEG_P(0);
                                673                 :              0 :     SEG        *b = PG_GETARG_SEG_P(1);
                                674                 :                :     SEG        *n;
                                675                 :                : 
  100 michael@paquier.xyz       676                 :UNC           0 :     n = palloc_object(SEG);
                                677                 :                : 
                                678                 :                :     /* take min of upper endpoints */
 9124 bruce@momjian.us          679         [ #  # ]:UBC           0 :     if (a->upper < b->upper)
                                680                 :                :     {
                                681                 :              0 :         n->upper = a->upper;
                                682                 :              0 :         n->u_sigd = a->u_sigd;
                                683                 :              0 :         n->u_ext = a->u_ext;
                                684                 :                :     }
                                685                 :                :     else
                                686                 :                :     {
                                687                 :              0 :         n->upper = b->upper;
                                688                 :              0 :         n->u_sigd = b->u_sigd;
                                689                 :              0 :         n->u_ext = b->u_ext;
                                690                 :                :     }
                                691                 :                : 
                                692                 :                :     /* take max of lower endpoints */
                                693         [ #  # ]:              0 :     if (a->lower > b->lower)
                                694                 :                :     {
                                695                 :              0 :         n->lower = a->lower;
                                696                 :              0 :         n->l_sigd = a->l_sigd;
                                697                 :              0 :         n->l_ext = a->l_ext;
                                698                 :                :     }
                                699                 :                :     else
                                700                 :                :     {
                                701                 :              0 :         n->lower = b->lower;
                                702                 :              0 :         n->l_sigd = b->l_sigd;
                                703                 :              0 :         n->l_ext = b->l_ext;
                                704                 :                :     }
                                705                 :                : 
 3273 andres@anarazel.de        706                 :              0 :     PG_RETURN_POINTER(n);
                                707                 :                : }
                                708                 :                : 
                                709                 :                : static void
 6121 bruce@momjian.us          710                 :CBC        9394 : rt_seg_size(SEG *a, float *size)
                                711                 :                : {
 9124                           712   [ +  -  -  + ]:           9394 :     if (a == (SEG *) NULL || a->upper <= a->lower)
 9124 bruce@momjian.us          713                 :UBC           0 :         *size = 0.0;
                                714                 :                :     else
 1254 peter@eisentraut.org      715                 :CBC        9394 :         *size = fabsf(a->upper - a->lower);
 9225 tgl@sss.pgh.pa.us         716                 :           9394 : }
                                717                 :                : 
                                718                 :                : Datum
 6539 tgl@sss.pgh.pa.us         719                 :UBC           0 : seg_size(PG_FUNCTION_ARGS)
                                720                 :                : {
 3273 andres@anarazel.de        721                 :              0 :     SEG        *seg = PG_GETARG_SEG_P(0);
                                722                 :                : 
 1254 peter@eisentraut.org      723                 :              0 :     PG_RETURN_FLOAT4(fabsf(seg->upper - seg->lower));
                                724                 :                : }
                                725                 :                : 
                                726                 :                : 
                                727                 :                : /*****************************************************************************
                                728                 :                :  *                 Miscellaneous operators
                                729                 :                :  *****************************************************************************/
                                730                 :                : Datum
 3273 andres@anarazel.de        731                 :CBC        4433 : seg_cmp(PG_FUNCTION_ARGS)
                                732                 :                : {
                                733                 :           4433 :     SEG        *a = PG_GETARG_SEG_P(0);
                                734                 :           4433 :     SEG        *b = PG_GETARG_SEG_P(1);
                                735                 :                : 
                                736                 :                :     /*
                                737                 :                :      * First compare on lower boundary position
                                738                 :                :      */
 9124 bruce@momjian.us          739         [ +  + ]:           4433 :     if (a->lower < b->lower)
 3273 andres@anarazel.de        740                 :            904 :         PG_RETURN_INT32(-1);
 9124 bruce@momjian.us          741         [ +  + ]:           3529 :     if (a->lower > b->lower)
 3273 andres@anarazel.de        742                 :            801 :         PG_RETURN_INT32(1);
                                743                 :                : 
                                744                 :                :     /*
                                745                 :                :      * a->lower == b->lower, so consider type of boundary.
                                746                 :                :      *
                                747                 :                :      * A '-' lower bound is < any other kind (this could only be relevant if
                                748                 :                :      * -HUGE_VAL is used as a regular data value). A '<' lower bound is < any
                                749                 :                :      * other kind except '-'. A '>' lower bound is > any other kind.
                                750                 :                :      */
 9124 bruce@momjian.us          751         [ +  + ]:           2728 :     if (a->l_ext != b->l_ext)
                                752                 :                :     {
                                753         [ -  + ]:             64 :         if (a->l_ext == '-')
 3273 andres@anarazel.de        754                 :UBC           0 :             PG_RETURN_INT32(-1);
 9124 bruce@momjian.us          755         [ -  + ]:CBC          64 :         if (b->l_ext == '-')
 3273 andres@anarazel.de        756                 :UBC           0 :             PG_RETURN_INT32(1);
 9124 bruce@momjian.us          757         [ +  + ]:CBC          64 :         if (a->l_ext == '<')
 3273 andres@anarazel.de        758                 :             24 :             PG_RETURN_INT32(-1);
 9124 bruce@momjian.us          759         [ +  + ]:             40 :         if (b->l_ext == '<')
 3273 andres@anarazel.de        760                 :             21 :             PG_RETURN_INT32(1);
 9124 bruce@momjian.us          761         [ +  + ]:             19 :         if (a->l_ext == '>')
 3273 andres@anarazel.de        762                 :             13 :             PG_RETURN_INT32(1);
 9124 bruce@momjian.us          763         [ +  - ]:              6 :         if (b->l_ext == '>')
 3273 andres@anarazel.de        764                 :              6 :             PG_RETURN_INT32(-1);
                                765                 :                :     }
                                766                 :                : 
                                767                 :                :     /*
                                768                 :                :      * For other boundary types, consider # of significant digits first.
                                769                 :                :      */
 7456 bruce@momjian.us          770         [ +  + ]:           2664 :     if (a->l_sigd < b->l_sigd) /* (a) is blurred and is likely to include (b) */
 3273 andres@anarazel.de        771                 :             18 :         PG_RETURN_INT32(-1);
 9124 bruce@momjian.us          772         [ +  + ]:           2646 :     if (a->l_sigd > b->l_sigd) /* (a) is less blurred and is likely to be
                                773                 :                :                                  * included in (b) */
 3273 andres@anarazel.de        774                 :             18 :         PG_RETURN_INT32(1);
                                775                 :                : 
                                776                 :                :     /*
                                777                 :                :      * For same # of digits, an approximate boundary is more blurred than
                                778                 :                :      * exact.
                                779                 :                :      */
 9124 bruce@momjian.us          780         [ -  + ]:           2628 :     if (a->l_ext != b->l_ext)
                                781                 :                :     {
 9124 bruce@momjian.us          782         [ #  # ]:UBC           0 :         if (a->l_ext == '~') /* (a) is approximate, while (b) is exact */
 3273 andres@anarazel.de        783                 :              0 :             PG_RETURN_INT32(-1);
 9124 bruce@momjian.us          784         [ #  # ]:              0 :         if (b->l_ext == '~')
 3273 andres@anarazel.de        785                 :              0 :             PG_RETURN_INT32(1);
                                786                 :                :         /* can't get here unless data is corrupt */
 8270 tgl@sss.pgh.pa.us         787         [ #  # ]:              0 :         elog(ERROR, "bogus lower boundary types %d %d",
                                788                 :                :              (int) a->l_ext, (int) b->l_ext);
                                789                 :                :     }
                                790                 :                : 
                                791                 :                :     /* at this point, the lower boundaries are identical */
                                792                 :                : 
                                793                 :                :     /*
                                794                 :                :      * First compare on upper boundary position
                                795                 :                :      */
 9124 bruce@momjian.us          796         [ +  + ]:CBC        2628 :     if (a->upper < b->upper)
 3273 andres@anarazel.de        797                 :            162 :         PG_RETURN_INT32(-1);
 9124 bruce@momjian.us          798         [ +  + ]:           2466 :     if (a->upper > b->upper)
 3273 andres@anarazel.de        799                 :            126 :         PG_RETURN_INT32(1);
                                800                 :                : 
                                801                 :                :     /*
                                802                 :                :      * a->upper == b->upper, so consider type of boundary.
                                803                 :                :      *
                                804                 :                :      * A '-' upper bound is > any other kind (this could only be relevant if
                                805                 :                :      * HUGE_VAL is used as a regular data value). A '<' upper bound is < any
                                806                 :                :      * other kind. A '>' upper bound is > any other kind except '-'.
                                807                 :                :      */
 9124 bruce@momjian.us          808         [ +  + ]:           2340 :     if (a->u_ext != b->u_ext)
                                809                 :                :     {
                                810         [ -  + ]:             38 :         if (a->u_ext == '-')
 3273 andres@anarazel.de        811                 :UBC           0 :             PG_RETURN_INT32(1);
 9124 bruce@momjian.us          812         [ -  + ]:CBC          38 :         if (b->u_ext == '-')
 3273 andres@anarazel.de        813                 :UBC           0 :             PG_RETURN_INT32(-1);
 9124 bruce@momjian.us          814         [ +  + ]:CBC          38 :         if (a->u_ext == '<')
 3273 andres@anarazel.de        815                 :              3 :             PG_RETURN_INT32(-1);
 9124 bruce@momjian.us          816         [ +  + ]:             35 :         if (b->u_ext == '<')
 3273 andres@anarazel.de        817                 :              1 :             PG_RETURN_INT32(1);
 9124 bruce@momjian.us          818         [ +  + ]:             34 :         if (a->u_ext == '>')
 3273 andres@anarazel.de        819                 :             20 :             PG_RETURN_INT32(1);
 9124 bruce@momjian.us          820         [ +  - ]:             14 :         if (b->u_ext == '>')
 3273 andres@anarazel.de        821                 :             14 :             PG_RETURN_INT32(-1);
                                822                 :                :     }
                                823                 :                : 
                                824                 :                :     /*
                                825                 :                :      * For other boundary types, consider # of significant digits first. Note
                                826                 :                :      * result here is converse of the lower-boundary case.
                                827                 :                :      */
 7456 bruce@momjian.us          828         [ +  + ]:           2302 :     if (a->u_sigd < b->u_sigd) /* (a) is blurred and is likely to include (b) */
 3273 andres@anarazel.de        829                 :              2 :         PG_RETURN_INT32(1);
 9124 bruce@momjian.us          830         [ +  + ]:           2300 :     if (a->u_sigd > b->u_sigd) /* (a) is less blurred and is likely to be
                                831                 :                :                                  * included in (b) */
 3273 andres@anarazel.de        832                 :              4 :         PG_RETURN_INT32(-1);
                                833                 :                : 
                                834                 :                :     /*
                                835                 :                :      * For same # of digits, an approximate boundary is more blurred than
                                836                 :                :      * exact.  Again, result is converse of lower-boundary case.
                                837                 :                :      */
 9124 bruce@momjian.us          838         [ -  + ]:           2296 :     if (a->u_ext != b->u_ext)
                                839                 :                :     {
 9124 bruce@momjian.us          840         [ #  # ]:UBC           0 :         if (a->u_ext == '~') /* (a) is approximate, while (b) is exact */
 3273 andres@anarazel.de        841                 :              0 :             PG_RETURN_INT32(1);
 9124 bruce@momjian.us          842         [ #  # ]:              0 :         if (b->u_ext == '~')
 3273 andres@anarazel.de        843                 :              0 :             PG_RETURN_INT32(-1);
                                844                 :                :         /* can't get here unless data is corrupt */
 8270 tgl@sss.pgh.pa.us         845         [ #  # ]:              0 :         elog(ERROR, "bogus upper boundary types %d %d",
                                846                 :                :              (int) a->u_ext, (int) b->u_ext);
                                847                 :                :     }
                                848                 :                : 
 3273 andres@anarazel.de        849                 :CBC        2296 :     PG_RETURN_INT32(0);
                                850                 :                : }
                                851                 :                : 
                                852                 :                : Datum
 3273 andres@anarazel.de        853                 :UBC           0 : seg_lt(PG_FUNCTION_ARGS)
                                854                 :                : {
 2236 alvherre@alvh.no-ip.      855                 :              0 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                856                 :                :                                                         PG_GETARG_DATUM(0),
                                857                 :                :                                                         PG_GETARG_DATUM(1)));
                                858                 :                : 
 3273 andres@anarazel.de        859                 :              0 :     PG_RETURN_BOOL(cmp < 0);
                                860                 :                : }
                                861                 :                : 
                                862                 :                : Datum
                                863                 :              0 : seg_le(PG_FUNCTION_ARGS)
                                864                 :                : {
 2236 alvherre@alvh.no-ip.      865                 :              0 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                866                 :                :                                                         PG_GETARG_DATUM(0),
                                867                 :                :                                                         PG_GETARG_DATUM(1)));
                                868                 :                : 
 3273 andres@anarazel.de        869                 :              0 :     PG_RETURN_BOOL(cmp <= 0);
                                870                 :                : }
                                871                 :                : 
                                872                 :                : Datum
                                873                 :              0 : seg_gt(PG_FUNCTION_ARGS)
                                874                 :                : {
 2236 alvherre@alvh.no-ip.      875                 :              0 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                876                 :                :                                                         PG_GETARG_DATUM(0),
                                877                 :                :                                                         PG_GETARG_DATUM(1)));
                                878                 :                : 
 3273 andres@anarazel.de        879                 :              0 :     PG_RETURN_BOOL(cmp > 0);
                                880                 :                : }
                                881                 :                : 
                                882                 :                : Datum
                                883                 :              0 : seg_ge(PG_FUNCTION_ARGS)
                                884                 :                : {
 2236 alvherre@alvh.no-ip.      885                 :              0 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                886                 :                :                                                         PG_GETARG_DATUM(0),
                                887                 :                :                                                         PG_GETARG_DATUM(1)));
                                888                 :                : 
 3273 andres@anarazel.de        889                 :              0 :     PG_RETURN_BOOL(cmp >= 0);
                                890                 :                : }
                                891                 :                : 
                                892                 :                : 
                                893                 :                : Datum
 3273 andres@anarazel.de        894                 :CBC           2 : seg_different(PG_FUNCTION_ARGS)
                                895                 :                : {
 2236 alvherre@alvh.no-ip.      896                 :              2 :     int         cmp = DatumGetInt32(DirectFunctionCall2(seg_cmp,
                                897                 :                :                                                         PG_GETARG_DATUM(0),
                                898                 :                :                                                         PG_GETARG_DATUM(1)));
                                899                 :                : 
 3273 andres@anarazel.de        900                 :              2 :     PG_RETURN_BOOL(cmp != 0);
                                901                 :                : }
                                902                 :                : 
                                903                 :                : 
                                904                 :                : 
                                905                 :                : /*****************************************************************************
                                906                 :                :  *                 Auxiliary functions
                                907                 :                :  *****************************************************************************/
                                908                 :                : 
                                909                 :                : /*
                                910                 :                :  * The purpose of this routine is to print the given floating point
                                911                 :                :  * value with exactly n significant digits.  Its behaviour
                                912                 :                :  * is similar to %.ng except it prints 8.00 where %.ng would
                                913                 :                :  * print 8.  Returns the length of the string written at "result".
                                914                 :                :  *
                                915                 :                :  * Caller must provide a sufficiently large result buffer; 16 bytes
                                916                 :                :  * should be enough for all known float implementations.
                                917                 :                :  */
                                918                 :                : static int
 9124 bruce@momjian.us          919                 :            325 : restore(char *result, float val, int n)
                                920                 :                : {
                                921                 :            325 :     char        buf[25] = {
                                922                 :                :         '0', '0', '0', '0', '0',
                                923                 :                :         '0', '0', '0', '0', '0',
                                924                 :                :         '0', '0', '0', '0', '0',
                                925                 :                :         '0', '0', '0', '0', '0',
                                926                 :                :         '0', '0', '0', '0', '\0'
                                927                 :                :     };
                                928                 :                :     char       *p;
                                929                 :                :     int         exp;
                                930                 :                :     int         i,
                                931                 :                :                 dp,
                                932                 :                :                 sign;
                                933                 :                : 
                                934                 :                :     /*
                                935                 :                :      * Put a cap on the number of significant digits to avoid garbage in the
                                936                 :                :      * output and ensure we don't overrun the result buffer.  (n should not be
                                937                 :                :      * negative, but check to protect ourselves against corrupted data.)
                                938                 :                :      */
 1180 tgl@sss.pgh.pa.us         939         [ -  + ]:            325 :     if (n <= 0)
 1180 tgl@sss.pgh.pa.us         940                 :UBC           0 :         n = FLT_DIG;
                                941                 :                :     else
 1180 tgl@sss.pgh.pa.us         942                 :CBC         325 :         n = Min(n, FLT_DIG);
                                943                 :                : 
                                944                 :                :     /* remember the sign */
 9124 bruce@momjian.us          945                 :            325 :     sign = (val < 0 ? 1 : 0);
                                946                 :                : 
                                947                 :                :     /* print, in %e style to start with */
 3633 tgl@sss.pgh.pa.us         948                 :            325 :     sprintf(result, "%.*e", n - 1, val);
                                949                 :                : 
                                950                 :                :     /* find the exponent */
                                951                 :            325 :     p = strchr(result, 'e');
                                952                 :                : 
                                953                 :                :     /* punt if we have 'inf' or similar */
                                954         [ -  + ]:            325 :     if (p == NULL)
 3633 tgl@sss.pgh.pa.us         955                 :UBC           0 :         return strlen(result);
                                956                 :                : 
 3633 tgl@sss.pgh.pa.us         957                 :CBC         325 :     exp = atoi(p + 1);
 9124 bruce@momjian.us          958         [ +  + ]:            325 :     if (exp == 0)
                                959                 :                :     {
                                960                 :                :         /* just truncate off the 'e+00' */
 3633 tgl@sss.pgh.pa.us         961                 :            169 :         *p = '\0';
                                962                 :                :     }
                                963                 :                :     else
                                964                 :                :     {
 1255 peter@eisentraut.org      965   [ +  +  +  + ]:            156 :         if (abs(exp) <= 4)
                                966                 :                :         {
                                967                 :                :             /*
                                968                 :                :              * remove the decimal point from the mantissa and write the digits
                                969                 :                :              * to the buf array
                                970                 :                :              */
 9124 bruce@momjian.us          971         [ +  + ]:            637 :             for (p = result + sign, i = 10, dp = 0; *p != 'e'; p++, i++)
                                972                 :                :             {
                                973                 :            499 :                 buf[i] = *p;
                                974         [ +  + ]:            499 :                 if (*p == '.')
                                975                 :                :                 {
                                976                 :            130 :                     dp = i--;   /* skip the decimal point */
                                977                 :                :                 }
                                978                 :                :             }
                                979         [ +  + ]:            138 :             if (dp == 0)
                                980                 :              8 :                 dp = i--;       /* no decimal point was found in the above
                                981                 :                :                                  * for() loop */
                                982                 :                : 
                                983         [ +  + ]:            138 :             if (exp > 0)
                                984                 :                :             {
                                985         [ +  + ]:            133 :                 if (dp - 10 + exp >= n)
                                986                 :                :                 {
                                987                 :                :                     /*
                                988                 :                :                      * the decimal point is behind the last significant digit;
                                989                 :                :                      * the digits in between must be converted to the exponent
                                990                 :                :                      * and the decimal point placed after the first digit
                                991                 :                :                      */
                                992                 :             43 :                     exp = dp - 10 + exp - n;
                                993                 :             43 :                     buf[10 + n] = '\0';
                                994                 :                : 
                                995                 :                :                     /* insert the decimal point */
                                996         [ +  + ]:             43 :                     if (n > 1)
                                997                 :                :                     {
                                998                 :             39 :                         dp = 11;
                                999         [ +  + ]:            507 :                         for (i = 23; i > dp; i--)
                               1000                 :            468 :                             buf[i] = buf[i - 1];
                               1001                 :             39 :                         buf[dp] = '.';
                               1002                 :                :                     }
                               1003                 :                : 
                               1004                 :                :                     /*
                               1005                 :                :                      * adjust the exponent by the number of digits after the
                               1006                 :                :                      * decimal point
                               1007                 :                :                      */
                               1008         [ +  + ]:             43 :                     if (n > 1)
                               1009                 :             39 :                         sprintf(&buf[11 + n], "e%d", exp + n - 1);
                               1010                 :                :                     else
                               1011                 :              4 :                         sprintf(&buf[11], "e%d", exp + n - 1);
                               1012                 :                : 
                               1013         [ -  + ]:             43 :                     if (sign)
                               1014                 :                :                     {
 9124 bruce@momjian.us         1015                 :UBC           0 :                         buf[9] = '-';
                               1016                 :              0 :                         strcpy(result, &buf[9]);
                               1017                 :                :                     }
                               1018                 :                :                     else
 9124 bruce@momjian.us         1019                 :CBC          43 :                         strcpy(result, &buf[10]);
                               1020                 :                :                 }
                               1021                 :                :                 else
                               1022                 :                :                 {               /* insert the decimal point */
                               1023                 :             90 :                     dp += exp;
                               1024         [ +  + ]:           1080 :                     for (i = 23; i > dp; i--)
                               1025                 :            990 :                         buf[i] = buf[i - 1];
                               1026                 :             90 :                     buf[11 + n] = '\0';
                               1027                 :             90 :                     buf[dp] = '.';
                               1028         [ -  + ]:             90 :                     if (sign)
                               1029                 :                :                     {
 9124 bruce@momjian.us         1030                 :UBC           0 :                         buf[9] = '-';
                               1031                 :              0 :                         strcpy(result, &buf[9]);
                               1032                 :                :                     }
                               1033                 :                :                     else
 9124 bruce@momjian.us         1034                 :CBC          90 :                         strcpy(result, &buf[10]);
                               1035                 :                :                 }
                               1036                 :                :             }
                               1037                 :                :             else
                               1038                 :                :             {                   /* exp <= 0 */
                               1039                 :              5 :                 dp += exp - 1;
                               1040                 :              5 :                 buf[10 + n] = '\0';
                               1041                 :              5 :                 buf[dp] = '.';
                               1042         [ -  + ]:              5 :                 if (sign)
                               1043                 :                :                 {
 9124 bruce@momjian.us         1044                 :UBC           0 :                     buf[dp - 2] = '-';
                               1045                 :              0 :                     strcpy(result, &buf[dp - 2]);
                               1046                 :                :                 }
                               1047                 :                :                 else
 9124 bruce@momjian.us         1048                 :CBC           5 :                     strcpy(result, &buf[dp - 1]);
                               1049                 :                :             }
                               1050                 :                :         }
                               1051                 :                : 
                               1052                 :                :         /* do nothing for abs(exp) > 4; %e must be OK */
                               1053                 :                :         /* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
                               1054                 :                : 
                               1055                 :                :         /* ... this is not done yet. */
                               1056                 :                :     }
 3132 peter_e@gmx.net          1057                 :            325 :     return strlen(result);
                               1058                 :                : }
                               1059                 :                : 
                               1060                 :                : 
                               1061                 :                : /*
                               1062                 :                : ** Miscellany
                               1063                 :                : */
                               1064                 :                : 
                               1065                 :                : /* find out the number of significant digits in a string representing
                               1066                 :                :  * a floating point number
                               1067                 :                :  */
                               1068                 :                : int
 3057                          1069                 :           5191 : significant_digits(const char *s)
                               1070                 :                : {
                               1071                 :           5191 :     const char *p = s;
                               1072                 :                :     int         n,
                               1073                 :                :                 c,
                               1074                 :                :                 zeroes;
                               1075                 :                : 
 9124 bruce@momjian.us         1076                 :           5191 :     zeroes = 1;
                               1077                 :                :     /* skip leading zeroes and sign */
                               1078   [ +  +  +  -  :           5330 :     for (c = *p; (c == '0' || c == '+' || c == '-') && c != 0; c = *(++p));
                                        +  +  +  - ]
                               1079                 :                : 
                               1080                 :                :     /* skip decimal point and following zeroes */
                               1081   [ +  +  +  +  :           5212 :     for (c = *p; (c == '0' || c == '.') && c != 0; c = *(++p))
                                              +  - ]
                               1082                 :                :     {
                               1083         [ +  + ]:             21 :         if (c != '.')
                               1084                 :             11 :             zeroes++;
                               1085                 :                :     }
                               1086                 :                : 
                               1087                 :                :     /* count significant digits (n) */
                               1088         [ +  + ]:          20353 :     for (c = *p, n = 0; c != 0; c = *(++p))
                               1089                 :                :     {
                               1090   [ +  +  +  +  :          15189 :         if (!((c >= '0' && c <= '9') || (c == '.')))
                                              +  + ]
                               1091                 :             27 :             break;
                               1092         [ +  + ]:          15162 :         if (c != '.')
                               1093                 :          10617 :             n++;
                               1094                 :                :     }
                               1095                 :                : 
                               1096         [ +  + ]:           5191 :     if (!n)
 3132 peter_e@gmx.net          1097                 :             98 :         return zeroes;
                               1098                 :                : 
                               1099                 :           5093 :     return n;
                               1100                 :                : }
        

Generated by: LCOV version 2.4-beta