LCOV - differential code coverage report
Current view: top level - contrib/btree_gist - btree_numeric.c (source / functions) Coverage Total Hit UBC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 99.0 % 101 100 1 100
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 21 21 21
Baseline: lcov-20261128-baseline Branches: 95.0 % 20 19 1 19
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
[..1] days: 100.0 % 15 15 15
(360..) days: 98.8 % 86 85 1 85
Function coverage date bins:
(360..) days: 100.0 % 21 21 21
Branch coverage date bins:
[..1] days: 100.0 % 8 8 8
(360..) days: 91.7 % 12 11 1 11

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * contrib/btree_gist/btree_numeric.c
                                  3                 :                :  *
                                  4                 :                :  * Support for numeric data type.
                                  5                 :                :  */
                                  6                 :                : #include "postgres.h"
                                  7                 :                : 
                                  8                 :                : #include <float.h>
                                  9                 :                : 
                                 10                 :                : #include "btree_gist.h"
                                 11                 :                : #include "btree_utils_var.h"
                                 12                 :                : #include "nodes/miscnodes.h"
                                 13                 :                : #include "utils/builtins.h"
                                 14                 :                : #include "utils/numeric.h"
                                 15                 :                : #include "utils/rel.h"
                                 16                 :                : #include "utils/sortsupport.h"
                                 17                 :                : 
                                 18                 :                : /* GiST support functions */
 8219 teodor@sigaev.ru           19                 :CBC           5 : PG_FUNCTION_INFO_V1(gbt_numeric_compress);
                                 20                 :              5 : PG_FUNCTION_INFO_V1(gbt_numeric_union);
                                 21                 :              5 : PG_FUNCTION_INFO_V1(gbt_numeric_picksplit);
                                 22                 :              5 : PG_FUNCTION_INFO_V1(gbt_numeric_consistent);
                                 23                 :              5 : PG_FUNCTION_INFO_V1(gbt_numeric_penalty);
                                 24                 :              5 : PG_FUNCTION_INFO_V1(gbt_numeric_same);
  604 heikki.linnakangas@i       25                 :              5 : PG_FUNCTION_INFO_V1(gbt_numeric_sortsupport);
                                 26                 :                : 
                                 27                 :                : 
                                 28                 :                : /* define for comparison */
                                 29                 :                : 
                                 30                 :                : static bool
 3539 andrew@dunslane.net        31                 :           2700 : gbt_numeric_gt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 32                 :                : {
 5699 tgl@sss.pgh.pa.us          33                 :           2700 :     return DatumGetBool(DirectFunctionCall2(numeric_gt,
                                 34                 :                :                                             PointerGetDatum(a),
                                 35                 :                :                                             PointerGetDatum(b)));
                                 36                 :                : }
                                 37                 :                : 
                                 38                 :                : static bool
 3539 andrew@dunslane.net        39                 :           1965 : gbt_numeric_ge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 40                 :                : {
 5699 tgl@sss.pgh.pa.us          41                 :           1965 :     return DatumGetBool(DirectFunctionCall2(numeric_ge,
                                 42                 :                :                                             PointerGetDatum(a),
                                 43                 :                :                                             PointerGetDatum(b)));
                                 44                 :                : }
                                 45                 :                : 
                                 46                 :                : static bool
 3539 andrew@dunslane.net        47                 :            567 : gbt_numeric_eq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 48                 :                : {
 5699 tgl@sss.pgh.pa.us          49                 :            567 :     return DatumGetBool(DirectFunctionCall2(numeric_eq,
                                 50                 :                :                                             PointerGetDatum(a),
                                 51                 :                :                                             PointerGetDatum(b)));
                                 52                 :                : }
                                 53                 :                : 
                                 54                 :                : static bool
 3539 andrew@dunslane.net        55                 :           2198 : gbt_numeric_le(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 56                 :                : {
 5699 tgl@sss.pgh.pa.us          57                 :           2198 :     return DatumGetBool(DirectFunctionCall2(numeric_le,
                                 58                 :                :                                             PointerGetDatum(a),
                                 59                 :                :                                             PointerGetDatum(b)));
                                 60                 :                : }
                                 61                 :                : 
                                 62                 :                : static bool
 3539 andrew@dunslane.net        63                 :           2185 : gbt_numeric_lt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 64                 :                : {
 5699 tgl@sss.pgh.pa.us          65                 :           2185 :     return DatumGetBool(DirectFunctionCall2(numeric_lt,
                                 66                 :                :                                             PointerGetDatum(a),
                                 67                 :                :                                             PointerGetDatum(b)));
                                 68                 :                : }
                                 69                 :                : 
                                 70                 :                : static int32
 3539 andrew@dunslane.net        71                 :          49643 : gbt_numeric_cmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 72                 :                : {
 5699 tgl@sss.pgh.pa.us          73                 :          49643 :     return DatumGetInt32(DirectFunctionCall2(numeric_cmp,
                                 74                 :                :                                              PointerGetDatum(a),
                                 75                 :                :                                              PointerGetDatum(b)));
                                 76                 :                : }
                                 77                 :                : 
                                 78                 :                : 
                                 79                 :                : /*
                                 80                 :                :  * We could conceivably support internal-key truncation here, but it would
                                 81                 :                :  * require custom truncation code, and most values wouldn't be long enough
                                 82                 :                :  * to make it worthwhile.
                                 83                 :                :  */
                                 84                 :                : static const gbtree_vinfo tinfo =
                                 85                 :                : {
                                 86                 :                :     gbt_t_numeric,
                                 87                 :                :     false,                      /* no truncation permitted */
                                 88                 :                :     gbt_numeric_gt,
                                 89                 :                :     gbt_numeric_ge,
                                 90                 :                :     gbt_numeric_eq,
                                 91                 :                :     gbt_numeric_le,
                                 92                 :                :     gbt_numeric_lt,
                                 93                 :                :     gbt_numeric_cmp,
                                 94                 :                :     NULL
                                 95                 :                : };
                                 96                 :                : 
                                 97                 :                : 
                                 98                 :                : /**************************************************
                                 99                 :                :  * GiST support functions
                                100                 :                :  **************************************************/
                                101                 :                : 
                                102                 :                : Datum
 8126 bruce@momjian.us          103                 :           4168 : gbt_numeric_compress(PG_FUNCTION_ARGS)
                                104                 :                : {
                                105                 :           4168 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
                                106                 :                : 
                                107                 :           4168 :     PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
                                108                 :                : }
                                109                 :                : 
                                110                 :                : Datum
 8219 teodor@sigaev.ru          111                 :           9738 : gbt_numeric_consistent(PG_FUNCTION_ARGS)
                                112                 :                : {
 8126 bruce@momjian.us          113                 :           9738 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
  730 peter@eisentraut.org      114                 :           9738 :     void       *query = DatumGetNumeric(PG_GETARG_DATUM(1));
 8126 bruce@momjian.us          115                 :           9738 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
                                116                 :                : #ifdef NOT_USED
                                117                 :                :     Oid         subtype = PG_GETARG_OID(3);
                                118                 :                : #endif
 6802 tgl@sss.pgh.pa.us         119                 :           9738 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
                                120                 :                :     bool        retval;
                                121                 :           9738 :     GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
 8126 bruce@momjian.us          122                 :           9738 :     GBT_VARKEY_R r = gbt_var_key_readable(key);
                                123                 :                : 
                                124                 :                :     /* All cases served by this function are exact */
 6802 tgl@sss.pgh.pa.us         125                 :           9738 :     *recheck = false;
                                126                 :                : 
 5699                           127                 :          19476 :     retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
 3539 andrew@dunslane.net       128                 :           9738 :                                 GIST_LEAF(entry), &tinfo, fcinfo->flinfo);
 8126 bruce@momjian.us          129                 :           9738 :     PG_RETURN_BOOL(retval);
                                130                 :                : }
                                131                 :                : 
                                132                 :                : Datum
 8219 teodor@sigaev.ru          133                 :           1864 : gbt_numeric_union(PG_FUNCTION_ARGS)
                                134                 :                : {
 8126 bruce@momjian.us          135                 :           1864 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
                                136                 :           1864 :     int32      *size = (int *) PG_GETARG_POINTER(1);
                                137                 :                : 
 5699 tgl@sss.pgh.pa.us         138                 :           1864 :     PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
                                139                 :                :                                     &tinfo, fcinfo->flinfo));
                                140                 :                : }
                                141                 :                : 
                                142                 :                : Datum
 8219 teodor@sigaev.ru          143                 :           1819 : gbt_numeric_same(PG_FUNCTION_ARGS)
                                144                 :                : {
 8126 bruce@momjian.us          145                 :           1819 :     Datum       d1 = PG_GETARG_DATUM(0);
                                146                 :           1819 :     Datum       d2 = PG_GETARG_DATUM(1);
                                147                 :           1819 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
                                148                 :                : 
 3539 andrew@dunslane.net       149                 :           1819 :     *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
 5699 tgl@sss.pgh.pa.us         150                 :           1819 :     PG_RETURN_POINTER(result);
                                151                 :                : }
                                152                 :                : 
                                153                 :                : Datum
 8126 bruce@momjian.us          154                 :           3540 : gbt_numeric_penalty(PG_FUNCTION_ARGS)
                                155                 :                : {
                                156                 :           3540 :     GISTENTRY  *o = (GISTENTRY *) PG_GETARG_POINTER(0);
                                157                 :           3540 :     GISTENTRY  *n = (GISTENTRY *) PG_GETARG_POINTER(1);
                                158                 :           3540 :     float      *result = (float *) PG_GETARG_POINTER(2);
                                159                 :                : 
                                160                 :                :     Numeric     us,
                                161                 :                :                 os,
                                162                 :                :                 ds;
                                163                 :                : 
                                164                 :           3540 :     GBT_VARKEY *org = (GBT_VARKEY *) DatumGetPointer(o->key);
                                165                 :           3540 :     GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
                                166                 :                :     Datum       uni;
                                167                 :                :     GBT_VARKEY_R rk,
                                168                 :                :                 ok,
                                169                 :                :                 uk;
    1 tgl@sss.pgh.pa.us         170                 :           3540 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
                                171                 :                : 
 8126 bruce@momjian.us          172                 :           3540 :     rk = gbt_var_key_readable(org);
 4265 heikki.linnakangas@i      173                 :           3540 :     uni = PointerGetDatum(gbt_var_key_copy(&rk));
 3539 andrew@dunslane.net       174                 :           3540 :     gbt_var_bin_union(&uni, newe, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
 8126 bruce@momjian.us          175                 :           3540 :     ok = gbt_var_key_readable(org);
                                176                 :           3540 :     uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(uni));
                                177                 :                : 
                                178                 :                :     /*
                                179                 :                :      * It's possible for these subtractions to exceed what the numeric type
                                180                 :                :      * can represent, so use numeric_sub_safe().  If we do get an overflow,
                                181                 :                :      * use Infinity for the key width.  The width can also come out as NaN or
                                182                 :                :      * Infinity because a bound is, so we must attend to both cases below.
                                183                 :                :      */
    1 tgl@sss.pgh.pa.us         184                 :           3540 :     us = numeric_sub_safe(DatumGetNumeric(PointerGetDatum(uk.upper)),
                                185                 :           3540 :                           DatumGetNumeric(PointerGetDatum(uk.lower)),
                                186                 :                :                           (Node *) &escontext);
                                187         [ +  + ]:           3540 :     if (escontext.error_occurred)
                                188                 :                :     {
                                189                 :              4 :         us = get_numeric_infinity();
                                190                 :              4 :         escontext.error_occurred = false;
                                191                 :                :     }
                                192                 :                : 
                                193                 :           3540 :     os = numeric_sub_safe(DatumGetNumeric(PointerGetDatum(ok.upper)),
                                194                 :           3540 :                           DatumGetNumeric(PointerGetDatum(ok.lower)),
                                195                 :                :                           (Node *) &escontext);
                                196         [ +  + ]:           3540 :     if (escontext.error_occurred)
                                197                 :              1 :         os = get_numeric_infinity();
                                198                 :                : 
 5965 rhaas@postgresql.org      199         [ +  + ]:           3540 :     if (numeric_is_nan(us))
                                200                 :                :     {
                                201                 :                :         /*
                                202                 :                :          * Either we have a NaN bound or the union width is Inf - Inf.  If the
                                203                 :                :          * same is true of the original key, charge 0; otherwise charge the
                                204                 :                :          * maximum.
                                205                 :                :          */
                                206         [ -  + ]:              4 :         if (numeric_is_nan(os))
 8126 bruce@momjian.us          207                 :UBC           0 :             *result = 0.0;
                                208                 :                :         else
 8126 bruce@momjian.us          209                 :CBC           4 :             *result = 1.0;
                                210                 :                :     }
    1 tgl@sss.pgh.pa.us         211         [ +  + ]:           3536 :     else if (numeric_is_inf(us))
                                212                 :                :     {
                                213                 :                :         /*
                                214                 :                :          * The union is too wide to measure.  If the original key is just as
                                215                 :                :          * wide it is a cheap home for the new entry; otherwise charge the
                                216                 :                :          * maximum.
                                217                 :                :          */
                                218         [ +  + ]:              8 :         if (numeric_is_inf(os))
                                219                 :              2 :             *result = 0.0;
                                220                 :                :         else
                                221                 :              6 :             *result = 1.0;
                                222                 :                :     }
                                223                 :                :     else
                                224                 :                :     {
 2271 peter@eisentraut.org      225                 :           3528 :         Numeric     nul = int64_to_numeric(0);
                                226                 :                : 
                                227                 :                :         /*
                                228                 :                :          * The union contains the original key, so os is finite whenever us
                                229                 :                :          * is.  So this subtraction can never see NaN or Infinity; nor can it
                                230                 :                :          * overflow a numeric, since neither input is negative.
                                231                 :                :          */
    1 tgl@sss.pgh.pa.us         232                 :           3528 :         ds = DatumGetNumeric(DirectFunctionCall2(numeric_sub,
                                233                 :                :                                                  NumericGetDatum(us),
                                234                 :                :                                                  NumericGetDatum(os)));
                                235                 :                : 
 8126 bruce@momjian.us          236                 :           3528 :         *result = 0.0;
                                237                 :                : 
  477 peter@eisentraut.org      238         [ +  + ]:           3528 :         if (DatumGetBool(DirectFunctionCall2(numeric_gt, NumericGetDatum(ds), NumericGetDatum(nul))))
                                239                 :                :         {
 8126 bruce@momjian.us          240                 :             18 :             *result += FLT_MIN;
 2494 alvherre@alvh.no-ip.      241                 :             18 :             os = DatumGetNumeric(DirectFunctionCall2(numeric_div,
                                242                 :                :                                                      NumericGetDatum(ds),
                                243                 :                :                                                      NumericGetDatum(us)));
 8126 bruce@momjian.us          244                 :             18 :             *result += (float4) DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, NumericGetDatum(os)));
                                245                 :                :         }
                                246                 :                :     }
                                247                 :                : 
                                248         [ +  + ]:           3540 :     if (*result > 0)
                                249                 :             28 :         *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
                                250                 :                : 
                                251                 :           3540 :     PG_RETURN_POINTER(result);
                                252                 :                : }
                                253                 :                : 
                                254                 :                : Datum
 8219 teodor@sigaev.ru          255                 :             27 : gbt_numeric_picksplit(PG_FUNCTION_ARGS)
                                256                 :                : {
 8126 bruce@momjian.us          257                 :             27 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
                                258                 :             27 :     GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
                                259                 :                : 
 5699 tgl@sss.pgh.pa.us         260                 :             27 :     gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
                                261                 :                :                       &tinfo, fcinfo->flinfo);
 8126 bruce@momjian.us          262                 :             27 :     PG_RETURN_POINTER(v);
                                263                 :                : }
                                264                 :                : 
                                265                 :                : static int
  604 heikki.linnakangas@i      266                 :          12513 : gbt_numeric_ssup_cmp(Datum x, Datum y, SortSupport ssup)
                                267                 :                : {
                                268                 :          12513 :     GBT_VARKEY *key1 = PG_DETOAST_DATUM(x);
                                269                 :          12513 :     GBT_VARKEY *key2 = PG_DETOAST_DATUM(y);
                                270                 :                : 
                                271                 :          12513 :     GBT_VARKEY_R arg1 = gbt_var_key_readable(key1);
                                272                 :          12513 :     GBT_VARKEY_R arg2 = gbt_var_key_readable(key2);
                                273                 :                :     Datum       result;
                                274                 :                : 
                                275                 :                :     /* for leaf items we expect lower == upper, so only compare lower */
                                276                 :          12513 :     result = DirectFunctionCall2(numeric_cmp,
                                277                 :                :                                  PointerGetDatum(arg1.lower),
                                278                 :                :                                  PointerGetDatum(arg2.lower));
                                279                 :                : 
                                280         [ +  + ]:          12513 :     GBT_FREE_IF_COPY(key1, x);
                                281         [ +  + ]:          12513 :     GBT_FREE_IF_COPY(key2, y);
                                282                 :                : 
                                283                 :          12513 :     return DatumGetInt32(result);
                                284                 :                : }
                                285                 :                : 
                                286                 :                : Datum
                                287                 :              3 : gbt_numeric_sortsupport(PG_FUNCTION_ARGS)
                                288                 :                : {
                                289                 :              3 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
                                290                 :                : 
                                291                 :              3 :     ssup->comparator = gbt_numeric_ssup_cmp;
                                292                 :              3 :     ssup->ssup_extra = NULL;
                                293                 :                : 
                                294                 :              3 :     PG_RETURN_VOID();
                                295                 :                : }
        

Generated by: LCOV version 2.0-1