summaryrefslogtreecommitdiffstats
path: root/src/backend/rewrite/rewriteSearchCycle.c
blob: 175f33cccc413ede79e1c90813b13bea3a5d0cf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*-------------------------------------------------------------------------
 *
 * rewriteSearchCycle.c
 *		Support for rewriting SEARCH and CYCLE clauses.
 *
 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/rewrite/rewriteSearchCycle.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "catalog/pg_operator_d.h"
#include "catalog/pg_type_d.h"
#include "nodes/makefuncs.h"
#include "nodes/pg_list.h"
#include "nodes/parsenodes.h"
#include "nodes/primnodes.h"
#include "parser/analyze.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
#include "rewrite/rewriteSearchCycle.h"
#include "utils/fmgroids.h"


/*----------
 * Rewrite a CTE with SEARCH or CYCLE clause
 *
 * Consider a CTE like
 *
 * WITH RECURSIVE ctename (col1, col2, col3) AS (
 *     query1
 *   UNION [ALL]
 *     SELECT trosl FROM ctename
 * )
 *
 * With a search clause
 *
 * SEARCH BREADTH FIRST BY col1, col2 SET sqc
 *
 * the CTE is rewritten to
 *
 * WITH RECURSIVE ctename (col1, col2, col3, sqc) AS (
 *     SELECT col1, col2, col3,               -- original WITH column list
 *            ROW(0, col1, col2)              -- initial row of search columns
 *       FROM (query1) "*TLOCRN*" (col1, col2, col3)
 *   UNION [ALL]
 *     SELECT col1, col2, col3,               -- same as above
 *            ROW(sqc.depth + 1, col1, col2)  -- count depth
 *       FROM (SELECT trosl, ctename.sqc FROM ctename) "*TROCRN*" (col1, col2, col3, sqc)
 * )
 *
 * (This isn't quite legal SQL: sqc.depth is meant to refer to the first
 * column of sqc, which has a row type, but the field names are not defined
 * here.  Representing this properly in SQL would be more complicated (and the
 * SQL standard actually does it in that more complicated way), but the
 * internal representation allows us to construct it this way.)
 *
 * With a search clause
 *
 * SEARCH DEPTH FIRST BY col1, col2 SET sqc
 *
 * the CTE is rewritten to
 *
 * WITH RECURSIVE ctename (col1, col2, col3, sqc) AS (
 *     SELECT col1, col2, col3,               -- original WITH column list
 *            ARRAY[ROW(col1, col2)]          -- initial row of search columns
 *       FROM (query1) "*TLOCRN*" (col1, col2, col3)
 *   UNION [ALL]
 *     SELECT col1, col2, col3,               -- same as above
 *            sqc || ARRAY[ROW(col1, col2)]   -- record rows seen
 *       FROM (SELECT trosl, ctename.sqc FROM ctename) "*TROCRN*" (col1, col2, col3, sqc)
 * )
 *
 * With a cycle clause
 *
 * CYCLE col1, col2 SET cmc TO 'Y' DEFAULT 'N' USING cpa
 *
 * (cmc = cycle mark column, cpa = cycle path) the CTE is rewritten to
 *
 * WITH RECURSIVE ctename (col1, col2, col3, cmc, cpa) AS (
 *     SELECT col1, col2, col3,               -- original WITH column list
 *            'N',                            -- cycle mark default
 *            ARRAY[ROW(col1, col2)]          -- initial row of cycle columns
 *       FROM (query1) "*TLOCRN*" (col1, col2, col3)
 *   UNION [ALL]
 *     SELECT col1, col2, col3,               -- same as above
 *            CASE WHEN ROW(col1, col2) = ANY (ARRAY[cpa]) THEN 'Y' ELSE 'N' END,  -- compute cycle mark column
 *            cpa || ARRAY[ROW(col1, col2)]   -- record rows seen
 *       FROM (SELECT trosl, ctename.cmc, ctename.cpa FROM ctename) "*TROCRN*" (col1, col2, col3, cmc, cpa)
 *       WHERE cmc <> 'Y'
 * )
 *
 * The expression to compute the cycle mark column in the right-hand query is
 * written as
 *
 * CASE WHEN ROW(col1, col2) IN (SELECT p.* FROM TABLE(cpa) p) THEN cmv ELSE cmd END
 *
 * in the SQL standard, but in PostgreSQL we can use the scalar-array operator
 * expression shown above.
 *
 * Also, in some of the cases where operators are shown above we actually
 * directly produce the underlying function call.
 *
 * If both a search clause and a cycle clause is specified, then the search
 * clause column is added before the cycle clause columns.
 */

/*
 * Make a RowExpr from the specified column names, which have to be among the
 * output columns of the CTE.
 */
static RowExpr *
make_path_rowexpr(const CommonTableExpr *cte, const List *col_list)
{
	RowExpr    *rowexpr;
	ListCell   *lc;

	rowexpr = makeNode(RowExpr);
	rowexpr->row_typeid = RECORDOID;
	rowexpr->row_format = COERCE_IMPLICIT_CAST;
	rowexpr->location = -1;

	foreach(lc, col_list)
	{
		char	   *colname = strVal(lfirst(lc));

		for (int i = 0; i < list_length(cte->ctecolnames); i++)
		{
			char	   *colname2 = strVal(list_nth(cte->ctecolnames, i));

			if (strcmp(colname, colname2) == 0)
			{
				Var		   *var;

				var = makeVar(1, i + 1,
							  list_nth_oid(cte->ctecoltypes, i),
							  list_nth_int(cte->ctecoltypmods, i),
							  list_nth_oid(cte->ctecolcollations, i),
							  0);
				rowexpr->args = lappend(rowexpr->args, var);
				rowexpr->colnames = lappend(rowexpr->colnames, makeString(colname));
				break;
			}
		}
	}

	return rowexpr;
}

/*
 * Wrap a RowExpr in an ArrayExpr, for the initial search depth first or cycle
 * row.
 */
static Expr *
make_path_initial_array(RowExpr *rowexpr)
{
	ArrayExpr  *arr;

	arr = makeNode(ArrayExpr);
	arr->array_typeid = RECORDARRAYOID;
	arr->element_typeid = RECORDOID;
	arr->location = -1;
	arr->elements = list_make1(rowexpr);

	return (Expr *) arr;
}

/*
 * Make an array catenation expression like
 *
 * cpa || ARRAY[ROW(cols)]
 *
 * where the varattno of cpa is provided as path_varattno.
 */
static Expr *
make_path_cat_expr(RowExpr *rowexpr, AttrNumber path_varattno)
{
	ArrayExpr  *arr;
	FuncExpr   *fexpr;

	arr = makeNode(ArrayExpr);
	arr->array_typeid = RECORDARRAYOID;
	arr->element_typeid = RECORDOID;
	arr->location = -1;
	arr->elements = list_make1(rowexpr);

	fexpr = makeFuncExpr(F_ARRAY_CAT, RECORDARRAYOID,
						 list_make2(makeVar(1, path_varattno, RECORDARRAYOID, -1, 0, 0),
									arr),
						 InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);

	return (Expr *) fexpr;
}

/*
 * The real work happens here.
 */
CommonTableExpr *
rewriteSearchAndCycle(CommonTableExpr *cte)
{
	Query	   *ctequery;
	SetOperationStmt *sos;
	int			rti1,
				rti2;
	RangeTblEntry *rte1,
			   *rte2,
			   *newrte;
	Query	   *newq1,
			   *newq2;
	Query	   *newsubquery;
	RangeTblRef *rtr;
	Oid			search_seq_type = InvalidOid;
	AttrNumber	sqc_attno = InvalidAttrNumber;
	AttrNumber	cmc_attno = InvalidAttrNumber;
	AttrNumber	cpa_attno = InvalidAttrNumber;
	TargetEntry *tle;
	RowExpr    *cycle_col_rowexpr = NULL;
	RowExpr    *search_col_rowexpr = NULL;
	List	   *ewcl;
	int			cte_rtindex = -1;

	Assert(cte->search_clause || cte->cycle_clause);

	cte = copyObject(cte);

	ctequery = castNode(Query, cte->ctequery);

	/*
	 * The top level of the CTE's query should be a UNION.  Find the two
	 * subqueries.
	 */
	Assert(ctequery->setOperations);
	sos = castNode(SetOperationStmt, ctequery->setOperations);
	Assert(sos->op == SETOP_UNION);

	rti1 = castNode(RangeTblRef, sos->larg)->rtindex;
	rti2 = castNode(RangeTblRef, sos->rarg)->rtindex;

	rte1 = rt_fetch(rti1, ctequery->rtable);
	rte2 = rt_fetch(rti2, ctequery->rtable);

	Assert(rte1->rtekind == RTE_SUBQUERY);
	Assert(rte2->rtekind == RTE_SUBQUERY);

	/*
	 * We'll need this a few times later.
	 */
	if (cte->search_clause)
	{
		if (cte->search_clause->search_breadth_first)
			search_seq_type = RECORDOID;
		else
			search_seq_type = RECORDARRAYOID;
	}

	/*
	 * Attribute numbers of the added columns in the CTE's column list
	 */
	if (cte->search_clause)
		sqc_attno = list_length(cte->ctecolnames) + 1;
	if (cte->cycle_clause)
	{
		cmc_attno = list_length(cte->ctecolnames) + 1;
		cpa_attno = list_length(cte->ctecolnames) + 2;
		if (cte->search_clause)
		{
			cmc_attno++;
			cpa_attno++;
		}
	}

	/*
	 * Make new left subquery
	 */
	newq1 = makeNode(Query);
	newq1->commandType = CMD_SELECT;
	newq1->canSetTag = true;

	newrte = makeNode(RangeTblEntry);
	newrte->rtekind = RTE_SUBQUERY;
	newrte->alias = makeAlias("*TLOCRN*", cte->ctecolnames);
	newrte->eref = newrte->alias;
	newsubquery = copyObject(rte1->subquery);
	IncrementVarSublevelsUp((Node *) newsubquery, 1, 1);
	newrte->subquery = newsubquery;
	newrte->inFromCl = true;
	newq1->rtable = list_make1(newrte);

	rtr = makeNode(RangeTblRef);
	rtr->rtindex = 1;
	newq1->jointree = makeFromExpr(list_make1(rtr), NULL);

	/*
	 * Make target list
	 */
	for (int i = 0; i < list_length(cte->ctecolnames); i++)
	{
		Var		   *var;

		var = makeVar(1, i + 1,
					  list_nth_oid(cte->ctecoltypes, i),
					  list_nth_int(cte->ctecoltypmods, i),
					  list_nth_oid(cte->ctecolcollations, i),
					  0);
		tle = makeTargetEntry((Expr *) var, i + 1, strVal(list_nth(cte->ctecolnames, i)), false);
		tle->resorigtbl = castNode(TargetEntry, list_nth(rte1->subquery->targetList, i))->resorigtbl;
		tle->resorigcol = castNode(TargetEntry, list_nth(rte1->subquery->targetList, i))->resorigcol;
		newq1->targetList = lappend(newq1->targetList, tle);
	}

	if (cte->search_clause)
	{
		Expr	   *texpr;

		search_col_rowexpr = make_path_rowexpr(cte, cte->search_clause->search_col_list);
		if (cte->search_clause->search_breadth_first)
		{
			search_col_rowexpr->args = lcons(makeConst(INT8OID, -1, InvalidOid, sizeof(int64),
													   Int64GetDatum(0), false, FLOAT8PASSBYVAL),
											 search_col_rowexpr->args);
			search_col_rowexpr->colnames = lcons(makeString("*DEPTH*"), search_col_rowexpr->colnames);
			texpr = (Expr *) search_col_rowexpr;
		}
		else
			texpr = make_path_initial_array(search_col_rowexpr);
		tle = makeTargetEntry(texpr,
							  list_length(newq1->targetList) + 1,
							  cte->search_clause->search_seq_column,
							  false);
		newq1->targetList = lappend(newq1->targetList, tle);
	}
	if (cte->cycle_clause)
	{
		tle = makeTargetEntry((Expr *) cte->cycle_clause->cycle_mark_default,
							  list_length(newq1->targetList) + 1,
							  cte->cycle_clause->cycle_mark_column,
							  false);
		newq1->targetList = lappend(newq1->targetList, tle);
		cycle_col_rowexpr = make_path_rowexpr(cte, cte->cycle_clause->cycle_col_list);
		tle = makeTargetEntry(make_path_initial_array(cycle_col_rowexpr),
							  list_length(newq1->targetList) + 1,
							  cte->cycle_clause->cycle_path_column,
							  false);
		newq1->targetList = lappend(newq1->targetList, tle);
	}

	rte1->subquery = newq1;

	if (cte->search_clause)
	{
		rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->search_clause->search_seq_column));
	}
	if (cte->cycle_clause)
	{
		rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
		rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
	}

	/*
	 * Make new right subquery
	 */
	newq2 = makeNode(Query);
	newq2->commandType = CMD_SELECT;
	newq2->canSetTag = true;

	newrte = makeNode(RangeTblEntry);
	newrte->rtekind = RTE_SUBQUERY;
	ewcl = copyObject(cte->ctecolnames);
	if (cte->search_clause)
	{
		ewcl = lappend(ewcl, makeString(cte->search_clause->search_seq_column));
	}
	if (cte->cycle_clause)
	{
		ewcl = lappend(ewcl, makeString(cte->cycle_clause->cycle_mark_column));
		ewcl = lappend(ewcl, makeString(cte->cycle_clause->cycle_path_column));
	}
	newrte->alias = makeAlias("*TROCRN*", ewcl);
	newrte->eref = newrte->alias;

	/*
	 * Find the reference to the recursive CTE in the right UNION subquery's
	 * range table.  We expect it to be two levels up from the UNION subquery
	 * (and must check that to avoid being fooled by sub-WITHs with the same
	 * CTE name).  There will not be more than one such reference, because the
	 * parser would have rejected that (see checkWellFormedRecursion() in
	 * parse_cte.c).  However, the parser doesn't insist that the reference
	 * appear in the UNION subquery's topmost range table, so we might fail to
	 * find it at all.  That's an unimplemented case for the moment.
	 */
	for (int rti = 1; rti <= list_length(rte2->subquery->rtable); rti++)
	{
		RangeTblEntry *e = rt_fetch(rti, rte2->subquery->rtable);

		if (e->rtekind == RTE_CTE &&
			strcmp(cte->ctename, e->ctename) == 0 &&
			e->ctelevelsup == 2)
		{
			cte_rtindex = rti;
			break;
		}
	}
	if (cte_rtindex <= 0)
		ereport(ERROR,
				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
				 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",
						cte->ctename)));

	newsubquery = copyObject(rte2->subquery);
	IncrementVarSublevelsUp((Node *) newsubquery, 1, 1);

	/*
	 * Add extra columns to target list of subquery of right subquery
	 */
	if (cte->search_clause)
	{
		Var		   *var;

		/* ctename.sqc */
		var = makeVar(cte_rtindex, sqc_attno,
					  search_seq_type, -1, InvalidOid, 0);
		tle = makeTargetEntry((Expr *) var,
							  list_length(newsubquery->targetList) + 1,
							  cte->search_clause->search_seq_column,
							  false);
		newsubquery->targetList = lappend(newsubquery->targetList, tle);
	}
	if (cte->cycle_clause)
	{
		Var		   *var;

		/* ctename.cmc */
		var = makeVar(cte_rtindex, cmc_attno,
					  cte->cycle_clause->cycle_mark_type,
					  cte->cycle_clause->cycle_mark_typmod,
					  cte->cycle_clause->cycle_mark_collation, 0);
		tle = makeTargetEntry((Expr *) var,
							  list_length(newsubquery->targetList) + 1,
							  cte->cycle_clause->cycle_mark_column,
							  false);
		newsubquery->targetList = lappend(newsubquery->targetList, tle);

		/* ctename.cpa */
		var = makeVar(cte_rtindex, cpa_attno,
					  RECORDARRAYOID, -1, InvalidOid, 0);
		tle = makeTargetEntry((Expr *) var,
							  list_length(newsubquery->targetList) + 1,
							  cte->cycle_clause->cycle_path_column,
							  false);
		newsubquery->targetList = lappend(newsubquery->targetList, tle);
	}

	newrte->subquery = newsubquery;
	newrte->inFromCl = true;
	newq2->rtable = list_make1(newrte);

	rtr = makeNode(RangeTblRef);
	rtr->rtindex = 1;

	if (cte->cycle_clause)
	{
		Expr	   *expr;

		/*
		 * Add cmc <> cmv condition
		 */
		expr = make_opclause(cte->cycle_clause->cycle_mark_neop, BOOLOID, false,
							 (Expr *) makeVar(1, cmc_attno,
											  cte->cycle_clause->cycle_mark_type,
											  cte->cycle_clause->cycle_mark_typmod,
											  cte->cycle_clause->cycle_mark_collation, 0),
							 (Expr *) cte->cycle_clause->cycle_mark_value,
							 InvalidOid,
							 cte->cycle_clause->cycle_mark_collation);

		newq2->jointree = makeFromExpr(list_make1(rtr), (Node *) expr);
	}
	else
		newq2->jointree = makeFromExpr(list_make1(rtr), NULL);

	/*
	 * Make target list
	 */
	for (int i = 0; i < list_length(cte->ctecolnames); i++)
	{
		Var		   *var;

		var = makeVar(1, i + 1,
					  list_nth_oid(cte->ctecoltypes, i),
					  list_nth_int(cte->ctecoltypmods, i),
					  list_nth_oid(cte->ctecolcollations, i),
					  0);
		tle = makeTargetEntry((Expr *) var, i + 1, strVal(list_nth(cte->ctecolnames, i)), false);
		tle->resorigtbl = castNode(TargetEntry, list_nth(rte2->subquery->targetList, i))->resorigtbl;
		tle->resorigcol = castNode(TargetEntry, list_nth(rte2->subquery->targetList, i))->resorigcol;
		newq2->targetList = lappend(newq2->targetList, tle);
	}

	if (cte->search_clause)
	{
		Expr	   *texpr;

		if (cte->search_clause->search_breadth_first)
		{
			FieldSelect *fs;
			FuncExpr   *fexpr;

			/*
			 * ROW(sqc.depth + 1, cols)
			 */

			search_col_rowexpr = copyObject(search_col_rowexpr);

			fs = makeNode(FieldSelect);
			fs->arg = (Expr *) makeVar(1, sqc_attno, RECORDOID, -1, 0, 0);
			fs->fieldnum = 1;
			fs->resulttype = INT8OID;
			fs->resulttypmod = -1;

			fexpr = makeFuncExpr(F_INT8INC, INT8OID, list_make1(fs), InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);

			lfirst(list_head(search_col_rowexpr->args)) = fexpr;

			texpr = (Expr *) search_col_rowexpr;
		}
		else
		{
			/*
			 * sqc || ARRAY[ROW(cols)]
			 */
			texpr = make_path_cat_expr(search_col_rowexpr, sqc_attno);
		}
		tle = makeTargetEntry(texpr,
							  list_length(newq2->targetList) + 1,
							  cte->search_clause->search_seq_column,
							  false);
		newq2->targetList = lappend(newq2->targetList, tle);
	}

	if (cte->cycle_clause)
	{
		ScalarArrayOpExpr *saoe;
		CaseExpr   *caseexpr;
		CaseWhen   *casewhen;

		/*
		 * CASE WHEN ROW(cols) = ANY (ARRAY[cpa]) THEN cmv ELSE cmd END
		 */

		saoe = makeNode(ScalarArrayOpExpr);
		saoe->location = -1;
		saoe->opno = RECORD_EQ_OP;
		saoe->useOr = true;
		saoe->args = list_make2(cycle_col_rowexpr,
								makeVar(1, cpa_attno, RECORDARRAYOID, -1, 0, 0));

		caseexpr = makeNode(CaseExpr);
		caseexpr->location = -1;
		caseexpr->casetype = cte->cycle_clause->cycle_mark_type;
		caseexpr->casecollid = cte->cycle_clause->cycle_mark_collation;
		casewhen = makeNode(CaseWhen);
		casewhen->location = -1;
		casewhen->expr = (Expr *) saoe;
		casewhen->result = (Expr *) cte->cycle_clause->cycle_mark_value;
		caseexpr->args = list_make1(casewhen);
		caseexpr->defresult = (Expr *) cte->cycle_clause->cycle_mark_default;

		tle = makeTargetEntry((Expr *) caseexpr,
							  list_length(newq2->targetList) + 1,
							  cte->cycle_clause->cycle_mark_column,
							  false);
		newq2->targetList = lappend(newq2->targetList, tle);

		/*
		 * cpa || ARRAY[ROW(cols)]
		 */
		tle = makeTargetEntry(make_path_cat_expr(cycle_col_rowexpr, cpa_attno),
							  list_length(newq2->targetList) + 1,
							  cte->cycle_clause->cycle_path_column,
							  false);
		newq2->targetList = lappend(newq2->targetList, tle);
	}

	rte2->subquery = newq2;

	if (cte->search_clause)
	{
		rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->search_clause->search_seq_column));
	}
	if (cte->cycle_clause)
	{
		rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
		rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
	}

	/*
	 * Add the additional columns to the SetOperationStmt
	 */
	if (cte->search_clause)
	{
		sos->colTypes = lappend_oid(sos->colTypes, search_seq_type);
		sos->colTypmods = lappend_int(sos->colTypmods, -1);
		sos->colCollations = lappend_oid(sos->colCollations, InvalidOid);
		if (!sos->all)
			sos->groupClauses = lappend(sos->groupClauses,
										makeSortGroupClauseForSetOp(search_seq_type, true));
	}
	if (cte->cycle_clause)
	{
		sos->colTypes = lappend_oid(sos->colTypes, cte->cycle_clause->cycle_mark_type);
		sos->colTypmods = lappend_int(sos->colTypmods, cte->cycle_clause->cycle_mark_typmod);
		sos->colCollations = lappend_oid(sos->colCollations, cte->cycle_clause->cycle_mark_collation);
		if (!sos->all)
			sos->groupClauses = lappend(sos->groupClauses,
										makeSortGroupClauseForSetOp(cte->cycle_clause->cycle_mark_type, true));

		sos->colTypes = lappend_oid(sos->colTypes, RECORDARRAYOID);
		sos->colTypmods = lappend_int(sos->colTypmods, -1);
		sos->colCollations = lappend_oid(sos->colCollations, InvalidOid);
		if (!sos->all)
			sos->groupClauses = lappend(sos->groupClauses,
										makeSortGroupClauseForSetOp(RECORDARRAYOID, true));
	}

	/*
	 * Add the additional columns to the CTE query's target list
	 */
	if (cte->search_clause)
	{
		ctequery->targetList = lappend(ctequery->targetList,
									   makeTargetEntry((Expr *) makeVar(1, sqc_attno,
																		search_seq_type, -1, InvalidOid, 0),
													   list_length(ctequery->targetList) + 1,
													   cte->search_clause->search_seq_column,
													   false));
	}
	if (cte->cycle_clause)
	{
		ctequery->targetList = lappend(ctequery->targetList,
									   makeTargetEntry((Expr *) makeVar(1, cmc_attno,
																		cte->cycle_clause->cycle_mark_type,
																		cte->cycle_clause->cycle_mark_typmod,
																		cte->cycle_clause->cycle_mark_collation, 0),
													   list_length(ctequery->targetList) + 1,
													   cte->cycle_clause->cycle_mark_column,
													   false));
		ctequery->targetList = lappend(ctequery->targetList,
									   makeTargetEntry((Expr *) makeVar(1, cpa_attno,
																		RECORDARRAYOID, -1, InvalidOid, 0),
													   list_length(ctequery->targetList) + 1,
													   cte->cycle_clause->cycle_path_column,
													   false));
	}

	/*
	 * Add the additional columns to the CTE's output columns
	 */
	cte->ctecolnames = ewcl;
	if (cte->search_clause)
	{
		cte->ctecoltypes = lappend_oid(cte->ctecoltypes, search_seq_type);
		cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, -1);
		cte->ctecolcollations = lappend_oid(cte->ctecolcollations, InvalidOid);
	}
	if (cte->cycle_clause)
	{
		cte->ctecoltypes = lappend_oid(cte->ctecoltypes, cte->cycle_clause->cycle_mark_type);
		cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, cte->cycle_clause->cycle_mark_typmod);
		cte->ctecolcollations = lappend_oid(cte->ctecolcollations, cte->cycle_clause->cycle_mark_collation);

		cte->ctecoltypes = lappend_oid(cte->ctecoltypes, RECORDARRAYOID);
		cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, -1);
		cte->ctecolcollations = lappend_oid(cte->ctecolcollations, InvalidOid);
	}

	return cte;
}