LCOV - code coverage report
Current view: top level - src/backend/rewrite - rewriteSearchCycle.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 100.0 % 345 345
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 91.2 % 102 93

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * rewriteSearchCycle.c
       4                 :             :  *              Support for rewriting SEARCH and CYCLE clauses.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *        src/backend/rewrite/rewriteSearchCycle.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : #include "postgres.h"
      15                 :             : 
      16                 :             : #include "catalog/pg_operator_d.h"
      17                 :             : #include "catalog/pg_type_d.h"
      18                 :             : #include "nodes/makefuncs.h"
      19                 :             : #include "nodes/parsenodes.h"
      20                 :             : #include "nodes/pg_list.h"
      21                 :             : #include "nodes/primnodes.h"
      22                 :             : #include "parser/analyze.h"
      23                 :             : #include "parser/parsetree.h"
      24                 :             : #include "rewrite/rewriteManip.h"
      25                 :             : #include "rewrite/rewriteSearchCycle.h"
      26                 :             : #include "utils/fmgroids.h"
      27                 :             : 
      28                 :             : 
      29                 :             : /*----------
      30                 :             :  * Rewrite a CTE with SEARCH or CYCLE clause
      31                 :             :  *
      32                 :             :  * Consider a CTE like
      33                 :             :  *
      34                 :             :  * WITH RECURSIVE ctename (col1, col2, col3) AS (
      35                 :             :  *     query1
      36                 :             :  *   UNION [ALL]
      37                 :             :  *     SELECT trosl FROM ctename
      38                 :             :  * )
      39                 :             :  *
      40                 :             :  * With a search clause
      41                 :             :  *
      42                 :             :  * SEARCH BREADTH FIRST BY col1, col2 SET sqc
      43                 :             :  *
      44                 :             :  * the CTE is rewritten to
      45                 :             :  *
      46                 :             :  * WITH RECURSIVE ctename (col1, col2, col3, sqc) AS (
      47                 :             :  *     SELECT col1, col2, col3,               -- original WITH column list
      48                 :             :  *            ROW(0, col1, col2)              -- initial row of search columns
      49                 :             :  *       FROM (query1) "*TLOCRN*" (col1, col2, col3)
      50                 :             :  *   UNION [ALL]
      51                 :             :  *     SELECT col1, col2, col3,               -- same as above
      52                 :             :  *            ROW(sqc.depth + 1, col1, col2)  -- count depth
      53                 :             :  *       FROM (SELECT trosl, ctename.sqc FROM ctename) "*TROCRN*" (col1, col2, col3, sqc)
      54                 :             :  * )
      55                 :             :  *
      56                 :             :  * (This isn't quite legal SQL: sqc.depth is meant to refer to the first
      57                 :             :  * column of sqc, which has a row type, but the field names are not defined
      58                 :             :  * here.  Representing this properly in SQL would be more complicated (and the
      59                 :             :  * SQL standard actually does it in that more complicated way), but the
      60                 :             :  * internal representation allows us to construct it this way.)
      61                 :             :  *
      62                 :             :  * With a search clause
      63                 :             :  *
      64                 :             :  * SEARCH DEPTH FIRST BY col1, col2 SET sqc
      65                 :             :  *
      66                 :             :  * the CTE is rewritten to
      67                 :             :  *
      68                 :             :  * WITH RECURSIVE ctename (col1, col2, col3, sqc) AS (
      69                 :             :  *     SELECT col1, col2, col3,               -- original WITH column list
      70                 :             :  *            ARRAY[ROW(col1, col2)]          -- initial row of search columns
      71                 :             :  *       FROM (query1) "*TLOCRN*" (col1, col2, col3)
      72                 :             :  *   UNION [ALL]
      73                 :             :  *     SELECT col1, col2, col3,               -- same as above
      74                 :             :  *            sqc || ARRAY[ROW(col1, col2)]   -- record rows seen
      75                 :             :  *       FROM (SELECT trosl, ctename.sqc FROM ctename) "*TROCRN*" (col1, col2, col3, sqc)
      76                 :             :  * )
      77                 :             :  *
      78                 :             :  * With a cycle clause
      79                 :             :  *
      80                 :             :  * CYCLE col1, col2 SET cmc TO 'Y' DEFAULT 'N' USING cpa
      81                 :             :  *
      82                 :             :  * (cmc = cycle mark column, cpa = cycle path) the CTE is rewritten to
      83                 :             :  *
      84                 :             :  * WITH RECURSIVE ctename (col1, col2, col3, cmc, cpa) AS (
      85                 :             :  *     SELECT col1, col2, col3,               -- original WITH column list
      86                 :             :  *            'N',                            -- cycle mark default
      87                 :             :  *            ARRAY[ROW(col1, col2)]          -- initial row of cycle columns
      88                 :             :  *       FROM (query1) "*TLOCRN*" (col1, col2, col3)
      89                 :             :  *   UNION [ALL]
      90                 :             :  *     SELECT col1, col2, col3,               -- same as above
      91                 :             :  *            CASE WHEN ROW(col1, col2) = ANY (ARRAY[cpa]) THEN 'Y' ELSE 'N' END,  -- compute cycle mark column
      92                 :             :  *            cpa || ARRAY[ROW(col1, col2)]   -- record rows seen
      93                 :             :  *       FROM (SELECT trosl, ctename.cmc, ctename.cpa FROM ctename) "*TROCRN*" (col1, col2, col3, cmc, cpa)
      94                 :             :  *       WHERE cmc <> 'Y'
      95                 :             :  * )
      96                 :             :  *
      97                 :             :  * The expression to compute the cycle mark column in the right-hand query is
      98                 :             :  * written as
      99                 :             :  *
     100                 :             :  * CASE WHEN ROW(col1, col2) IN (SELECT p.* FROM TABLE(cpa) p) THEN cmv ELSE cmd END
     101                 :             :  *
     102                 :             :  * in the SQL standard, but in PostgreSQL we can use the scalar-array operator
     103                 :             :  * expression shown above.
     104                 :             :  *
     105                 :             :  * Also, in some of the cases where operators are shown above we actually
     106                 :             :  * directly produce the underlying function call.
     107                 :             :  *
     108                 :             :  * If both a search clause and a cycle clause is specified, then the search
     109                 :             :  * clause column is added before the cycle clause columns.
     110                 :             :  */
     111                 :             : 
     112                 :             : /*
     113                 :             :  * Make a RowExpr from the specified column names, which have to be among the
     114                 :             :  * output columns of the CTE.
     115                 :             :  */
     116                 :             : static RowExpr *
     117                 :          26 : make_path_rowexpr(const CommonTableExpr *cte, const List *col_list)
     118                 :             : {
     119                 :          26 :         RowExpr    *rowexpr;
     120                 :          26 :         ListCell   *lc;
     121                 :             : 
     122                 :          26 :         rowexpr = makeNode(RowExpr);
     123                 :          26 :         rowexpr->row_typeid = RECORDOID;
     124                 :          26 :         rowexpr->row_format = COERCE_IMPLICIT_CAST;
     125                 :          26 :         rowexpr->location = -1;
     126                 :             : 
     127   [ +  -  +  +  :          69 :         foreach(lc, col_list)
                   +  + ]
     128                 :             :         {
     129                 :          43 :                 char       *colname = strVal(lfirst(lc));
     130                 :             : 
     131         [ +  - ]:         103 :                 for (int i = 0; i < list_length(cte->ctecolnames); i++)
     132                 :             :                 {
     133                 :          60 :                         char       *colname2 = strVal(list_nth(cte->ctecolnames, i));
     134                 :             : 
     135         [ +  + ]:          60 :                         if (strcmp(colname, colname2) == 0)
     136                 :             :                         {
     137                 :          43 :                                 Var                *var;
     138                 :             : 
     139                 :          86 :                                 var = makeVar(1, i + 1,
     140                 :          43 :                                                           list_nth_oid(cte->ctecoltypes, i),
     141                 :          43 :                                                           list_nth_int(cte->ctecoltypmods, i),
     142                 :          43 :                                                           list_nth_oid(cte->ctecolcollations, i),
     143                 :             :                                                           0);
     144                 :          43 :                                 rowexpr->args = lappend(rowexpr->args, var);
     145                 :          43 :                                 rowexpr->colnames = lappend(rowexpr->colnames, makeString(colname));
     146                 :             :                                 break;
     147                 :          43 :                         }
     148         [ +  + ]:          60 :                 }
     149                 :          43 :         }
     150                 :             : 
     151                 :          52 :         return rowexpr;
     152                 :          26 : }
     153                 :             : 
     154                 :             : /*
     155                 :             :  * Wrap a RowExpr in an ArrayExpr, for the initial search depth first or cycle
     156                 :             :  * row.
     157                 :             :  */
     158                 :             : static Expr *
     159                 :          20 : make_path_initial_array(RowExpr *rowexpr)
     160                 :             : {
     161                 :          20 :         ArrayExpr  *arr;
     162                 :             : 
     163                 :          20 :         arr = makeNode(ArrayExpr);
     164                 :          20 :         arr->array_typeid = RECORDARRAYOID;
     165                 :          20 :         arr->element_typeid = RECORDOID;
     166                 :          20 :         arr->location = -1;
     167                 :          20 :         arr->elements = list_make1(rowexpr);
     168                 :             : 
     169                 :          40 :         return (Expr *) arr;
     170                 :          20 : }
     171                 :             : 
     172                 :             : /*
     173                 :             :  * Make an array catenation expression like
     174                 :             :  *
     175                 :             :  * cpa || ARRAY[ROW(cols)]
     176                 :             :  *
     177                 :             :  * where the varattno of cpa is provided as path_varattno.
     178                 :             :  */
     179                 :             : static Expr *
     180                 :          19 : make_path_cat_expr(RowExpr *rowexpr, AttrNumber path_varattno)
     181                 :             : {
     182                 :          19 :         ArrayExpr  *arr;
     183                 :          19 :         FuncExpr   *fexpr;
     184                 :             : 
     185                 :          19 :         arr = makeNode(ArrayExpr);
     186                 :          19 :         arr->array_typeid = RECORDARRAYOID;
     187                 :          19 :         arr->element_typeid = RECORDOID;
     188                 :          19 :         arr->location = -1;
     189                 :          19 :         arr->elements = list_make1(rowexpr);
     190                 :             : 
     191                 :          19 :         fexpr = makeFuncExpr(F_ARRAY_CAT, RECORDARRAYOID,
     192                 :          19 :                                                  list_make2(makeVar(1, path_varattno, RECORDARRAYOID, -1, 0, 0),
     193                 :             :                                                                         arr),
     194                 :             :                                                  InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
     195                 :             : 
     196                 :          38 :         return (Expr *) fexpr;
     197                 :          19 : }
     198                 :             : 
     199                 :             : /*
     200                 :             :  * The real work happens here.
     201                 :             :  */
     202                 :             : CommonTableExpr *
     203                 :          24 : rewriteSearchAndCycle(CommonTableExpr *cte)
     204                 :             : {
     205                 :          24 :         Query      *ctequery;
     206                 :          24 :         SetOperationStmt *sos;
     207                 :          24 :         int                     rti1,
     208                 :             :                                 rti2;
     209                 :          24 :         RangeTblEntry *rte1,
     210                 :             :                            *rte2,
     211                 :             :                            *newrte;
     212                 :          24 :         Query      *newq1,
     213                 :             :                            *newq2;
     214                 :          24 :         Query      *newsubquery;
     215                 :          24 :         RangeTblRef *rtr;
     216                 :          24 :         Oid                     search_seq_type = InvalidOid;
     217                 :          24 :         AttrNumber      sqc_attno = InvalidAttrNumber;
     218                 :          24 :         AttrNumber      cmc_attno = InvalidAttrNumber;
     219                 :          24 :         AttrNumber      cpa_attno = InvalidAttrNumber;
     220                 :          24 :         TargetEntry *tle;
     221                 :          24 :         RowExpr    *cycle_col_rowexpr = NULL;
     222                 :          24 :         RowExpr    *search_col_rowexpr = NULL;
     223                 :          24 :         List       *ewcl;
     224                 :          24 :         int                     cte_rtindex = -1;
     225                 :             : 
     226   [ +  +  +  - ]:          24 :         Assert(cte->search_clause || cte->cycle_clause);
     227                 :             : 
     228                 :          24 :         cte = copyObject(cte);
     229                 :             : 
     230                 :          24 :         ctequery = castNode(Query, cte->ctequery);
     231                 :             : 
     232                 :             :         /*
     233                 :             :          * The top level of the CTE's query should be a UNION.  Find the two
     234                 :             :          * subqueries.
     235                 :             :          */
     236         [ +  - ]:          24 :         Assert(ctequery->setOperations);
     237                 :          24 :         sos = castNode(SetOperationStmt, ctequery->setOperations);
     238         [ +  - ]:          24 :         Assert(sos->op == SETOP_UNION);
     239                 :             : 
     240                 :          24 :         rti1 = castNode(RangeTblRef, sos->larg)->rtindex;
     241                 :          24 :         rti2 = castNode(RangeTblRef, sos->rarg)->rtindex;
     242                 :             : 
     243                 :          24 :         rte1 = rt_fetch(rti1, ctequery->rtable);
     244                 :          24 :         rte2 = rt_fetch(rti2, ctequery->rtable);
     245                 :             : 
     246         [ +  - ]:          24 :         Assert(rte1->rtekind == RTE_SUBQUERY);
     247         [ +  - ]:          24 :         Assert(rte2->rtekind == RTE_SUBQUERY);
     248                 :             : 
     249                 :             :         /*
     250                 :             :          * We'll need this a few times later.
     251                 :             :          */
     252         [ +  + ]:          24 :         if (cte->search_clause)
     253                 :             :         {
     254         [ +  + ]:          14 :                 if (cte->search_clause->search_breadth_first)
     255                 :           6 :                         search_seq_type = RECORDOID;
     256                 :             :                 else
     257                 :           8 :                         search_seq_type = RECORDARRAYOID;
     258                 :          14 :         }
     259                 :             : 
     260                 :             :         /*
     261                 :             :          * Attribute numbers of the added columns in the CTE's column list
     262                 :             :          */
     263         [ +  + ]:          24 :         if (cte->search_clause)
     264                 :          14 :                 sqc_attno = list_length(cte->ctecolnames) + 1;
     265         [ +  + ]:          24 :         if (cte->cycle_clause)
     266                 :             :         {
     267                 :          12 :                 cmc_attno = list_length(cte->ctecolnames) + 1;
     268                 :          12 :                 cpa_attno = list_length(cte->ctecolnames) + 2;
     269         [ +  + ]:          12 :                 if (cte->search_clause)
     270                 :             :                 {
     271                 :           2 :                         cmc_attno++;
     272                 :           2 :                         cpa_attno++;
     273                 :           2 :                 }
     274                 :          12 :         }
     275                 :             : 
     276                 :             :         /*
     277                 :             :          * Make new left subquery
     278                 :             :          */
     279                 :          24 :         newq1 = makeNode(Query);
     280                 :          24 :         newq1->commandType = CMD_SELECT;
     281                 :          24 :         newq1->canSetTag = true;
     282                 :             : 
     283                 :          24 :         newrte = makeNode(RangeTblEntry);
     284                 :          24 :         newrte->rtekind = RTE_SUBQUERY;
     285                 :          24 :         newrte->alias = NULL;
     286                 :          24 :         newrte->eref = makeAlias("*TLOCRN*", cte->ctecolnames);
     287                 :          24 :         newsubquery = copyObject(rte1->subquery);
     288                 :          24 :         IncrementVarSublevelsUp((Node *) newsubquery, 1, 1);
     289                 :          24 :         newrte->subquery = newsubquery;
     290                 :          24 :         newrte->inFromCl = true;
     291                 :          24 :         newq1->rtable = list_make1(newrte);
     292                 :             : 
     293                 :          24 :         rtr = makeNode(RangeTblRef);
     294                 :          24 :         rtr->rtindex = 1;
     295                 :          24 :         newq1->jointree = makeFromExpr(list_make1(rtr), NULL);
     296                 :             : 
     297                 :             :         /*
     298                 :             :          * Make target list
     299                 :             :          */
     300         [ +  + ]:          78 :         for (int i = 0; i < list_length(cte->ctecolnames); i++)
     301                 :             :         {
     302                 :          54 :                 Var                *var;
     303                 :             : 
     304                 :         108 :                 var = makeVar(1, i + 1,
     305                 :          54 :                                           list_nth_oid(cte->ctecoltypes, i),
     306                 :          54 :                                           list_nth_int(cte->ctecoltypmods, i),
     307                 :          54 :                                           list_nth_oid(cte->ctecolcollations, i),
     308                 :             :                                           0);
     309                 :          54 :                 tle = makeTargetEntry((Expr *) var, i + 1, strVal(list_nth(cte->ctecolnames, i)), false);
     310                 :          54 :                 tle->resorigtbl = list_nth_node(TargetEntry, rte1->subquery->targetList, i)->resorigtbl;
     311                 :          54 :                 tle->resorigcol = list_nth_node(TargetEntry, rte1->subquery->targetList, i)->resorigcol;
     312                 :          54 :                 newq1->targetList = lappend(newq1->targetList, tle);
     313                 :          54 :         }
     314                 :             : 
     315         [ +  + ]:          24 :         if (cte->search_clause)
     316                 :             :         {
     317                 :          14 :                 Expr       *texpr;
     318                 :             : 
     319                 :          14 :                 search_col_rowexpr = make_path_rowexpr(cte, cte->search_clause->search_col_list);
     320         [ +  + ]:          14 :                 if (cte->search_clause->search_breadth_first)
     321                 :             :                 {
     322                 :          12 :                         search_col_rowexpr->args = lcons(makeConst(INT8OID, -1, InvalidOid, sizeof(int64),
     323                 :           6 :                                                                                                            Int64GetDatum(0), false, true),
     324                 :           6 :                                                                                          search_col_rowexpr->args);
     325                 :           6 :                         search_col_rowexpr->colnames = lcons(makeString("*DEPTH*"), search_col_rowexpr->colnames);
     326                 :           6 :                         texpr = (Expr *) search_col_rowexpr;
     327                 :           6 :                 }
     328                 :             :                 else
     329                 :           8 :                         texpr = make_path_initial_array(search_col_rowexpr);
     330                 :          28 :                 tle = makeTargetEntry(texpr,
     331                 :          14 :                                                           list_length(newq1->targetList) + 1,
     332                 :          14 :                                                           cte->search_clause->search_seq_column,
     333                 :             :                                                           false);
     334                 :          14 :                 newq1->targetList = lappend(newq1->targetList, tle);
     335                 :          14 :         }
     336         [ +  + ]:          24 :         if (cte->cycle_clause)
     337                 :             :         {
     338                 :          24 :                 tle = makeTargetEntry((Expr *) cte->cycle_clause->cycle_mark_default,
     339                 :          12 :                                                           list_length(newq1->targetList) + 1,
     340                 :          12 :                                                           cte->cycle_clause->cycle_mark_column,
     341                 :             :                                                           false);
     342                 :          12 :                 newq1->targetList = lappend(newq1->targetList, tle);
     343                 :          12 :                 cycle_col_rowexpr = make_path_rowexpr(cte, cte->cycle_clause->cycle_col_list);
     344                 :          24 :                 tle = makeTargetEntry(make_path_initial_array(cycle_col_rowexpr),
     345                 :          12 :                                                           list_length(newq1->targetList) + 1,
     346                 :          12 :                                                           cte->cycle_clause->cycle_path_column,
     347                 :             :                                                           false);
     348                 :          12 :                 newq1->targetList = lappend(newq1->targetList, tle);
     349                 :          12 :         }
     350                 :             : 
     351                 :          24 :         rte1->subquery = newq1;
     352                 :             : 
     353         [ +  + ]:          24 :         if (cte->search_clause)
     354                 :             :         {
     355                 :          14 :                 rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->search_clause->search_seq_column));
     356                 :          14 :         }
     357         [ +  + ]:          24 :         if (cte->cycle_clause)
     358                 :             :         {
     359                 :          12 :                 rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
     360                 :          12 :                 rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
     361                 :          12 :         }
     362                 :             : 
     363                 :             :         /*
     364                 :             :          * Make new right subquery
     365                 :             :          */
     366                 :          24 :         newq2 = makeNode(Query);
     367                 :          24 :         newq2->commandType = CMD_SELECT;
     368                 :          24 :         newq2->canSetTag = true;
     369                 :             : 
     370                 :          24 :         newrte = makeNode(RangeTblEntry);
     371                 :          24 :         newrte->rtekind = RTE_SUBQUERY;
     372                 :          24 :         ewcl = copyObject(cte->ctecolnames);
     373         [ +  + ]:          24 :         if (cte->search_clause)
     374                 :             :         {
     375                 :          14 :                 ewcl = lappend(ewcl, makeString(cte->search_clause->search_seq_column));
     376                 :          14 :         }
     377         [ +  + ]:          24 :         if (cte->cycle_clause)
     378                 :             :         {
     379                 :          12 :                 ewcl = lappend(ewcl, makeString(cte->cycle_clause->cycle_mark_column));
     380                 :          12 :                 ewcl = lappend(ewcl, makeString(cte->cycle_clause->cycle_path_column));
     381                 :          12 :         }
     382                 :          24 :         newrte->alias = NULL;
     383                 :          24 :         newrte->eref = makeAlias("*TROCRN*", ewcl);
     384                 :             : 
     385                 :             :         /*
     386                 :             :          * Find the reference to the recursive CTE in the right UNION subquery's
     387                 :             :          * range table.  We expect it to be two levels up from the UNION subquery
     388                 :             :          * (and must check that to avoid being fooled by sub-WITHs with the same
     389                 :             :          * CTE name).  There will not be more than one such reference, because the
     390                 :             :          * parser would have rejected that (see checkWellFormedRecursion() in
     391                 :             :          * parse_cte.c).  However, the parser doesn't insist that the reference
     392                 :             :          * appear in the UNION subquery's topmost range table, so we might fail to
     393                 :             :          * find it at all.  That's an unimplemented case for the moment.
     394                 :             :          */
     395         [ +  + ]:          63 :         for (int rti = 1; rti <= list_length(rte2->subquery->rtable); rti++)
     396                 :             :         {
     397                 :          39 :                 RangeTblEntry *e = rt_fetch(rti, rte2->subquery->rtable);
     398                 :             : 
     399         [ +  + ]:          39 :                 if (e->rtekind == RTE_CTE &&
     400   [ +  +  +  + ]:          25 :                         strcmp(cte->ctename, e->ctename) == 0 &&
     401                 :          24 :                         e->ctelevelsup == 2)
     402                 :             :                 {
     403                 :          23 :                         cte_rtindex = rti;
     404                 :          23 :                         break;
     405                 :             :                 }
     406         [ +  + ]:          39 :         }
     407         [ +  + ]:          24 :         if (cte_rtindex <= 0)
     408   [ +  -  +  - ]:           1 :                 ereport(ERROR,
     409                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     410                 :             :                                  errmsg("with a SEARCH or CYCLE clause, the recursive reference to WITH query \"%s\" must be at the top level of its right-hand SELECT",
     411                 :             :                                                 cte->ctename)));
     412                 :             : 
     413                 :          23 :         newsubquery = copyObject(rte2->subquery);
     414                 :          23 :         IncrementVarSublevelsUp((Node *) newsubquery, 1, 1);
     415                 :             : 
     416                 :             :         /*
     417                 :             :          * Add extra columns to target list of subquery of right subquery
     418                 :             :          */
     419         [ +  + ]:          23 :         if (cte->search_clause)
     420                 :             :         {
     421                 :          13 :                 Var                *var;
     422                 :             : 
     423                 :             :                 /* ctename.sqc */
     424                 :          26 :                 var = makeVar(cte_rtindex, sqc_attno,
     425                 :          13 :                                           search_seq_type, -1, InvalidOid, 0);
     426                 :          26 :                 tle = makeTargetEntry((Expr *) var,
     427                 :          13 :                                                           list_length(newsubquery->targetList) + 1,
     428                 :          13 :                                                           cte->search_clause->search_seq_column,
     429                 :             :                                                           false);
     430                 :          13 :                 newsubquery->targetList = lappend(newsubquery->targetList, tle);
     431                 :          13 :         }
     432         [ +  + ]:          23 :         if (cte->cycle_clause)
     433                 :             :         {
     434                 :          12 :                 Var                *var;
     435                 :             : 
     436                 :             :                 /* ctename.cmc */
     437                 :          24 :                 var = makeVar(cte_rtindex, cmc_attno,
     438                 :          12 :                                           cte->cycle_clause->cycle_mark_type,
     439                 :          12 :                                           cte->cycle_clause->cycle_mark_typmod,
     440                 :          12 :                                           cte->cycle_clause->cycle_mark_collation, 0);
     441                 :          24 :                 tle = makeTargetEntry((Expr *) var,
     442                 :          12 :                                                           list_length(newsubquery->targetList) + 1,
     443                 :          12 :                                                           cte->cycle_clause->cycle_mark_column,
     444                 :             :                                                           false);
     445                 :          12 :                 newsubquery->targetList = lappend(newsubquery->targetList, tle);
     446                 :             : 
     447                 :             :                 /* ctename.cpa */
     448                 :          12 :                 var = makeVar(cte_rtindex, cpa_attno,
     449                 :             :                                           RECORDARRAYOID, -1, InvalidOid, 0);
     450                 :          24 :                 tle = makeTargetEntry((Expr *) var,
     451                 :          12 :                                                           list_length(newsubquery->targetList) + 1,
     452                 :          12 :                                                           cte->cycle_clause->cycle_path_column,
     453                 :             :                                                           false);
     454                 :          12 :                 newsubquery->targetList = lappend(newsubquery->targetList, tle);
     455                 :          12 :         }
     456                 :             : 
     457                 :          23 :         newrte->subquery = newsubquery;
     458                 :          23 :         newrte->inFromCl = true;
     459                 :          23 :         newq2->rtable = list_make1(newrte);
     460                 :             : 
     461                 :          23 :         rtr = makeNode(RangeTblRef);
     462                 :          23 :         rtr->rtindex = 1;
     463                 :             : 
     464         [ +  + ]:          23 :         if (cte->cycle_clause)
     465                 :             :         {
     466                 :          12 :                 Expr       *expr;
     467                 :             : 
     468                 :             :                 /*
     469                 :             :                  * Add cmc <> cmv condition
     470                 :             :                  */
     471                 :          24 :                 expr = make_opclause(cte->cycle_clause->cycle_mark_neop, BOOLOID, false,
     472                 :          24 :                                                          (Expr *) makeVar(1, cmc_attno,
     473                 :          12 :                                                                                           cte->cycle_clause->cycle_mark_type,
     474                 :          12 :                                                                                           cte->cycle_clause->cycle_mark_typmod,
     475                 :          12 :                                                                                           cte->cycle_clause->cycle_mark_collation, 0),
     476                 :          12 :                                                          (Expr *) cte->cycle_clause->cycle_mark_value,
     477                 :             :                                                          InvalidOid,
     478                 :          12 :                                                          cte->cycle_clause->cycle_mark_collation);
     479                 :             : 
     480                 :          12 :                 newq2->jointree = makeFromExpr(list_make1(rtr), (Node *) expr);
     481                 :          12 :         }
     482                 :             :         else
     483                 :          11 :                 newq2->jointree = makeFromExpr(list_make1(rtr), NULL);
     484                 :             : 
     485                 :             :         /*
     486                 :             :          * Make target list
     487                 :             :          */
     488         [ +  + ]:          76 :         for (int i = 0; i < list_length(cte->ctecolnames); i++)
     489                 :             :         {
     490                 :          53 :                 Var                *var;
     491                 :             : 
     492                 :         106 :                 var = makeVar(1, i + 1,
     493                 :          53 :                                           list_nth_oid(cte->ctecoltypes, i),
     494                 :          53 :                                           list_nth_int(cte->ctecoltypmods, i),
     495                 :          53 :                                           list_nth_oid(cte->ctecolcollations, i),
     496                 :             :                                           0);
     497                 :          53 :                 tle = makeTargetEntry((Expr *) var, i + 1, strVal(list_nth(cte->ctecolnames, i)), false);
     498                 :          53 :                 tle->resorigtbl = list_nth_node(TargetEntry, rte2->subquery->targetList, i)->resorigtbl;
     499                 :          53 :                 tle->resorigcol = list_nth_node(TargetEntry, rte2->subquery->targetList, i)->resorigcol;
     500                 :          53 :                 newq2->targetList = lappend(newq2->targetList, tle);
     501                 :          53 :         }
     502                 :             : 
     503         [ +  + ]:          23 :         if (cte->search_clause)
     504                 :             :         {
     505                 :          13 :                 Expr       *texpr;
     506                 :             : 
     507         [ +  + ]:          13 :                 if (cte->search_clause->search_breadth_first)
     508                 :             :                 {
     509                 :           6 :                         FieldSelect *fs;
     510                 :           6 :                         FuncExpr   *fexpr;
     511                 :             : 
     512                 :             :                         /*
     513                 :             :                          * ROW(sqc.depth + 1, cols)
     514                 :             :                          */
     515                 :             : 
     516                 :           6 :                         search_col_rowexpr = copyObject(search_col_rowexpr);
     517                 :             : 
     518                 :           6 :                         fs = makeNode(FieldSelect);
     519                 :           6 :                         fs->arg = (Expr *) makeVar(1, sqc_attno, RECORDOID, -1, 0, 0);
     520                 :           6 :                         fs->fieldnum = 1;
     521                 :           6 :                         fs->resulttype = INT8OID;
     522                 :           6 :                         fs->resulttypmod = -1;
     523                 :             : 
     524                 :           6 :                         fexpr = makeFuncExpr(F_INT8INC, INT8OID, list_make1(fs), InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
     525                 :             : 
     526                 :           6 :                         linitial(search_col_rowexpr->args) = fexpr;
     527                 :             : 
     528                 :           6 :                         texpr = (Expr *) search_col_rowexpr;
     529                 :           6 :                 }
     530                 :             :                 else
     531                 :             :                 {
     532                 :             :                         /*
     533                 :             :                          * sqc || ARRAY[ROW(cols)]
     534                 :             :                          */
     535                 :           7 :                         texpr = make_path_cat_expr(search_col_rowexpr, sqc_attno);
     536                 :             :                 }
     537                 :          26 :                 tle = makeTargetEntry(texpr,
     538                 :          13 :                                                           list_length(newq2->targetList) + 1,
     539                 :          13 :                                                           cte->search_clause->search_seq_column,
     540                 :             :                                                           false);
     541                 :          13 :                 newq2->targetList = lappend(newq2->targetList, tle);
     542                 :          13 :         }
     543                 :             : 
     544         [ +  + ]:          23 :         if (cte->cycle_clause)
     545                 :             :         {
     546                 :          12 :                 ScalarArrayOpExpr *saoe;
     547                 :          12 :                 CaseExpr   *caseexpr;
     548                 :          12 :                 CaseWhen   *casewhen;
     549                 :             : 
     550                 :             :                 /*
     551                 :             :                  * CASE WHEN ROW(cols) = ANY (ARRAY[cpa]) THEN cmv ELSE cmd END
     552                 :             :                  */
     553                 :             : 
     554                 :          12 :                 saoe = makeNode(ScalarArrayOpExpr);
     555                 :          12 :                 saoe->location = -1;
     556                 :          12 :                 saoe->opno = RECORD_EQ_OP;
     557                 :          12 :                 saoe->useOr = true;
     558                 :          12 :                 saoe->args = list_make2(cycle_col_rowexpr,
     559                 :             :                                                                 makeVar(1, cpa_attno, RECORDARRAYOID, -1, 0, 0));
     560                 :             : 
     561                 :          12 :                 caseexpr = makeNode(CaseExpr);
     562                 :          12 :                 caseexpr->location = -1;
     563                 :          12 :                 caseexpr->casetype = cte->cycle_clause->cycle_mark_type;
     564                 :          12 :                 caseexpr->casecollid = cte->cycle_clause->cycle_mark_collation;
     565                 :          12 :                 casewhen = makeNode(CaseWhen);
     566                 :          12 :                 casewhen->location = -1;
     567                 :          12 :                 casewhen->expr = (Expr *) saoe;
     568                 :          12 :                 casewhen->result = (Expr *) cte->cycle_clause->cycle_mark_value;
     569                 :          12 :                 caseexpr->args = list_make1(casewhen);
     570                 :          12 :                 caseexpr->defresult = (Expr *) cte->cycle_clause->cycle_mark_default;
     571                 :             : 
     572                 :          24 :                 tle = makeTargetEntry((Expr *) caseexpr,
     573                 :          12 :                                                           list_length(newq2->targetList) + 1,
     574                 :          12 :                                                           cte->cycle_clause->cycle_mark_column,
     575                 :             :                                                           false);
     576                 :          12 :                 newq2->targetList = lappend(newq2->targetList, tle);
     577                 :             : 
     578                 :             :                 /*
     579                 :             :                  * cpa || ARRAY[ROW(cols)]
     580                 :             :                  */
     581                 :          24 :                 tle = makeTargetEntry(make_path_cat_expr(cycle_col_rowexpr, cpa_attno),
     582                 :          12 :                                                           list_length(newq2->targetList) + 1,
     583                 :          12 :                                                           cte->cycle_clause->cycle_path_column,
     584                 :             :                                                           false);
     585                 :          12 :                 newq2->targetList = lappend(newq2->targetList, tle);
     586                 :          12 :         }
     587                 :             : 
     588                 :          23 :         rte2->subquery = newq2;
     589                 :             : 
     590         [ +  + ]:          23 :         if (cte->search_clause)
     591                 :             :         {
     592                 :          13 :                 rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->search_clause->search_seq_column));
     593                 :          13 :         }
     594         [ +  + ]:          23 :         if (cte->cycle_clause)
     595                 :             :         {
     596                 :          12 :                 rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
     597                 :          12 :                 rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
     598                 :          12 :         }
     599                 :             : 
     600                 :             :         /*
     601                 :             :          * Add the additional columns to the SetOperationStmt
     602                 :             :          */
     603         [ +  + ]:          23 :         if (cte->search_clause)
     604                 :             :         {
     605                 :          13 :                 sos->colTypes = lappend_oid(sos->colTypes, search_seq_type);
     606                 :          13 :                 sos->colTypmods = lappend_int(sos->colTypmods, -1);
     607                 :          13 :                 sos->colCollations = lappend_oid(sos->colCollations, InvalidOid);
     608         [ +  + ]:          13 :                 if (!sos->all)
     609                 :           4 :                         sos->groupClauses = lappend(sos->groupClauses,
     610                 :           2 :                                                                                 makeSortGroupClauseForSetOp(search_seq_type, true));
     611                 :          13 :         }
     612         [ +  + ]:          23 :         if (cte->cycle_clause)
     613                 :             :         {
     614                 :          12 :                 sos->colTypes = lappend_oid(sos->colTypes, cte->cycle_clause->cycle_mark_type);
     615                 :          12 :                 sos->colTypmods = lappend_int(sos->colTypmods, cte->cycle_clause->cycle_mark_typmod);
     616                 :          12 :                 sos->colCollations = lappend_oid(sos->colCollations, cte->cycle_clause->cycle_mark_collation);
     617         [ +  + ]:          12 :                 if (!sos->all)
     618                 :           2 :                         sos->groupClauses = lappend(sos->groupClauses,
     619                 :           1 :                                                                                 makeSortGroupClauseForSetOp(cte->cycle_clause->cycle_mark_type, true));
     620                 :             : 
     621                 :          12 :                 sos->colTypes = lappend_oid(sos->colTypes, RECORDARRAYOID);
     622                 :          12 :                 sos->colTypmods = lappend_int(sos->colTypmods, -1);
     623                 :          12 :                 sos->colCollations = lappend_oid(sos->colCollations, InvalidOid);
     624         [ +  + ]:          12 :                 if (!sos->all)
     625                 :           2 :                         sos->groupClauses = lappend(sos->groupClauses,
     626                 :           1 :                                                                                 makeSortGroupClauseForSetOp(RECORDARRAYOID, true));
     627                 :          12 :         }
     628                 :             : 
     629                 :             :         /*
     630                 :             :          * Add the additional columns to the CTE query's target list
     631                 :             :          */
     632         [ +  + ]:          23 :         if (cte->search_clause)
     633                 :             :         {
     634                 :          26 :                 ctequery->targetList = lappend(ctequery->targetList,
     635                 :          39 :                                                                            makeTargetEntry((Expr *) makeVar(1, sqc_attno,
     636                 :          13 :                                                                                                                                                 search_seq_type, -1, InvalidOid, 0),
     637                 :          13 :                                                                                                            list_length(ctequery->targetList) + 1,
     638                 :          13 :                                                                                                            cte->search_clause->search_seq_column,
     639                 :             :                                                                                                            false));
     640                 :          13 :         }
     641         [ +  + ]:          23 :         if (cte->cycle_clause)
     642                 :             :         {
     643                 :          24 :                 ctequery->targetList = lappend(ctequery->targetList,
     644                 :          36 :                                                                            makeTargetEntry((Expr *) makeVar(1, cmc_attno,
     645                 :          12 :                                                                                                                                                 cte->cycle_clause->cycle_mark_type,
     646                 :          12 :                                                                                                                                                 cte->cycle_clause->cycle_mark_typmod,
     647                 :          12 :                                                                                                                                                 cte->cycle_clause->cycle_mark_collation, 0),
     648                 :          12 :                                                                                                            list_length(ctequery->targetList) + 1,
     649                 :          12 :                                                                                                            cte->cycle_clause->cycle_mark_column,
     650                 :             :                                                                                                            false));
     651                 :          24 :                 ctequery->targetList = lappend(ctequery->targetList,
     652                 :          24 :                                                                            makeTargetEntry((Expr *) makeVar(1, cpa_attno,
     653                 :             :                                                                                                                                                 RECORDARRAYOID, -1, InvalidOid, 0),
     654                 :          12 :                                                                                                            list_length(ctequery->targetList) + 1,
     655                 :          12 :                                                                                                            cte->cycle_clause->cycle_path_column,
     656                 :             :                                                                                                            false));
     657                 :          12 :         }
     658                 :             : 
     659                 :             :         /*
     660                 :             :          * Add the additional columns to the CTE's output columns
     661                 :             :          */
     662                 :          23 :         cte->ctecolnames = ewcl;
     663         [ +  + ]:          23 :         if (cte->search_clause)
     664                 :             :         {
     665                 :          13 :                 cte->ctecoltypes = lappend_oid(cte->ctecoltypes, search_seq_type);
     666                 :          13 :                 cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, -1);
     667                 :          13 :                 cte->ctecolcollations = lappend_oid(cte->ctecolcollations, InvalidOid);
     668                 :          13 :         }
     669         [ +  + ]:          23 :         if (cte->cycle_clause)
     670                 :             :         {
     671                 :          12 :                 cte->ctecoltypes = lappend_oid(cte->ctecoltypes, cte->cycle_clause->cycle_mark_type);
     672                 :          12 :                 cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, cte->cycle_clause->cycle_mark_typmod);
     673                 :          12 :                 cte->ctecolcollations = lappend_oid(cte->ctecolcollations, cte->cycle_clause->cycle_mark_collation);
     674                 :             : 
     675                 :          12 :                 cte->ctecoltypes = lappend_oid(cte->ctecoltypes, RECORDARRAYOID);
     676                 :          12 :                 cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, -1);
     677                 :          12 :                 cte->ctecolcollations = lappend_oid(cte->ctecolcollations, InvalidOid);
     678                 :          12 :         }
     679                 :             : 
     680                 :          46 :         return cte;
     681                 :          23 : }
        

Generated by: LCOV version 2.3.2-1