LCOV - differential code coverage report
Current view: top level - contrib/jsonb_plperl - jsonb_plperl.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 94.5 % 145 137 8 5 132 3
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 10 10 1 9
Baseline: lcov-20261128-baseline Branches: 67.9 % 84 57 27 2 55
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 11 11 11
(30,360] days: 96.7 % 60 58 2 5 53
(360..) days: 91.9 % 74 68 6 68
Function coverage date bins:
(30,360] days: 100.0 % 3 3 1 2
(360..) days: 100.0 % 7 7 7
Branch coverage date bins:
(7,30] days: 50.0 % 4 2 2 2
(30,360] days: 75.6 % 41 31 10 2 29
(360..) days: 61.5 % 39 24 15 24

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : #include "postgres.h"
                                  2                 :                : 
                                  3                 :                : #include <math.h>
                                  4                 :                : 
                                  5                 :                : #include "fmgr.h"
                                  6                 :                : #include "miscadmin.h"
                                  7                 :                : #include "plperl.h"
                                  8                 :                : #include "utils/fmgrprotos.h"
                                  9                 :                : #include "utils/jsonb.h"
                                 10                 :                : 
  612 tgl@sss.pgh.pa.us          11                 :CBC           2 : PG_MODULE_MAGIC_EXT(
                                 12                 :                :                     .name = "jsonb_plperl",
                                 13                 :                :                     .version = PG_VERSION
                                 14                 :                : );
                                 15                 :                : 
                                 16                 :                : static SV  *Jsonb_to_SV(JsonbContainer *jsonb);
                                 17                 :                : static void SV_to_JsonbValue(SV *in, JsonbInState *jsonb_state, bool is_elem);
                                 18                 :                : 
                                 19                 :                : 
                                 20                 :                : static SV  *
 3161 peter_e@gmx.net            21                 :             81 : JsonbValue_to_SV(JsonbValue *jbv)
                                 22                 :                : {
                                 23                 :             81 :     dTHX;
                                 24                 :                : 
                                 25   [ +  +  +  +  :             81 :     switch (jbv->type)
                                              +  - ]
                                 26                 :                :     {
                                 27                 :              6 :         case jbvBinary:
 3085 tgl@sss.pgh.pa.us          28                 :              6 :             return Jsonb_to_SV(jbv->val.binary.data);
                                 29                 :                : 
 3161 peter_e@gmx.net            30                 :             49 :         case jbvNumeric:
                                 31                 :                :             {
                                 32                 :             49 :                 char       *str = DatumGetCString(DirectFunctionCall1(numeric_out,
                                 33                 :                :                                                                       NumericGetDatum(jbv->val.numeric)));
                                 34                 :             49 :                 SV         *result = newSVnv(SvNV(cstr2sv(str)));
                                 35                 :                : 
                                 36                 :             49 :                 pfree(str);
                                 37                 :             49 :                 return result;
                                 38                 :                :             }
                                 39                 :                : 
                                 40                 :             14 :         case jbvString:
                                 41                 :                :             {
                                 42                 :             14 :                 char       *str = pnstrdup(jbv->val.string.val,
                                 43                 :             14 :                                            jbv->val.string.len);
                                 44                 :             14 :                 SV         *result = cstr2sv(str);
                                 45                 :                : 
                                 46                 :             14 :                 pfree(str);
                                 47                 :             14 :                 return result;
                                 48                 :                :             }
                                 49                 :                : 
                                 50                 :              4 :         case jbvBool:
                                 51         [ +  + ]:              4 :             return newSVnv(SvNV(jbv->val.boolean ? &PL_sv_yes : &PL_sv_no));
                                 52                 :                : 
                                 53                 :              8 :         case jbvNull:
                                 54                 :              8 :             return newSV(0);
                                 55                 :                : 
 3161 peter_e@gmx.net            56                 :UBC           0 :         default:
                                 57         [ #  # ]:              0 :             elog(ERROR, "unexpected jsonb value type: %d", jbv->type);
                                 58                 :                :             return NULL;
                                 59                 :                :     }
                                 60                 :                : }
                                 61                 :                : 
                                 62                 :                : static SV  *
 3161 peter_e@gmx.net            63                 :CBC          57 : Jsonb_to_SV(JsonbContainer *jsonb)
                                 64                 :                : {
                                 65                 :             57 :     dTHX;
                                 66                 :                :     JsonbValue  v;
                                 67                 :                :     JsonbIterator *it;
                                 68                 :                :     JsonbIteratorToken r;
                                 69                 :                : 
                                 70                 :                :     /* this can recurse via JsonbValue_to_SV() */
  164 tgl@sss.pgh.pa.us          71                 :             57 :     check_stack_depth();
                                 72                 :                : 
 3161 peter_e@gmx.net            73                 :             57 :     it = JsonbIteratorInit(jsonb);
                                 74                 :             57 :     r = JsonbIteratorNext(&it, &v, true);
                                 75                 :                : 
                                 76      [ +  +  - ]:             57 :     switch (r)
                                 77                 :                :     {
                                 78                 :             39 :         case WJB_BEGIN_ARRAY:
                                 79         [ +  + ]:             39 :             if (v.val.array.rawScalar)
                                 80                 :                :             {
                                 81                 :                :                 JsonbValue  tmp;
                                 82                 :                : 
                                 83   [ +  -  +  - ]:             38 :                 if ((r = JsonbIteratorNext(&it, &v, true)) != WJB_ELEM ||
                                 84         [ -  + ]:             38 :                     (r = JsonbIteratorNext(&it, &tmp, true)) != WJB_END_ARRAY ||
                                 85                 :             19 :                     (r = JsonbIteratorNext(&it, &tmp, true)) != WJB_DONE)
 3161 peter_e@gmx.net            86         [ #  # ]:UBC           0 :                     elog(ERROR, "unexpected jsonb token: %d", r);
                                 87                 :                : 
 3085 tgl@sss.pgh.pa.us          88                 :CBC          19 :                 return JsonbValue_to_SV(&v);
                                 89                 :                :             }
                                 90                 :                :             else
                                 91                 :                :             {
 3161 peter_e@gmx.net            92                 :             20 :                 AV         *av = newAV();
                                 93                 :                : 
                                 94         [ +  + ]:            104 :                 while ((r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE)
                                 95                 :                :                 {
                                 96         [ +  + ]:             64 :                     if (r == WJB_ELEM)
                                 97                 :             44 :                         av_push(av, JsonbValue_to_SV(&v));
                                 98                 :                :                 }
                                 99                 :                : 
 3085 tgl@sss.pgh.pa.us         100                 :             20 :                 return newRV((SV *) av);
                                101                 :                :             }
                                102                 :                : 
 3161 peter_e@gmx.net           103                 :             18 :         case WJB_BEGIN_OBJECT:
                                104                 :                :             {
                                105                 :             18 :                 HV         *hv = newHV();
                                106                 :                : 
                                107         [ +  + ]:             72 :                 while ((r = JsonbIteratorNext(&it, &v, true)) != WJB_DONE)
                                108                 :                :                 {
                                109         [ +  + ]:             36 :                     if (r == WJB_KEY)
                                110                 :                :                     {
                                111                 :                :                         /* json key in v, json value in val */
                                112                 :                :                         JsonbValue  val;
                                113                 :                : 
                                114         [ +  - ]:             18 :                         if (JsonbIteratorNext(&it, &val, true) == WJB_VALUE)
                                115                 :                :                         {
                                116                 :             18 :                             SV         *value = JsonbValue_to_SV(&val);
                                117                 :                : 
                                118                 :             18 :                             (void) hv_store(hv,
                                119                 :                :                                             v.val.string.val, v.val.string.len,
                                120                 :                :                                             value, 0);
                                121                 :                :                         }
                                122                 :                :                     }
                                123                 :                :                 }
                                124                 :                : 
 3085 tgl@sss.pgh.pa.us         125                 :             18 :                 return newRV((SV *) hv);
                                126                 :                :             }
                                127                 :                : 
 3161 peter_e@gmx.net           128                 :UBC           0 :         default:
                                129         [ #  # ]:              0 :             elog(ERROR, "unexpected jsonb token: %d", r);
                                130                 :                :             return NULL;
                                131                 :                :     }
                                132                 :                : }
                                133                 :                : 
                                134                 :                : static void
  356 tgl@sss.pgh.pa.us         135                 :CBC          23 : AV_to_JsonbValue(AV *in, JsonbInState *jsonb_state)
                                136                 :                : {
 3161 peter_e@gmx.net           137                 :             23 :     dTHX;
                                138                 :                :     Size_t      pcount;
                                139                 :                : 
                                140                 :                :     /* Hold a reference on the array while we scan it */
   19 tgl@sss.pgh.pa.us         141                 :             23 :     ENTER;
                                142                 :             23 :     SAVETMPS;
                                143                 :             23 :     sv_2mortal(newRV((SV *) in));
                                144                 :                : 
                                145                 :             23 :     pcount = av_count(in);
                                146                 :                : 
 3161 peter_e@gmx.net           147                 :             23 :     pushJsonbValue(jsonb_state, WJB_BEGIN_ARRAY, NULL);
                                148                 :                : 
  103 tgl@sss.pgh.pa.us         149         [ +  + ]:             73 :     for (Size_t i = 0; i < pcount; i++)
                                150                 :                :     {
 3161 peter_e@gmx.net           151                 :             50 :         SV        **value = av_fetch(in, i, FALSE);
                                152                 :                : 
                                153         [ +  - ]:             50 :         if (value)
  356 tgl@sss.pgh.pa.us         154                 :             50 :             SV_to_JsonbValue(*value, jsonb_state, true);
                                155                 :                :     }
                                156                 :                : 
                                157                 :             23 :     pushJsonbValue(jsonb_state, WJB_END_ARRAY, NULL);
                                158                 :                : 
                                159                 :                :     /* release our reference on the array */
   19                           160         [ +  - ]:             23 :     FREETMPS;
                                161                 :             23 :     LEAVE;
 3161 peter_e@gmx.net           162                 :             23 : }
                                163                 :                : 
                                164                 :                : static void
  356 tgl@sss.pgh.pa.us         165                 :             29 : HV_to_JsonbValue(HV *obj, JsonbInState *jsonb_state)
                                166                 :                : {
 3161 peter_e@gmx.net           167                 :             29 :     dTHX;
                                168                 :                :     JsonbValue  key;
                                169                 :                :     HE         *he;
                                170                 :                : 
                                171                 :                :     /* Hold a reference on the hash while we scan it */
   19 tgl@sss.pgh.pa.us         172                 :             29 :     ENTER;
                                173                 :             29 :     SAVETMPS;
                                174                 :             29 :     sv_2mortal(newRV((SV *) obj));
                                175                 :                : 
 3161 peter_e@gmx.net           176                 :             29 :     key.type = jbvString;
                                177                 :                : 
                                178                 :             29 :     pushJsonbValue(jsonb_state, WJB_BEGIN_OBJECT, NULL);
                                179                 :                : 
                                180                 :             29 :     (void) hv_iterinit(obj);
                                181                 :                : 
  102 tgl@sss.pgh.pa.us         182         [ +  + ]:GNC          68 :     while ((he = hv_iternext(obj)))
                                183                 :                :     {
                                184                 :             39 :         char       *k = hek2cstr(he);
  101                           185                 :             39 :         SV         *val = hv_iterval(obj, he);
                                186                 :                : 
  102                           187                 :             39 :         key.val.string.val = k;
                                188                 :             39 :         key.val.string.len = strlen(k);
 3161 peter_e@gmx.net           189                 :CBC          39 :         pushJsonbValue(jsonb_state, WJB_KEY, &key);
  356 tgl@sss.pgh.pa.us         190                 :             39 :         SV_to_JsonbValue(val, jsonb_state, false);
                                191                 :                :     }
                                192                 :                : 
                                193                 :             29 :     pushJsonbValue(jsonb_state, WJB_END_OBJECT, NULL);
                                194                 :                : 
                                195                 :                :     /* release our reference on the hash */
   19                           196         [ +  - ]:             29 :     FREETMPS;
                                197                 :             29 :     LEAVE;
 3161 peter_e@gmx.net           198                 :             29 : }
                                199                 :                : 
                                200                 :                : static void
  356 tgl@sss.pgh.pa.us         201                 :            154 : SV_to_JsonbValue(SV *in, JsonbInState *jsonb_state, bool is_elem)
                                202                 :                : {
 3161 peter_e@gmx.net           203                 :            154 :     dTHX;
                                204                 :                :     JsonbValue  out;            /* result */
                                205                 :                : 
                                206                 :                :     /* this can recurse via AV_to_JsonbValue() or HV_to_JsonbValue() */
  164 tgl@sss.pgh.pa.us         207                 :            154 :     check_stack_depth();
                                208                 :                : 
                                209                 :                :     /* Dereference references recursively. */
  101                           210         [ +  - ]:            154 :     plperl_materialize_sv(in);
  102                           211   [ +  -  +  + ]:            206 :     while (in && SvROK(in))
                                212                 :                :     {
                                213                 :                :         /*
                                214                 :                :          * It's possible for circular references to make this an infinite
                                215                 :                :          * loop.  Checking for such a situation seems like much more trouble
                                216                 :                :          * than it's worth, but let's provide a way to break out of the loop.
                                217                 :                :          */
  164                           218         [ -  + ]:             52 :         CHECK_FOR_INTERRUPTS();
 3161 peter_e@gmx.net           219                 :             52 :         in = SvRV(in);
  101 tgl@sss.pgh.pa.us         220         [ +  - ]:             52 :         plperl_materialize_sv(in);
                                221                 :                :     }
                                222                 :                : 
  102                           223         [ -  + ]:            154 :     if (!in)
                                224                 :                :     {
  102 tgl@sss.pgh.pa.us         225                 :UBC           0 :         out.type = jbvNull;
                                226                 :                :     }
                                227                 :                :     else
                                228                 :                :     {
  102 tgl@sss.pgh.pa.us         229      [ +  +  + ]:CBC         154 :         switch (SvTYPE(in))
                                230                 :                :         {
                                231                 :             23 :             case SVt_PVAV:
                                232                 :             23 :                 AV_to_JsonbValue((AV *) in, jsonb_state);
                                233                 :             52 :                 return;
                                234                 :                : 
                                235                 :             29 :             case SVt_PVHV:
                                236                 :             29 :                 HV_to_JsonbValue((HV *) in, jsonb_state);
                                237                 :             29 :                 return;
                                238                 :                : 
                                239                 :            102 :             default:
                                240         [ +  + ]:            102 :                 if (!SvOK(in))
                                241                 :                :                 {
                                242                 :             13 :                     out.type = jbvNull;
                                243                 :                :                 }
                                244         [ +  + ]:             89 :                 else if (SvUOK(in))
                                245                 :                :                 {
                                246                 :                :                     /*
                                247                 :                :                      * If UV is >=64 bits, we have no better way to make this
                                248                 :                :                      * happen than converting to text and back.  Given the low
                                249                 :                :                      * usage of UV in Perl code, it's not clear it's worth
                                250                 :                :                      * working hard to provide alternate code paths.
                                251                 :                :                      */
                                252                 :              2 :                     const char *strval = SvPV_nolen(in);
                                253                 :                : 
                                254                 :              2 :                     out.type = jbvNumeric;
                                255                 :              2 :                     out.val.numeric =
                                256                 :              2 :                         DatumGetNumeric(DirectFunctionCall3(numeric_in,
                                257                 :                :                                                             CStringGetDatum(strval),
                                258                 :                :                                                             ObjectIdGetDatum(InvalidOid),
                                259                 :                :                                                             Int32GetDatum(-1)));
                                260                 :                :                 }
                                261         [ +  + ]:             87 :                 else if (SvIOK(in))
                                262                 :                :                 {
                                263                 :             12 :                     IV          ival = SvIV(in);
                                264                 :                : 
                                265                 :             12 :                     out.type = jbvNumeric;
                                266                 :             12 :                     out.val.numeric = int64_to_numeric(ival);
                                267                 :                :                 }
                                268         [ +  + ]:             75 :                 else if (SvNOK(in))
                                269                 :                :                 {
                                270                 :             53 :                     double      nval = SvNV(in);
                                271                 :                : 
                                272                 :                :                     /*
                                273                 :                :                      * jsonb doesn't allow infinity or NaN (per JSON
                                274                 :                :                      * specification), but the numeric type that is used for
                                275                 :                :                      * the storage accepts those, so we have to reject them
                                276                 :                :                      * here explicitly.
                                277                 :                :                      */
                                278         [ +  + ]:             53 :                     if (isinf(nval))
                                279         [ +  - ]:              1 :                         ereport(ERROR,
                                280                 :                :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                281                 :                :                                  errmsg("cannot convert infinity to jsonb")));
                                282         [ -  + ]:             52 :                     if (isnan(nval))
  102 tgl@sss.pgh.pa.us         283         [ #  # ]:UBC           0 :                         ereport(ERROR,
                                284                 :                :                                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                                285                 :                :                                  errmsg("cannot convert NaN to jsonb")));
                                286                 :                : 
  102 tgl@sss.pgh.pa.us         287                 :CBC          52 :                     out.type = jbvNumeric;
                                288                 :             52 :                     out.val.numeric =
                                289                 :             52 :                         DatumGetNumeric(DirectFunctionCall1(float8_numeric,
                                290                 :                :                                                             Float8GetDatum(nval)));
                                291                 :                :                 }
                                292         [ +  - ]:             22 :                 else if (SvPOK(in))
                                293                 :                :                 {
                                294                 :             22 :                     out.type = jbvString;
                                295                 :             22 :                     out.val.string.val = sv2cstr(in);
                                296                 :             22 :                     out.val.string.len = strlen(out.val.string.val);
                                297                 :                :                 }
                                298                 :                :                 else
                                299                 :                :                 {
                                300                 :                :                     /*
                                301                 :                :                      * XXX It might be nice if we could include the Perl type
                                302                 :                :                      * in the error message.
                                303                 :                :                      */
 3134 peter_e@gmx.net           304         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                305                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                306                 :                :                              errmsg("cannot transform this Perl type to jsonb")));
                                307                 :                :                 }
                                308                 :                :         }
                                309                 :                :     }
                                310                 :                : 
  356 tgl@sss.pgh.pa.us         311         [ +  + ]:CBC         101 :     if (jsonb_state->parseState)
                                312                 :                :     {
                                313                 :                :         /* We're in an array or object, so push value as element or field. */
                                314         [ +  + ]:             79 :         pushJsonbValue(jsonb_state, is_elem ? WJB_ELEM : WJB_VALUE, &out);
                                315                 :                :     }
                                316                 :                :     else
                                317                 :                :     {
                                318                 :                :         /*
                                319                 :                :          * We are at top level, so it's a raw scalar.  If we just shove the
                                320                 :                :          * scalar value into jsonb_state->result, JsonbValueToJsonb will take
                                321                 :                :          * care of wrapping it into a dummy array.
                                322                 :                :          */
                                323                 :             22 :         jsonb_state->result = palloc_object(JsonbValue);
                                324                 :             22 :         memcpy(jsonb_state->result, &out, sizeof(JsonbValue));
                                325                 :                :     }
                                326                 :                : }
                                327                 :                : 
                                328                 :                : 
 3161 peter_e@gmx.net           329                 :              4 : PG_FUNCTION_INFO_V1(jsonb_to_plperl);
                                330                 :                : 
                                331                 :                : Datum
                                332                 :             51 : jsonb_to_plperl(PG_FUNCTION_ARGS)
                                333                 :                : {
                                334                 :             51 :     dTHX;
                                335                 :             51 :     Jsonb      *in = PG_GETARG_JSONB_P(0);
                                336                 :             51 :     SV         *sv = Jsonb_to_SV(&in->root);
                                337                 :                : 
 3085 tgl@sss.pgh.pa.us         338                 :             51 :     return PointerGetDatum(sv);
                                339                 :                : }
                                340                 :                : 
                                341                 :                : 
 3161 peter_e@gmx.net           342                 :              4 : PG_FUNCTION_INFO_V1(plperl_to_jsonb);
                                343                 :                : 
                                344                 :                : Datum
                                345                 :             65 : plperl_to_jsonb(PG_FUNCTION_ARGS)
                                346                 :                : {
                                347                 :             65 :     dTHX;
                                348                 :             65 :     SV         *in = (SV *) PG_GETARG_POINTER(0);
  356 tgl@sss.pgh.pa.us         349                 :             65 :     JsonbInState jsonb_state = {0};
                                350                 :                : 
                                351                 :             65 :     SV_to_JsonbValue(in, &jsonb_state, true);
                                352                 :             64 :     PG_RETURN_JSONB_P(JsonbValueToJsonb(jsonb_state.result));
                                353                 :                : }
        

Generated by: LCOV version 2.0-1