LCOV - code coverage report
Current view: top level - src/backend/parser - parse_jsontable.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 99.1 % 228 226
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 10 10
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 79.5 % 127 101

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * parse_jsontable.c
       4                 :             :  *        parsing of JSON_TABLE
       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/parser/parse_jsontable.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : 
      16                 :             : #include "postgres.h"
      17                 :             : 
      18                 :             : #include "catalog/pg_type.h"
      19                 :             : #include "nodes/makefuncs.h"
      20                 :             : #include "nodes/nodeFuncs.h"
      21                 :             : #include "optimizer/optimizer.h"
      22                 :             : #include "parser/parse_clause.h"
      23                 :             : #include "parser/parse_collate.h"
      24                 :             : #include "parser/parse_expr.h"
      25                 :             : #include "parser/parse_relation.h"
      26                 :             : #include "parser/parse_type.h"
      27                 :             : #include "utils/fmgrprotos.h"
      28                 :             : #include "utils/json.h"
      29                 :             : #include "utils/lsyscache.h"
      30                 :             : 
      31                 :             : /* Context for transformJsonTableColumns() */
      32                 :             : typedef struct JsonTableParseContext
      33                 :             : {
      34                 :             :         ParseState *pstate;
      35                 :             :         JsonTable  *jt;
      36                 :             :         TableFunc  *tf;
      37                 :             :         List       *pathNames;          /* list of all path and columns names */
      38                 :             :         int                     pathNameId;             /* path name id counter */
      39                 :             : } JsonTableParseContext;
      40                 :             : 
      41                 :             : static JsonTablePlan *transformJsonTableColumns(JsonTableParseContext *cxt,
      42                 :             :                                                                                                 List *columns,
      43                 :             :                                                                                                 List *passingArgs,
      44                 :             :                                                                                                 JsonTablePathSpec *pathspec);
      45                 :             : static JsonTablePlan *transformJsonTableNestedColumns(JsonTableParseContext *cxt,
      46                 :             :                                                                                                           List *passingArgs,
      47                 :             :                                                                                                           List *columns);
      48                 :             : static JsonFuncExpr *transformJsonTableColumn(JsonTableColumn *jtc,
      49                 :             :                                                                                           Node *contextItemExpr,
      50                 :             :                                                                                           List *passingArgs);
      51                 :             : static bool isCompositeType(Oid typid);
      52                 :             : static JsonTablePlan *makeJsonTablePathScan(JsonTablePathSpec *pathspec,
      53                 :             :                                                                                         bool errorOnError,
      54                 :             :                                                                                         int colMin, int colMax,
      55                 :             :                                                                                         JsonTablePlan *childplan);
      56                 :             : static void CheckDuplicateColumnOrPathNames(JsonTableParseContext *cxt,
      57                 :             :                                                                                         List *columns);
      58                 :             : static bool LookupPathOrColumnName(JsonTableParseContext *cxt, char *name);
      59                 :             : static char *generateJsonTablePathName(JsonTableParseContext *cxt);
      60                 :             : static JsonTablePlan *makeJsonTableSiblingJoin(JsonTablePlan *lplan,
      61                 :             :                                                                                            JsonTablePlan *rplan);
      62                 :             : 
      63                 :             : /*
      64                 :             :  * transformJsonTable -
      65                 :             :  *                      Transform a raw JsonTable into TableFunc
      66                 :             :  *
      67                 :             :  * Mainly, this transforms the JSON_TABLE() document-generating expression
      68                 :             :  * (jt->context_item) and the column-generating expressions (jt->columns) to
      69                 :             :  * populate TableFunc.docexpr and TableFunc.colvalexprs, respectively. Also,
      70                 :             :  * the PASSING values (jt->passing) are transformed and added into
      71                 :             :  * TableFunc.passingvalexprs.
      72                 :             :  */
      73                 :             : ParseNamespaceItem *
      74                 :          81 : transformJsonTable(ParseState *pstate, JsonTable *jt)
      75                 :             : {
      76                 :          81 :         TableFunc  *tf;
      77                 :          81 :         JsonFuncExpr *jfe;
      78                 :          81 :         JsonExpr   *je;
      79                 :          81 :         JsonTablePathSpec *rootPathSpec = jt->pathspec;
      80                 :          81 :         bool            is_lateral;
      81                 :          81 :         JsonTableParseContext cxt = {pstate};
      82                 :             : 
      83         [ +  - ]:          81 :         Assert(IsA(rootPathSpec->string, A_Const) &&
      84                 :             :                    castNode(A_Const, rootPathSpec->string)->val.node.type == T_String);
      85                 :             : 
      86         [ +  + ]:          81 :         if (jt->on_error &&
      87         [ +  + ]:          10 :                 jt->on_error->btype != JSON_BEHAVIOR_ERROR &&
      88   [ +  -  +  + ]:           6 :                 jt->on_error->btype != JSON_BEHAVIOR_EMPTY &&
      89                 :           6 :                 jt->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY)
      90   [ +  -  +  - ]:           3 :                 ereport(ERROR,
      91                 :             :                                 errcode(ERRCODE_SYNTAX_ERROR),
      92                 :             :                                 errmsg("invalid %s behavior", "ON ERROR"),
      93                 :             :                                 errdetail("Only EMPTY [ ARRAY ] or ERROR is allowed in the top-level ON ERROR clause."),
      94                 :             :                                 parser_errposition(pstate, jt->on_error->location));
      95                 :             : 
      96                 :          78 :         cxt.pathNameId = 0;
      97         [ +  + ]:          78 :         if (rootPathSpec->name == NULL)
      98                 :          74 :                 rootPathSpec->name = generateJsonTablePathName(&cxt);
      99                 :          78 :         cxt.pathNames = list_make1(rootPathSpec->name);
     100                 :          78 :         CheckDuplicateColumnOrPathNames(&cxt, jt->columns);
     101                 :             : 
     102                 :             :         /*
     103                 :             :          * We make lateral_only names of this level visible, whether or not the
     104                 :             :          * RangeTableFunc is explicitly marked LATERAL.  This is needed for SQL
     105                 :             :          * spec compliance and seems useful on convenience grounds for all
     106                 :             :          * functions in FROM.
     107                 :             :          *
     108                 :             :          * (LATERAL can't nest within a single pstate level, so we don't need
     109                 :             :          * save/restore logic here.)
     110                 :             :          */
     111         [ +  - ]:          78 :         Assert(!pstate->p_lateral_active);
     112                 :          78 :         pstate->p_lateral_active = true;
     113                 :             : 
     114                 :          78 :         tf = makeNode(TableFunc);
     115                 :          78 :         tf->functype = TFT_JSON_TABLE;
     116                 :             : 
     117                 :             :         /*
     118                 :             :          * Transform JsonFuncExpr representing the top JSON_TABLE context_item and
     119                 :             :          * pathspec into a dummy JSON_TABLE_OP JsonExpr.
     120                 :             :          */
     121                 :          78 :         jfe = makeNode(JsonFuncExpr);
     122                 :          78 :         jfe->op = JSON_TABLE_OP;
     123                 :          78 :         jfe->context_item = jt->context_item;
     124                 :          78 :         jfe->pathspec = (Node *) rootPathSpec->string;
     125                 :          78 :         jfe->passing = jt->passing;
     126                 :          78 :         jfe->on_empty = NULL;
     127                 :          78 :         jfe->on_error = jt->on_error;
     128                 :          78 :         jfe->location = jt->location;
     129                 :          78 :         tf->docexpr = transformExpr(pstate, (Node *) jfe, EXPR_KIND_FROM_FUNCTION);
     130                 :             : 
     131                 :             :         /*
     132                 :             :          * Create a JsonTablePlan that will generate row pattern that becomes
     133                 :             :          * source data for JSON path expressions in jt->columns.  This also adds
     134                 :             :          * the columns' transformed JsonExpr nodes into tf->colvalexprs.
     135                 :             :          */
     136                 :          78 :         cxt.jt = jt;
     137                 :          78 :         cxt.tf = tf;
     138                 :         156 :         tf->plan = (Node *) transformJsonTableColumns(&cxt, jt->columns,
     139                 :          78 :                                                                                                   jt->passing,
     140                 :          78 :                                                                                                   rootPathSpec);
     141                 :             : 
     142                 :             :         /*
     143                 :             :          * Copy the transformed PASSING arguments into the TableFunc node, because
     144                 :             :          * they are evaluated separately from the JsonExpr that we just put in
     145                 :             :          * TableFunc.docexpr.  JsonExpr.passing_values is still kept around for
     146                 :             :          * get_json_table().
     147                 :             :          */
     148                 :          78 :         je = (JsonExpr *) tf->docexpr;
     149                 :          78 :         tf->passingvalexprs = copyObject(je->passing_values);
     150                 :             : 
     151                 :          78 :         tf->ordinalitycol = -1;              /* undefine ordinality column number */
     152                 :          78 :         tf->location = jt->location;
     153                 :             : 
     154                 :          78 :         pstate->p_lateral_active = false;
     155                 :             : 
     156                 :             :         /*
     157                 :             :          * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
     158                 :             :          * there are any lateral cross-references in it.
     159                 :             :          */
     160         [ +  + ]:          78 :         is_lateral = jt->lateral || contain_vars_of_level((Node *) tf, 0);
     161                 :             : 
     162                 :         234 :         return addRangeTableEntryForTableFunc(pstate,
     163                 :          78 :                                                                                   tf, jt->alias, is_lateral, true);
     164                 :          78 : }
     165                 :             : 
     166                 :             : /*
     167                 :             :  * Check if a column / path name is duplicated in the given shared list of
     168                 :             :  * names.
     169                 :             :  */
     170                 :             : static void
     171                 :         127 : CheckDuplicateColumnOrPathNames(JsonTableParseContext *cxt,
     172                 :             :                                                                 List *columns)
     173                 :             : {
     174                 :         127 :         ListCell   *lc1;
     175                 :             : 
     176   [ +  +  +  +  :         337 :         foreach(lc1, columns)
                   +  + ]
     177                 :             :         {
     178                 :         215 :                 JsonTableColumn *jtc = castNode(JsonTableColumn, lfirst(lc1));
     179                 :             : 
     180         [ +  + ]:         215 :                 if (jtc->coltype == JTC_NESTED)
     181                 :             :                 {
     182         [ +  + ]:          47 :                         if (jtc->pathspec->name)
     183                 :             :                         {
     184         [ +  + ]:          23 :                                 if (LookupPathOrColumnName(cxt, jtc->pathspec->name))
     185   [ +  -  +  - ]:           3 :                                         ereport(ERROR,
     186                 :             :                                                         errcode(ERRCODE_DUPLICATE_ALIAS),
     187                 :             :                                                         errmsg("duplicate JSON_TABLE column or path name: %s",
     188                 :             :                                                                    jtc->pathspec->name),
     189                 :             :                                                         parser_errposition(cxt->pstate,
     190                 :             :                                                                                            jtc->pathspec->name_location));
     191                 :          20 :                                 cxt->pathNames = lappend(cxt->pathNames, jtc->pathspec->name);
     192                 :          20 :                         }
     193                 :             : 
     194                 :          44 :                         CheckDuplicateColumnOrPathNames(cxt, jtc->columns);
     195                 :          44 :                 }
     196                 :             :                 else
     197                 :             :                 {
     198         [ +  + ]:         168 :                         if (LookupPathOrColumnName(cxt, jtc->name))
     199   [ +  -  +  - ]:           2 :                                 ereport(ERROR,
     200                 :             :                                                 errcode(ERRCODE_DUPLICATE_ALIAS),
     201                 :             :                                                 errmsg("duplicate JSON_TABLE column or path name: %s",
     202                 :             :                                                            jtc->name),
     203                 :             :                                                 parser_errposition(cxt->pstate, jtc->location));
     204                 :         166 :                         cxt->pathNames = lappend(cxt->pathNames, jtc->name);
     205                 :             :                 }
     206                 :         210 :         }
     207                 :         122 : }
     208                 :             : 
     209                 :             : /*
     210                 :             :  * Lookup a column/path name in the given name list, returning true if already
     211                 :             :  * there.
     212                 :             :  */
     213                 :             : static bool
     214                 :         191 : LookupPathOrColumnName(JsonTableParseContext *cxt, char *name)
     215                 :             : {
     216                 :         191 :         ListCell   *lc;
     217                 :             : 
     218   [ +  -  +  +  :         742 :         foreach(lc, cxt->pathNames)
             +  +  +  + ]
     219                 :             :         {
     220         [ +  + ]:         551 :                 if (strcmp(name, (const char *) lfirst(lc)) == 0)
     221                 :           5 :                         return true;
     222                 :         546 :         }
     223                 :             : 
     224                 :         186 :         return false;
     225                 :         191 : }
     226                 :             : 
     227                 :             : /* Generate a new unique JSON_TABLE path name. */
     228                 :             : static char *
     229                 :          97 : generateJsonTablePathName(JsonTableParseContext *cxt)
     230                 :             : {
     231                 :          97 :         char            namebuf[32];
     232                 :          97 :         char       *name = namebuf;
     233                 :             : 
     234                 :         194 :         snprintf(namebuf, sizeof(namebuf), "json_table_path_%d",
     235                 :          97 :                          cxt->pathNameId++);
     236                 :             : 
     237                 :          97 :         name = pstrdup(name);
     238                 :          97 :         cxt->pathNames = lappend(cxt->pathNames, name);
     239                 :             : 
     240                 :         194 :         return name;
     241                 :          97 : }
     242                 :             : 
     243                 :             : /*
     244                 :             :  * Create a JsonTablePlan that will supply the source row for 'columns'
     245                 :             :  * using 'pathspec' and append the columns' transformed JsonExpr nodes and
     246                 :             :  * their type/collation information to cxt->tf.
     247                 :             :  */
     248                 :             : static JsonTablePlan *
     249                 :         116 : transformJsonTableColumns(JsonTableParseContext *cxt, List *columns,
     250                 :             :                                                   List *passingArgs,
     251                 :             :                                                   JsonTablePathSpec *pathspec)
     252                 :             : {
     253                 :         116 :         ParseState *pstate = cxt->pstate;
     254                 :         116 :         JsonTable  *jt = cxt->jt;
     255                 :         116 :         TableFunc  *tf = cxt->tf;
     256                 :         116 :         ListCell   *col;
     257                 :         116 :         bool            ordinality_found = false;
     258         [ +  + ]:         123 :         bool            errorOnError = jt->on_error &&
     259                 :           7 :                 jt->on_error->btype == JSON_BEHAVIOR_ERROR;
     260                 :         116 :         Oid                     contextItemTypid = exprType(tf->docexpr);
     261                 :         116 :         int                     colMin,
     262                 :             :                                 colMax;
     263                 :         116 :         JsonTablePlan *childplan;
     264                 :             : 
     265                 :             :         /* Start of column range */
     266                 :         116 :         colMin = list_length(tf->colvalexprs);
     267                 :             : 
     268   [ +  -  +  +  :         318 :         foreach(col, columns)
                   +  + ]
     269                 :             :         {
     270                 :         203 :                 JsonTableColumn *rawc = castNode(JsonTableColumn, lfirst(col));
     271                 :         203 :                 Oid                     typid;
     272                 :         203 :                 int32           typmod;
     273                 :         203 :                 Oid                     typcoll = InvalidOid;
     274                 :         203 :                 Node       *colexpr;
     275                 :             : 
     276         [ +  + ]:         203 :                 if (rawc->coltype != JTC_NESTED)
     277                 :             :                 {
     278         [ +  - ]:         161 :                         Assert(rawc->name);
     279                 :         322 :                         tf->colnames = lappend(tf->colnames,
     280                 :         161 :                                                                    makeString(pstrdup(rawc->name)));
     281                 :         161 :                 }
     282                 :             : 
     283                 :             :                 /*
     284                 :             :                  * Determine the type and typmod for the new column. FOR ORDINALITY
     285                 :             :                  * columns are INTEGER by standard; the others are user-specified.
     286                 :             :                  */
     287   [ +  +  +  +  :         203 :                 switch (rawc->coltype)
                      - ]
     288                 :             :                 {
     289                 :             :                         case JTC_FOR_ORDINALITY:
     290         [ +  + ]:          14 :                                 if (ordinality_found)
     291   [ -  +  +  - ]:           1 :                                         ereport(ERROR,
     292                 :             :                                                         (errcode(ERRCODE_SYNTAX_ERROR),
     293                 :             :                                                          errmsg("only one FOR ORDINALITY column is allowed"),
     294                 :             :                                                          parser_errposition(pstate, rawc->location)));
     295                 :          13 :                                 ordinality_found = true;
     296                 :          13 :                                 colexpr = NULL;
     297                 :          13 :                                 typid = INT4OID;
     298                 :          13 :                                 typmod = -1;
     299                 :          13 :                                 break;
     300                 :             : 
     301                 :             :                         case JTC_REGULAR:
     302                 :         106 :                                 typenameTypeIdAndMod(pstate, rawc->typeName, &typid, &typmod);
     303                 :             : 
     304                 :             :                                 /*
     305                 :             :                                  * Use JTC_FORMATTED so as to use JSON_QUERY for this column
     306                 :             :                                  * if the specified type is one that's better handled using
     307                 :             :                                  * JSON_QUERY() or if non-default WRAPPER or QUOTES behavior
     308                 :             :                                  * is specified.
     309                 :             :                                  */
     310         [ +  + ]:         106 :                                 if (isCompositeType(typid) ||
     311   [ +  +  -  + ]:          84 :                                         rawc->quotes != JS_QUOTES_UNSPEC ||
     312                 :         106 :                                         rawc->wrapper != JSW_UNSPEC)
     313                 :          29 :                                         rawc->coltype = JTC_FORMATTED;
     314                 :             : 
     315                 :             :                                 /* FALLTHROUGH */
     316                 :             :                         case JTC_FORMATTED:
     317                 :             :                         case JTC_EXISTS:
     318                 :             :                                 {
     319                 :         147 :                                         JsonFuncExpr *jfe;
     320                 :         147 :                                         CaseTestExpr *param = makeNode(CaseTestExpr);
     321                 :             : 
     322                 :         147 :                                         param->collation = InvalidOid;
     323                 :         147 :                                         param->typeId = contextItemTypid;
     324                 :         147 :                                         param->typeMod = -1;
     325                 :             : 
     326                 :         294 :                                         jfe = transformJsonTableColumn(rawc, (Node *) param,
     327                 :         147 :                                                                                                    passingArgs);
     328                 :             : 
     329                 :         147 :                                         colexpr = transformExpr(pstate, (Node *) jfe,
     330                 :             :                                                                                         EXPR_KIND_FROM_FUNCTION);
     331                 :         147 :                                         assign_expr_collations(pstate, colexpr);
     332                 :             : 
     333                 :         147 :                                         typid = exprType(colexpr);
     334                 :         147 :                                         typmod = exprTypmod(colexpr);
     335                 :         147 :                                         typcoll = exprCollation(colexpr);
     336                 :             :                                         break;
     337                 :         147 :                                 }
     338                 :             : 
     339                 :             :                         case JTC_NESTED:
     340                 :          42 :                                 continue;
     341                 :             : 
     342                 :             :                         default:
     343   [ #  #  #  # ]:           0 :                                 elog(ERROR, "unknown JSON_TABLE column type: %d", (int) rawc->coltype);
     344                 :           0 :                                 break;
     345                 :             :                 }
     346                 :             : 
     347                 :         160 :                 tf->coltypes = lappend_oid(tf->coltypes, typid);
     348                 :         160 :                 tf->coltypmods = lappend_int(tf->coltypmods, typmod);
     349                 :         160 :                 tf->colcollations = lappend_oid(tf->colcollations, typcoll);
     350                 :         160 :                 tf->colvalexprs = lappend(tf->colvalexprs, colexpr);
     351      [ -  +  + ]:         202 :         }
     352                 :             : 
     353                 :             :         /* End of column range. */
     354         [ +  + ]:         115 :         if (list_length(tf->colvalexprs) == colMin)
     355                 :             :         {
     356                 :             :                 /* No columns in this Scan beside the nested ones. */
     357                 :          16 :                 colMax = colMin = -1;
     358                 :          16 :         }
     359                 :             :         else
     360                 :          99 :                 colMax = list_length(tf->colvalexprs) - 1;
     361                 :             : 
     362                 :             :         /* Recursively transform nested columns */
     363                 :         115 :         childplan = transformJsonTableNestedColumns(cxt, passingArgs, columns);
     364                 :             : 
     365                 :             :         /* Create a "parent" scan responsible for all columns handled above. */
     366                 :         345 :         return makeJsonTablePathScan(pathspec, errorOnError, colMin, colMax,
     367                 :         115 :                                                                  childplan);
     368                 :         115 : }
     369                 :             : 
     370                 :             : /*
     371                 :             :  * Check if the type is "composite" for the purpose of checking whether to use
     372                 :             :  * JSON_VALUE() or JSON_QUERY() for a given JsonTableColumn.
     373                 :             :  */
     374                 :             : static bool
     375                 :         112 : isCompositeType(Oid typid)
     376                 :             : {
     377                 :         112 :         char            typtype = get_typtype(typid);
     378                 :             : 
     379         [ +  + ]:         217 :         return typid == JSONOID ||
     380         [ +  + ]:         105 :                 typid == JSONBOID ||
     381         [ +  - ]:          99 :                 typid == RECORDOID ||
     382         [ +  + ]:          99 :                 type_is_array(typid) ||
     383         [ +  + ]:         181 :                 typtype == TYPTYPE_COMPOSITE ||
     384                 :             :         /* domain over one of the above? */
     385         [ +  + ]:          90 :                 (typtype == TYPTYPE_DOMAIN &&
     386                 :           6 :                  isCompositeType(getBaseType(typid)));
     387                 :         112 : }
     388                 :             : 
     389                 :             : /*
     390                 :             :  * Transform JSON_TABLE column definition into a JsonFuncExpr
     391                 :             :  * This turns:
     392                 :             :  *   - regular column into JSON_VALUE()
     393                 :             :  *   - FORMAT JSON column into JSON_QUERY()
     394                 :             :  *   - EXISTS column into JSON_EXISTS()
     395                 :             :  */
     396                 :             : static JsonFuncExpr *
     397                 :         147 : transformJsonTableColumn(JsonTableColumn *jtc, Node *contextItemExpr,
     398                 :             :                                                  List *passingArgs)
     399                 :             : {
     400                 :         147 :         Node       *pathspec;
     401                 :         147 :         JsonFuncExpr *jfexpr = makeNode(JsonFuncExpr);
     402                 :             : 
     403         [ +  + ]:         147 :         if (jtc->coltype == JTC_REGULAR)
     404                 :          77 :                 jfexpr->op = JSON_VALUE_OP;
     405         [ +  + ]:          70 :         else if (jtc->coltype == JTC_EXISTS)
     406                 :          23 :                 jfexpr->op = JSON_EXISTS_OP;
     407                 :             :         else
     408                 :          47 :                 jfexpr->op = JSON_QUERY_OP;
     409                 :             : 
     410                 :             :         /* Pass the column name so any runtime JsonExpr errors can print it. */
     411         [ +  - ]:         147 :         Assert(jtc->name != NULL);
     412                 :         147 :         jfexpr->column_name = pstrdup(jtc->name);
     413                 :             : 
     414                 :         294 :         jfexpr->context_item = makeJsonValueExpr((Expr *) contextItemExpr, NULL,
     415                 :         147 :                                                                                          makeJsonFormat(JS_FORMAT_DEFAULT,
     416                 :             :                                                                                                                         JS_ENC_DEFAULT,
     417                 :             :                                                                                                                         -1));
     418         [ +  + ]:         147 :         if (jtc->pathspec)
     419                 :         132 :                 pathspec = (Node *) jtc->pathspec->string;
     420                 :             :         else
     421                 :             :         {
     422                 :             :                 /* Construct default path as '$."column_name"' */
     423                 :          15 :                 StringInfoData path;
     424                 :             : 
     425                 :          15 :                 initStringInfo(&path);
     426                 :             : 
     427                 :          15 :                 appendStringInfoString(&path, "$.");
     428                 :          15 :                 escape_json(&path, jtc->name);
     429                 :             : 
     430                 :          15 :                 pathspec = makeStringConst(path.data, -1);
     431                 :          15 :         }
     432                 :         147 :         jfexpr->pathspec = pathspec;
     433                 :         147 :         jfexpr->passing = passingArgs;
     434                 :         147 :         jfexpr->output = makeNode(JsonOutput);
     435                 :         147 :         jfexpr->output->typeName = jtc->typeName;
     436                 :         147 :         jfexpr->output->returning = makeNode(JsonReturning);
     437                 :         147 :         jfexpr->output->returning->format = jtc->format;
     438                 :         147 :         jfexpr->on_empty = jtc->on_empty;
     439                 :         147 :         jfexpr->on_error = jtc->on_error;
     440                 :         147 :         jfexpr->quotes = jtc->quotes;
     441                 :         147 :         jfexpr->wrapper = jtc->wrapper;
     442                 :         147 :         jfexpr->location = jtc->location;
     443                 :             : 
     444                 :         294 :         return jfexpr;
     445                 :         147 : }
     446                 :             : 
     447                 :             : /*
     448                 :             :  * Recursively transform nested columns and create child plan(s) that will be
     449                 :             :  * used to evaluate their row patterns.
     450                 :             :  */
     451                 :             : static JsonTablePlan *
     452                 :         115 : transformJsonTableNestedColumns(JsonTableParseContext *cxt,
     453                 :             :                                                                 List *passingArgs,
     454                 :             :                                                                 List *columns)
     455                 :             : {
     456                 :         115 :         JsonTablePlan *plan = NULL;
     457                 :         115 :         ListCell   *lc;
     458                 :             : 
     459                 :             :         /*
     460                 :             :          * If there are multiple NESTED COLUMNS clauses in 'columns', their
     461                 :             :          * respective plans will be combined using a "sibling join" plan, which
     462                 :             :          * effectively does a UNION of the sets of rows coming from each nested
     463                 :             :          * plan.
     464                 :             :          */
     465   [ +  -  +  +  :         312 :         foreach(lc, columns)
                   +  + ]
     466                 :             :         {
     467                 :         197 :                 JsonTableColumn *jtc = castNode(JsonTableColumn, lfirst(lc));
     468                 :         197 :                 JsonTablePlan *nested;
     469                 :             : 
     470         [ +  + ]:         197 :                 if (jtc->coltype != JTC_NESTED)
     471                 :         155 :                         continue;
     472                 :             : 
     473         [ +  + ]:          42 :                 if (jtc->pathspec->name == NULL)
     474                 :          23 :                         jtc->pathspec->name = generateJsonTablePathName(cxt);
     475                 :             : 
     476                 :          84 :                 nested = transformJsonTableColumns(cxt, jtc->columns, passingArgs,
     477                 :          42 :                                                                                    jtc->pathspec);
     478                 :             : 
     479         [ +  + ]:          42 :                 if (plan)
     480                 :          15 :                         plan = makeJsonTableSiblingJoin(plan, nested);
     481                 :             :                 else
     482                 :          27 :                         plan = nested;
     483      [ -  +  + ]:         197 :         }
     484                 :             : 
     485                 :         230 :         return plan;
     486                 :         115 : }
     487                 :             : 
     488                 :             : /*
     489                 :             :  * Create a JsonTablePlan for given path and ON ERROR behavior.
     490                 :             :  *
     491                 :             :  * colMin and colMin give the range of columns computed by this scan in the
     492                 :             :  * global flat list of column expressions that will be passed to the
     493                 :             :  * JSON_TABLE's TableFunc.  Both are -1 when all of columns are nested and
     494                 :             :  * thus computed by 'childplan'.
     495                 :             :  */
     496                 :             : static JsonTablePlan *
     497                 :         115 : makeJsonTablePathScan(JsonTablePathSpec *pathspec, bool errorOnError,
     498                 :             :                                           int colMin, int colMax,
     499                 :             :                                           JsonTablePlan *childplan)
     500                 :             : {
     501                 :         115 :         JsonTablePathScan *scan = makeNode(JsonTablePathScan);
     502                 :         115 :         char       *pathstring;
     503                 :         115 :         Const      *value;
     504                 :             : 
     505         [ +  - ]:         115 :         Assert(IsA(pathspec->string, A_Const));
     506                 :         115 :         pathstring = castNode(A_Const, pathspec->string)->val.sval.sval;
     507                 :         115 :         value = makeConst(JSONPATHOID, -1, InvalidOid, -1,
     508                 :         115 :                                           DirectFunctionCall1(jsonpath_in,
     509                 :             :                                                                                   CStringGetDatum(pathstring)),
     510                 :             :                                           false, false);
     511                 :             : 
     512                 :         115 :         scan->plan.type = T_JsonTablePathScan;
     513                 :         115 :         scan->path = makeJsonTablePath(value, pathspec->name);
     514                 :         115 :         scan->errorOnError = errorOnError;
     515                 :             : 
     516                 :         115 :         scan->child = childplan;
     517                 :             : 
     518                 :         115 :         scan->colMin = colMin;
     519                 :         115 :         scan->colMax = colMax;
     520                 :             : 
     521                 :         230 :         return (JsonTablePlan *) scan;
     522                 :         115 : }
     523                 :             : 
     524                 :             : /*
     525                 :             :  * Create a JsonTablePlan that will perform a join of the rows coming from
     526                 :             :  * 'lplan' and 'rplan'.
     527                 :             :  *
     528                 :             :  * The default way of "joining" the rows is to perform a UNION between the
     529                 :             :  * sets of rows from 'lplan' and 'rplan'.
     530                 :             :  */
     531                 :             : static JsonTablePlan *
     532                 :          15 : makeJsonTableSiblingJoin(JsonTablePlan *lplan, JsonTablePlan *rplan)
     533                 :             : {
     534                 :          15 :         JsonTableSiblingJoin *join = makeNode(JsonTableSiblingJoin);
     535                 :             : 
     536                 :          15 :         join->plan.type = T_JsonTableSiblingJoin;
     537                 :          15 :         join->lplan = lplan;
     538                 :          15 :         join->rplan = rplan;
     539                 :             : 
     540                 :          30 :         return (JsonTablePlan *) join;
     541                 :          15 : }
        

Generated by: LCOV version 2.3.2-1