LCOV - code coverage report
Current view: top level - src/backend/utils/adt - jsonbsubs.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 87.1 % 140 122
Test Date: 2026-01-26 10:56:24 Functions: 85.7 % 7 6
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 62.5 % 88 55

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * jsonbsubs.c
       4                 :             :  *        Subscripting support functions for jsonb.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *        src/backend/utils/adt/jsonbsubs.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include "executor/execExpr.h"
      18                 :             : #include "nodes/nodeFuncs.h"
      19                 :             : #include "nodes/subscripting.h"
      20                 :             : #include "parser/parse_coerce.h"
      21                 :             : #include "parser/parse_expr.h"
      22                 :             : #include "utils/builtins.h"
      23                 :             : #include "utils/jsonb.h"
      24                 :             : 
      25                 :             : 
      26                 :             : /* SubscriptingRefState.workspace for jsonb subscripting execution */
      27                 :             : typedef struct JsonbSubWorkspace
      28                 :             : {
      29                 :             :         bool            expectArray;    /* jsonb root is expected to be an array */
      30                 :             :         Oid                *indexOid;           /* OID of coerced subscript expression, could
      31                 :             :                                                                  * be only integer or text */
      32                 :             :         Datum      *index;                      /* Subscript values in Datum format */
      33                 :             : } JsonbSubWorkspace;
      34                 :             : 
      35                 :             : 
      36                 :             : /*
      37                 :             :  * Finish parse analysis of a SubscriptingRef expression for a jsonb.
      38                 :             :  *
      39                 :             :  * Transform the subscript expressions, coerce them to text,
      40                 :             :  * and determine the result type of the SubscriptingRef node.
      41                 :             :  */
      42                 :             : static void
      43                 :          70 : jsonb_subscript_transform(SubscriptingRef *sbsref,
      44                 :             :                                                   List *indirection,
      45                 :             :                                                   ParseState *pstate,
      46                 :             :                                                   bool isSlice,
      47                 :             :                                                   bool isAssignment)
      48                 :             : {
      49                 :          70 :         List       *upperIndexpr = NIL;
      50                 :          70 :         ListCell   *idx;
      51                 :             : 
      52                 :             :         /*
      53                 :             :          * Transform and convert the subscript expressions. Jsonb subscripting
      54                 :             :          * does not support slices, look only at the upper index.
      55                 :             :          */
      56   [ +  -  +  +  :         189 :         foreach(idx, indirection)
                   +  + ]
      57                 :             :         {
      58                 :         125 :                 A_Indices  *ai = lfirst_node(A_Indices, idx);
      59                 :         125 :                 Node       *subExpr;
      60                 :             : 
      61         [ +  + ]:         125 :                 if (isSlice)
      62                 :             :                 {
      63         [ +  + ]:           5 :                         Node       *expr = ai->uidx ? ai->uidx : ai->lidx;
      64                 :             : 
      65   [ -  +  +  - ]:           5 :                         ereport(ERROR,
      66                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
      67                 :             :                                          errmsg("jsonb subscript does not support slices"),
      68                 :             :                                          parser_errposition(pstate, exprLocation(expr))));
      69                 :           0 :                 }
      70                 :             : 
      71         [ +  - ]:         120 :                 if (ai->uidx)
      72                 :             :                 {
      73                 :         120 :                         Oid                     subExprType = InvalidOid,
      74                 :         120 :                                                 targetType = UNKNOWNOID;
      75                 :             : 
      76                 :         120 :                         subExpr = transformExpr(pstate, ai->uidx, pstate->p_expr_kind);
      77                 :         120 :                         subExprType = exprType(subExpr);
      78                 :             : 
      79         [ +  + ]:         120 :                         if (subExprType != UNKNOWNOID)
      80                 :             :                         {
      81                 :          52 :                                 Oid                     targets[2] = {INT4OID, TEXTOID};
      82                 :             : 
      83                 :             :                                 /*
      84                 :             :                                  * Jsonb can handle multiple subscript types, but cases when a
      85                 :             :                                  * subscript could be coerced to multiple target types must be
      86                 :             :                                  * avoided, similar to overloaded functions. It could be
      87                 :             :                                  * possibly extend with jsonpath in the future.
      88                 :             :                                  */
      89         [ +  + ]:         156 :                                 for (int i = 0; i < 2; i++)
      90                 :             :                                 {
      91         [ +  + ]:         104 :                                         if (can_coerce_type(1, &subExprType, &targets[i], COERCION_IMPLICIT))
      92                 :             :                                         {
      93                 :             :                                                 /*
      94                 :             :                                                  * One type has already succeeded, it means there are
      95                 :             :                                                  * two coercion targets possible, failure.
      96                 :             :                                                  */
      97         [ -  + ]:          51 :                                                 if (targetType != UNKNOWNOID)
      98   [ #  #  #  # ]:           0 :                                                         ereport(ERROR,
      99                 :             :                                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     100                 :             :                                                                          errmsg("subscript type %s is not supported", format_type_be(subExprType)),
     101                 :             :                                                                          errhint("jsonb subscript must be coercible to only one type, integer or text."),
     102                 :             :                                                                          parser_errposition(pstate, exprLocation(subExpr))));
     103                 :             : 
     104                 :          51 :                                                 targetType = targets[i];
     105                 :          51 :                                         }
     106                 :         104 :                                 }
     107                 :             : 
     108                 :             :                                 /*
     109                 :             :                                  * No suitable types were found, failure.
     110                 :             :                                  */
     111         [ +  + ]:          52 :                                 if (targetType == UNKNOWNOID)
     112   [ -  +  +  - ]:           1 :                                         ereport(ERROR,
     113                 :             :                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     114                 :             :                                                          errmsg("subscript type %s is not supported", format_type_be(subExprType)),
     115                 :             :                                                          errhint("jsonb subscript must be coercible to either integer or text."),
     116                 :             :                                                          parser_errposition(pstate, exprLocation(subExpr))));
     117                 :          51 :                         }
     118                 :             :                         else
     119                 :          68 :                                 targetType = TEXTOID;
     120                 :             : 
     121                 :             :                         /*
     122                 :             :                          * We known from can_coerce_type that coercion will succeed, so
     123                 :             :                          * coerce_type could be used. Note the implicit coercion context,
     124                 :             :                          * which is required to handle subscripts of different types,
     125                 :             :                          * similar to overloaded functions.
     126                 :             :                          */
     127                 :         238 :                         subExpr = coerce_type(pstate,
     128                 :         119 :                                                                   subExpr, subExprType,
     129                 :         119 :                                                                   targetType, -1,
     130                 :             :                                                                   COERCION_IMPLICIT,
     131                 :             :                                                                   COERCE_IMPLICIT_CAST,
     132                 :             :                                                                   -1);
     133         [ +  - ]:         119 :                         if (subExpr == NULL)
     134   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     135                 :             :                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
     136                 :             :                                                  errmsg("jsonb subscript must have text type"),
     137                 :             :                                                  parser_errposition(pstate, exprLocation(subExpr))));
     138                 :         119 :                 }
     139                 :             :                 else
     140                 :             :                 {
     141                 :             :                         /*
     142                 :             :                          * Slice with omitted upper bound. Should not happen as we already
     143                 :             :                          * errored out on slice earlier, but handle this just in case.
     144                 :             :                          */
     145   [ #  #  #  # ]:           0 :                         Assert(isSlice && ai->is_slice);
     146   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     147                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     148                 :             :                                          errmsg("jsonb subscript does not support slices"),
     149                 :             :                                          parser_errposition(pstate, exprLocation(ai->uidx))));
     150                 :             :                 }
     151                 :             : 
     152                 :         119 :                 upperIndexpr = lappend(upperIndexpr, subExpr);
     153                 :         119 :         }
     154                 :             : 
     155                 :             :         /* store the transformed lists into the SubscriptingRef node */
     156                 :          64 :         sbsref->refupperindexpr = upperIndexpr;
     157                 :          64 :         sbsref->reflowerindexpr = NIL;
     158                 :             : 
     159                 :             :         /* Determine the result type of the subscripting operation; always jsonb */
     160                 :          64 :         sbsref->refrestype = JSONBOID;
     161                 :          64 :         sbsref->reftypmod = -1;
     162                 :          64 : }
     163                 :             : 
     164                 :             : /*
     165                 :             :  * During execution, process the subscripts in a SubscriptingRef expression.
     166                 :             :  *
     167                 :             :  * The subscript expressions are already evaluated in Datum form in the
     168                 :             :  * SubscriptingRefState's arrays.  Check and convert them as necessary.
     169                 :             :  *
     170                 :             :  * If any subscript is NULL, we throw error in assignment cases, or in fetch
     171                 :             :  * cases set result to NULL and return false (instructing caller to skip the
     172                 :             :  * rest of the SubscriptingRef sequence).
     173                 :             :  */
     174                 :             : static bool
     175                 :          76 : jsonb_subscript_check_subscripts(ExprState *state,
     176                 :             :                                                                  ExprEvalStep *op,
     177                 :             :                                                                  ExprContext *econtext)
     178                 :             : {
     179                 :          76 :         SubscriptingRefState *sbsrefstate = op->d.sbsref_subscript.state;
     180                 :          76 :         JsonbSubWorkspace *workspace = (JsonbSubWorkspace *) sbsrefstate->workspace;
     181                 :             : 
     182                 :             :         /*
     183                 :             :          * In case if the first subscript is an integer, the source jsonb is
     184                 :             :          * expected to be an array. This information is not used directly, all
     185                 :             :          * such cases are handled within corresponding jsonb assign functions. But
     186                 :             :          * if the source jsonb is NULL the expected type will be used to construct
     187                 :             :          * an empty source.
     188                 :             :          */
     189   [ +  -  +  - ]:          76 :         if (sbsrefstate->numupper > 0 && sbsrefstate->upperprovided[0] &&
     190   [ +  +  +  + ]:          76 :                 !sbsrefstate->upperindexnull[0] && workspace->indexOid[0] == INT4OID)
     191                 :          22 :                 workspace->expectArray = true;
     192                 :             : 
     193                 :             :         /* Process upper subscripts */
     194   [ +  +  +  + ]:         206 :         for (int i = 0; i < sbsrefstate->numupper; i++)
     195                 :             :         {
     196         [ -  + ]:         131 :                 if (sbsrefstate->upperprovided[i])
     197                 :             :                 {
     198                 :             :                         /* If any index expr yields NULL, result is NULL or error */
     199         [ +  + ]:         131 :                         if (sbsrefstate->upperindexnull[i])
     200                 :             :                         {
     201         [ +  + ]:           3 :                                 if (sbsrefstate->isassignment)
     202   [ +  -  +  - ]:           1 :                                         ereport(ERROR,
     203                 :             :                                                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
     204                 :             :                                                          errmsg("jsonb subscript in assignment must not be null")));
     205                 :           2 :                                 *op->resnull = true;
     206                 :           2 :                                 return false;
     207                 :             :                         }
     208                 :             : 
     209                 :             :                         /*
     210                 :             :                          * For jsonb fetch and assign functions we need to provide path in
     211                 :             :                          * text format. Convert if it's not already text.
     212                 :             :                          */
     213         [ +  + ]:         128 :                         if (workspace->indexOid[i] == INT4OID)
     214                 :             :                         {
     215                 :          50 :                                 Datum           datum = sbsrefstate->upperindex[i];
     216                 :          50 :                                 char       *cs = DatumGetCString(DirectFunctionCall1(int4out, datum));
     217                 :             : 
     218                 :          50 :                                 workspace->index[i] = CStringGetTextDatum(cs);
     219                 :          50 :                         }
     220                 :             :                         else
     221                 :          78 :                                 workspace->index[i] = sbsrefstate->upperindex[i];
     222                 :         128 :                 }
     223                 :         128 :         }
     224                 :             : 
     225                 :          73 :         return true;
     226                 :          75 : }
     227                 :             : 
     228                 :             : /*
     229                 :             :  * Evaluate SubscriptingRef fetch for a jsonb element.
     230                 :             :  *
     231                 :             :  * Source container is in step's result variable (it's known not NULL, since
     232                 :             :  * we set fetch_strict to true).
     233                 :             :  */
     234                 :             : static void
     235                 :          32 : jsonb_subscript_fetch(ExprState *state,
     236                 :             :                                           ExprEvalStep *op,
     237                 :             :                                           ExprContext *econtext)
     238                 :             : {
     239                 :          32 :         SubscriptingRefState *sbsrefstate = op->d.sbsref.state;
     240                 :          32 :         JsonbSubWorkspace *workspace = (JsonbSubWorkspace *) sbsrefstate->workspace;
     241                 :          32 :         Jsonb      *jsonbSource;
     242                 :             : 
     243                 :             :         /* Should not get here if source jsonb (or any subscript) is null */
     244         [ +  - ]:          32 :         Assert(!(*op->resnull));
     245                 :             : 
     246                 :          32 :         jsonbSource = DatumGetJsonbP(*op->resvalue);
     247                 :          64 :         *op->resvalue = jsonb_get_element(jsonbSource,
     248                 :          32 :                                                                           workspace->index,
     249                 :          32 :                                                                           sbsrefstate->numupper,
     250                 :          32 :                                                                           op->resnull,
     251                 :             :                                                                           false);
     252                 :          32 : }
     253                 :             : 
     254                 :             : /*
     255                 :             :  * Evaluate SubscriptingRef assignment for a jsonb element assignment.
     256                 :             :  *
     257                 :             :  * Input container (possibly null) is in result area, replacement value is in
     258                 :             :  * SubscriptingRefState's replacevalue/replacenull.
     259                 :             :  */
     260                 :             : static void
     261                 :          41 : jsonb_subscript_assign(ExprState *state,
     262                 :             :                                            ExprEvalStep *op,
     263                 :             :                                            ExprContext *econtext)
     264                 :             : {
     265                 :          41 :         SubscriptingRefState *sbsrefstate = op->d.sbsref.state;
     266                 :          41 :         JsonbSubWorkspace *workspace = (JsonbSubWorkspace *) sbsrefstate->workspace;
     267                 :          41 :         Jsonb      *jsonbSource;
     268                 :          41 :         JsonbValue      replacevalue;
     269                 :             : 
     270         [ +  + ]:          41 :         if (sbsrefstate->replacenull)
     271                 :           2 :                 replacevalue.type = jbvNull;
     272                 :             :         else
     273                 :          39 :                 JsonbToJsonbValue(DatumGetJsonbP(sbsrefstate->replacevalue),
     274                 :             :                                                   &replacevalue);
     275                 :             : 
     276                 :             :         /*
     277                 :             :          * In case if the input container is null, set up an empty jsonb and
     278                 :             :          * proceed with the assignment.
     279                 :             :          */
     280         [ +  + ]:          41 :         if (*op->resnull)
     281                 :             :         {
     282                 :           2 :                 JsonbValue      newSource;
     283                 :             : 
     284                 :             :                 /*
     285                 :             :                  * To avoid any surprising results, set up an empty jsonb array in
     286                 :             :                  * case of an array is expected (i.e. the first subscript is integer),
     287                 :             :                  * otherwise jsonb object.
     288                 :             :                  */
     289         [ +  + ]:           2 :                 if (workspace->expectArray)
     290                 :             :                 {
     291                 :           1 :                         newSource.type = jbvArray;
     292                 :           1 :                         newSource.val.array.nElems = 0;
     293                 :           1 :                         newSource.val.array.rawScalar = false;
     294                 :           1 :                 }
     295                 :             :                 else
     296                 :             :                 {
     297                 :           1 :                         newSource.type = jbvObject;
     298                 :           1 :                         newSource.val.object.nPairs = 0;
     299                 :             :                 }
     300                 :             : 
     301                 :           2 :                 jsonbSource = JsonbValueToJsonb(&newSource);
     302                 :           2 :                 *op->resnull = false;
     303                 :           2 :         }
     304                 :             :         else
     305                 :          39 :                 jsonbSource = DatumGetJsonbP(*op->resvalue);
     306                 :             : 
     307                 :          82 :         *op->resvalue = jsonb_set_element(jsonbSource,
     308                 :          41 :                                                                           workspace->index,
     309                 :          41 :                                                                           sbsrefstate->numupper,
     310                 :             :                                                                           &replacevalue);
     311                 :             :         /* The result is never NULL, so no need to change *op->resnull */
     312                 :          41 : }
     313                 :             : 
     314                 :             : /*
     315                 :             :  * Compute old jsonb element value for a SubscriptingRef assignment
     316                 :             :  * expression.  Will only be called if the new-value subexpression
     317                 :             :  * contains SubscriptingRef or FieldStore.  This is the same as the
     318                 :             :  * regular fetch case, except that we have to handle a null jsonb,
     319                 :             :  * and the value should be stored into the SubscriptingRefState's
     320                 :             :  * prevvalue/prevnull fields.
     321                 :             :  */
     322                 :             : static void
     323                 :           0 : jsonb_subscript_fetch_old(ExprState *state,
     324                 :             :                                                   ExprEvalStep *op,
     325                 :             :                                                   ExprContext *econtext)
     326                 :             : {
     327                 :           0 :         SubscriptingRefState *sbsrefstate = op->d.sbsref.state;
     328                 :             : 
     329         [ #  # ]:           0 :         if (*op->resnull)
     330                 :             :         {
     331                 :             :                 /* whole jsonb is null, so any element is too */
     332                 :           0 :                 sbsrefstate->prevvalue = (Datum) 0;
     333                 :           0 :                 sbsrefstate->prevnull = true;
     334                 :           0 :         }
     335                 :             :         else
     336                 :             :         {
     337                 :           0 :                 Jsonb      *jsonbSource = DatumGetJsonbP(*op->resvalue);
     338                 :             : 
     339                 :           0 :                 sbsrefstate->prevvalue = jsonb_get_element(jsonbSource,
     340                 :           0 :                                                                                                    sbsrefstate->upperindex,
     341                 :           0 :                                                                                                    sbsrefstate->numupper,
     342                 :           0 :                                                                                                    &sbsrefstate->prevnull,
     343                 :             :                                                                                                    false);
     344                 :           0 :         }
     345                 :           0 : }
     346                 :             : 
     347                 :             : /*
     348                 :             :  * Set up execution state for a jsonb subscript operation. Opposite to the
     349                 :             :  * arrays subscription, there is no limit for number of subscripts as jsonb
     350                 :             :  * type itself doesn't have nesting limits.
     351                 :             :  */
     352                 :             : static void
     353                 :          64 : jsonb_exec_setup(const SubscriptingRef *sbsref,
     354                 :             :                                  SubscriptingRefState *sbsrefstate,
     355                 :             :                                  SubscriptExecSteps *methods)
     356                 :             : {
     357                 :          64 :         JsonbSubWorkspace *workspace;
     358                 :          64 :         ListCell   *lc;
     359                 :          64 :         int                     nupper = sbsref->refupperindexpr->length;
     360                 :          64 :         char       *ptr;
     361                 :             : 
     362                 :             :         /* Allocate type-specific workspace with space for per-subscript data */
     363                 :          64 :         workspace = palloc0(MAXALIGN(sizeof(JsonbSubWorkspace)) +
     364                 :          64 :                                                 nupper * (sizeof(Datum) + sizeof(Oid)));
     365                 :          64 :         workspace->expectArray = false;
     366                 :          64 :         ptr = ((char *) workspace) + MAXALIGN(sizeof(JsonbSubWorkspace));
     367                 :             : 
     368                 :             :         /*
     369                 :             :          * This coding assumes sizeof(Datum) >= sizeof(Oid), else we might
     370                 :             :          * misalign the indexOid pointer
     371                 :             :          */
     372                 :          64 :         workspace->index = (Datum *) ptr;
     373                 :          64 :         ptr += nupper * sizeof(Datum);
     374                 :          64 :         workspace->indexOid = (Oid *) ptr;
     375                 :             : 
     376                 :          64 :         sbsrefstate->workspace = workspace;
     377                 :             : 
     378                 :             :         /* Collect subscript data types necessary at execution time */
     379   [ +  -  +  +  :         183 :         foreach(lc, sbsref->refupperindexpr)
                   +  + ]
     380                 :             :         {
     381                 :         119 :                 Node       *expr = lfirst(lc);
     382                 :         119 :                 int                     i = foreach_current_index(lc);
     383                 :             : 
     384                 :         119 :                 workspace->indexOid[i] = exprType(expr);
     385                 :         119 :         }
     386                 :             : 
     387                 :             :         /*
     388                 :             :          * Pass back pointers to appropriate step execution functions.
     389                 :             :          */
     390                 :          64 :         methods->sbs_check_subscripts = jsonb_subscript_check_subscripts;
     391                 :          64 :         methods->sbs_fetch = jsonb_subscript_fetch;
     392                 :          64 :         methods->sbs_assign = jsonb_subscript_assign;
     393                 :          64 :         methods->sbs_fetch_old = jsonb_subscript_fetch_old;
     394                 :          64 : }
     395                 :             : 
     396                 :             : /*
     397                 :             :  * jsonb_subscript_handler
     398                 :             :  *              Subscripting handler for jsonb.
     399                 :             :  *
     400                 :             :  */
     401                 :             : Datum
     402                 :         134 : jsonb_subscript_handler(PG_FUNCTION_ARGS)
     403                 :             : {
     404                 :             :         static const SubscriptRoutines sbsroutines = {
     405                 :             :                 .transform = jsonb_subscript_transform,
     406                 :             :                 .exec_setup = jsonb_exec_setup,
     407                 :             :                 .fetch_strict = true,   /* fetch returns NULL for NULL inputs */
     408                 :             :                 .fetch_leakproof = true,        /* fetch returns NULL for bad subscript */
     409                 :             :                 .store_leakproof = false        /* ... but assignment throws error */
     410                 :             :         };
     411                 :             : 
     412                 :         134 :         PG_RETURN_POINTER(&sbsroutines);
     413                 :             : }
        

Generated by: LCOV version 2.3.2-1