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 : 124 : make_path_rowexpr(const CommonTableExpr *cte, const List *col_list)
118 : : {
119 : : RowExpr *rowexpr;
120 : : ListCell *lc;
121 : :
122 : 124 : rowexpr = makeNode(RowExpr);
123 : 124 : rowexpr->row_typeid = RECORDOID;
124 : 124 : rowexpr->row_format = COERCE_IMPLICIT_CAST;
125 : 124 : rowexpr->location = -1;
126 : :
127 [ + - + + : 316 : foreach(lc, col_list)
+ + ]
128 : : {
129 : 192 : char *colname = strVal(lfirst(lc));
130 : :
131 [ + - ]: 260 : for (int i = 0; i < list_length(cte->ctecolnames); i++)
132 : : {
133 : 260 : char *colname2 = strVal(list_nth(cte->ctecolnames, i));
134 : :
135 [ + + ]: 260 : if (strcmp(colname, colname2) == 0)
136 : : {
137 : : Var *var;
138 : :
139 : 192 : var = makeVar(1, i + 1,
140 : 192 : list_nth_oid(cte->ctecoltypes, i),
141 : 192 : list_nth_int(cte->ctecoltypmods, i),
142 : 192 : list_nth_oid(cte->ctecolcollations, i),
143 : : 0);
144 : 192 : rowexpr->args = lappend(rowexpr->args, var);
145 : 192 : rowexpr->colnames = lappend(rowexpr->colnames, makeString(colname));
146 : 192 : break;
147 : : }
148 : : }
149 : : }
150 : :
151 : 124 : return rowexpr;
152 : : }
153 : :
154 : : /*
155 : : * Wrap a RowExpr in an ArrayExpr, for the initial search depth first or cycle
156 : : * row.
157 : : */
158 : : static Expr *
159 : 96 : make_path_initial_array(RowExpr *rowexpr)
160 : : {
161 : : ArrayExpr *arr;
162 : :
163 : 96 : arr = makeNode(ArrayExpr);
164 : 96 : arr->array_typeid = RECORDARRAYOID;
165 : 96 : arr->element_typeid = RECORDOID;
166 : 96 : arr->location = -1;
167 : 96 : arr->elements = list_make1(rowexpr);
168 : :
169 : 96 : return (Expr *) arr;
170 : : }
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 : 92 : make_path_cat_expr(RowExpr *rowexpr, AttrNumber path_varattno)
181 : : {
182 : : ArrayExpr *arr;
183 : : FuncExpr *fexpr;
184 : :
185 : 92 : arr = makeNode(ArrayExpr);
186 : 92 : arr->array_typeid = RECORDARRAYOID;
187 : 92 : arr->element_typeid = RECORDOID;
188 : 92 : arr->location = -1;
189 : 92 : arr->elements = list_make1(rowexpr);
190 : :
191 : 184 : fexpr = makeFuncExpr(F_ARRAY_CAT, RECORDARRAYOID,
192 : 92 : list_make2(makeVar(1, path_varattno, RECORDARRAYOID, -1, 0, 0),
193 : : arr),
194 : : InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
195 : :
196 : 92 : return (Expr *) fexpr;
197 : : }
198 : :
199 : : /*
200 : : * The real work happens here.
201 : : */
202 : : CommonTableExpr *
203 : 112 : rewriteSearchAndCycle(CommonTableExpr *cte)
204 : : {
205 : : Query *ctequery;
206 : : SetOperationStmt *sos;
207 : : int rti1,
208 : : rti2;
209 : : RangeTblEntry *rte1,
210 : : *rte2,
211 : : *newrte;
212 : : Query *newq1,
213 : : *newq2;
214 : : Query *newsubquery;
215 : : RangeTblRef *rtr;
216 : 112 : Oid search_seq_type = InvalidOid;
217 : 112 : AttrNumber sqc_attno = InvalidAttrNumber;
218 : 112 : AttrNumber cmc_attno = InvalidAttrNumber;
219 : 112 : AttrNumber cpa_attno = InvalidAttrNumber;
220 : : TargetEntry *tle;
221 : 112 : RowExpr *cycle_col_rowexpr = NULL;
222 : 112 : RowExpr *search_col_rowexpr = NULL;
223 : : List *ewcl,
224 : : *junk_tles,
225 : : *nonjunk_tles;
226 : : ListCell *lc;
227 : 112 : int cte_rtindex = -1;
228 : :
229 : : Assert(cte->search_clause || cte->cycle_clause);
230 : :
231 : : /*
232 : : * Copy the whole CTE structure, so we can scribble on it freely.
233 : : */
234 : 112 : cte = copyObject(cte);
235 : :
236 : 112 : ctequery = castNode(Query, cte->ctequery);
237 : :
238 : : /*
239 : : * The top level of the CTE's query should be a UNION. Find the two
240 : : * subqueries.
241 : : */
242 : : Assert(ctequery->setOperations);
243 : 112 : sos = castNode(SetOperationStmt, ctequery->setOperations);
244 : : Assert(sos->op == SETOP_UNION);
245 : :
246 : 112 : rti1 = castNode(RangeTblRef, sos->larg)->rtindex;
247 : 112 : rti2 = castNode(RangeTblRef, sos->rarg)->rtindex;
248 : :
249 : 112 : rte1 = rt_fetch(rti1, ctequery->rtable);
250 : 112 : rte2 = rt_fetch(rti2, ctequery->rtable);
251 : :
252 : : Assert(rte1->rtekind == RTE_SUBQUERY);
253 : : Assert(rte2->rtekind == RTE_SUBQUERY);
254 : :
255 : : /*
256 : : * We'll need this a few times later.
257 : : */
258 [ + + ]: 112 : if (cte->search_clause)
259 : : {
260 [ + + ]: 64 : if (cte->search_clause->search_breadth_first)
261 : 28 : search_seq_type = RECORDOID;
262 : : else
263 : 36 : search_seq_type = RECORDARRAYOID;
264 : : }
265 : :
266 : : /*
267 : : * Attribute numbers of the added columns in the CTE's column list
268 : : */
269 [ + + ]: 112 : if (cte->search_clause)
270 : 64 : sqc_attno = list_length(cte->ctecolnames) + 1;
271 [ + + ]: 112 : if (cte->cycle_clause)
272 : : {
273 : 60 : cmc_attno = list_length(cte->ctecolnames) + 1;
274 : 60 : cpa_attno = list_length(cte->ctecolnames) + 2;
275 [ + + ]: 60 : if (cte->search_clause)
276 : : {
277 : 12 : cmc_attno++;
278 : 12 : cpa_attno++;
279 : : }
280 : : }
281 : :
282 : : /*
283 : : * Make new left subquery
284 : : */
285 : 112 : newq1 = makeNode(Query);
286 : 112 : newq1->commandType = CMD_SELECT;
287 : 112 : newq1->canSetTag = true;
288 : :
289 : 112 : newrte = makeNode(RangeTblEntry);
290 : 112 : newrte->rtekind = RTE_SUBQUERY;
291 : 112 : newrte->alias = NULL;
292 : 112 : newrte->eref = makeAlias("*TLOCRN*", cte->ctecolnames);
293 : 112 : newsubquery = copyObject(rte1->subquery);
294 : 112 : IncrementVarSublevelsUp((Node *) newsubquery, 1, 1);
295 : 112 : newrte->subquery = newsubquery;
296 : 112 : newrte->inFromCl = true;
297 : 112 : newq1->rtable = list_make1(newrte);
298 : :
299 : 112 : rtr = makeNode(RangeTblRef);
300 : 112 : rtr->rtindex = 1;
301 : 112 : newq1->jointree = makeFromExpr(list_make1(rtr), NULL);
302 : :
303 : : /*
304 : : * Make target list
305 : : */
306 [ + + ]: 344 : for (int i = 0; i < list_length(cte->ctecolnames); i++)
307 : : {
308 : : Var *var;
309 : :
310 : 232 : var = makeVar(1, i + 1,
311 : 232 : list_nth_oid(cte->ctecoltypes, i),
312 : 232 : list_nth_int(cte->ctecoltypmods, i),
313 : 232 : list_nth_oid(cte->ctecolcollations, i),
314 : : 0);
315 : 232 : tle = makeTargetEntry((Expr *) var, i + 1, strVal(list_nth(cte->ctecolnames, i)), false);
316 : 232 : tle->resorigtbl = list_nth_node(TargetEntry, rte1->subquery->targetList, i)->resorigtbl;
317 : 232 : tle->resorigcol = list_nth_node(TargetEntry, rte1->subquery->targetList, i)->resorigcol;
318 : 232 : newq1->targetList = lappend(newq1->targetList, tle);
319 : : }
320 : :
321 [ + + ]: 112 : if (cte->search_clause)
322 : : {
323 : : Expr *texpr;
324 : :
325 : 64 : search_col_rowexpr = make_path_rowexpr(cte, cte->search_clause->search_col_list);
326 [ + + ]: 64 : if (cte->search_clause->search_breadth_first)
327 : : {
328 : 28 : search_col_rowexpr->args = lcons(makeConst(INT8OID, -1, InvalidOid, sizeof(int64),
329 : : Int64GetDatum(0), false, true),
330 : : search_col_rowexpr->args);
331 : 28 : search_col_rowexpr->colnames = lcons(makeString("*DEPTH*"), search_col_rowexpr->colnames);
332 : 28 : texpr = (Expr *) search_col_rowexpr;
333 : : }
334 : : else
335 : 36 : texpr = make_path_initial_array(search_col_rowexpr);
336 : 128 : tle = makeTargetEntry(texpr,
337 : 64 : list_length(newq1->targetList) + 1,
338 : 64 : cte->search_clause->search_seq_column,
339 : : false);
340 : 64 : newq1->targetList = lappend(newq1->targetList, tle);
341 : : }
342 [ + + ]: 112 : if (cte->cycle_clause)
343 : : {
344 : 120 : tle = makeTargetEntry((Expr *) cte->cycle_clause->cycle_mark_default,
345 : 60 : list_length(newq1->targetList) + 1,
346 : 60 : cte->cycle_clause->cycle_mark_column,
347 : : false);
348 : 60 : newq1->targetList = lappend(newq1->targetList, tle);
349 : 60 : cycle_col_rowexpr = make_path_rowexpr(cte, cte->cycle_clause->cycle_col_list);
350 : 60 : tle = makeTargetEntry(make_path_initial_array(cycle_col_rowexpr),
351 : 60 : list_length(newq1->targetList) + 1,
352 : 60 : cte->cycle_clause->cycle_path_column,
353 : : false);
354 : 60 : newq1->targetList = lappend(newq1->targetList, tle);
355 : : }
356 : :
357 : 112 : rte1->subquery = newq1;
358 : :
359 [ + + ]: 112 : if (cte->search_clause)
360 : : {
361 : 64 : rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->search_clause->search_seq_column));
362 : : }
363 [ + + ]: 112 : if (cte->cycle_clause)
364 : : {
365 : 60 : rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
366 : 60 : rte1->eref->colnames = lappend(rte1->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
367 : : }
368 : :
369 : : /*
370 : : * Make new right subquery
371 : : */
372 : 112 : newq2 = makeNode(Query);
373 : 112 : newq2->commandType = CMD_SELECT;
374 : 112 : newq2->canSetTag = true;
375 : :
376 : 112 : newrte = makeNode(RangeTblEntry);
377 : 112 : newrte->rtekind = RTE_SUBQUERY;
378 : 112 : ewcl = copyObject(cte->ctecolnames);
379 [ + + ]: 112 : if (cte->search_clause)
380 : : {
381 : 64 : ewcl = lappend(ewcl, makeString(cte->search_clause->search_seq_column));
382 : : }
383 [ + + ]: 112 : if (cte->cycle_clause)
384 : : {
385 : 60 : ewcl = lappend(ewcl, makeString(cte->cycle_clause->cycle_mark_column));
386 : 60 : ewcl = lappend(ewcl, makeString(cte->cycle_clause->cycle_path_column));
387 : : }
388 : 112 : newrte->alias = NULL;
389 : 112 : newrte->eref = makeAlias("*TROCRN*", ewcl);
390 : :
391 : : /*
392 : : * Find the reference to the recursive CTE in the right UNION subquery's
393 : : * range table. We expect it to be two levels up from the UNION subquery
394 : : * (and must check that to avoid being fooled by sub-WITHs with the same
395 : : * CTE name). There will not be more than one such reference, because the
396 : : * parser would have rejected that (see checkWellFormedRecursion() in
397 : : * parse_cte.c). However, the parser doesn't insist that the reference
398 : : * appear in the UNION subquery's topmost range table, so we might fail to
399 : : * find it at all. That's an unimplemented case for the moment.
400 : : */
401 [ + + ]: 176 : for (int rti = 1; rti <= list_length(rte2->subquery->rtable); rti++)
402 : : {
403 : 172 : RangeTblEntry *e = rt_fetch(rti, rte2->subquery->rtable);
404 : :
405 [ + + ]: 172 : if (e->rtekind == RTE_CTE &&
406 [ + + ]: 116 : strcmp(cte->ctename, e->ctename) == 0 &&
407 [ + + ]: 112 : e->ctelevelsup == 2)
408 : : {
409 : 108 : cte_rtindex = rti;
410 : 108 : break;
411 : : }
412 : : }
413 [ + + ]: 112 : if (cte_rtindex <= 0)
414 [ + - ]: 4 : ereport(ERROR,
415 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
416 : : 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",
417 : : cte->ctename)));
418 : :
419 : : /*
420 : : * Make a copy of the original subquery, which will become a subquery of
421 : : * newq2, so it needs to be bumped down in level.
422 : : */
423 : 108 : newsubquery = copyObject(rte2->subquery);
424 : 108 : IncrementVarSublevelsUp((Node *) newsubquery, 1, 1);
425 : :
426 : : /*
427 : : * Add extra columns to target list of subquery of right subquery.
428 : : *
429 : : * The subquery's targetlist might contain resjunk entries, such as sort
430 : : * or grouping columns, which are not part of its output. Those must stay
431 : : * after all the non-resjunk entries, both by convention and because the
432 : : * column names we assigned above to the wrapping subquery assume that the
433 : : * added columns immediately follow the original output columns. So
434 : : * detach any resjunk entries before adding the new columns, and re-attach
435 : : * them afterwards with new resnos.
436 : : */
437 : 108 : nonjunk_tles = junk_tles = NIL;
438 [ + - + + : 348 : foreach(lc, newsubquery->targetList)
+ + ]
439 : : {
440 : 240 : tle = lfirst_node(TargetEntry, lc);
441 [ + + ]: 240 : if (tle->resjunk)
442 : 12 : junk_tles = lappend(junk_tles, tle);
443 : : else
444 : 228 : nonjunk_tles = lappend(nonjunk_tles, tle);
445 : : }
446 : : Assert(list_length(nonjunk_tles) == list_length(cte->ctecolnames));
447 : 108 : newsubquery->targetList = nonjunk_tles;
448 : :
449 [ + + ]: 108 : if (cte->search_clause)
450 : : {
451 : : Var *var;
452 : :
453 : : /* ctename.sqc */
454 : 60 : var = makeVar(cte_rtindex, sqc_attno,
455 : : search_seq_type, -1, InvalidOid, 0);
456 : 120 : tle = makeTargetEntry((Expr *) var,
457 : 60 : list_length(newsubquery->targetList) + 1,
458 : 60 : cte->search_clause->search_seq_column,
459 : : false);
460 : 60 : newsubquery->targetList = lappend(newsubquery->targetList, tle);
461 : : }
462 [ + + ]: 108 : if (cte->cycle_clause)
463 : : {
464 : : Var *var;
465 : :
466 : : /* ctename.cmc */
467 : 60 : var = makeVar(cte_rtindex, cmc_attno,
468 : 60 : cte->cycle_clause->cycle_mark_type,
469 : 60 : cte->cycle_clause->cycle_mark_typmod,
470 : 60 : cte->cycle_clause->cycle_mark_collation, 0);
471 : 120 : tle = makeTargetEntry((Expr *) var,
472 : 60 : list_length(newsubquery->targetList) + 1,
473 : 60 : cte->cycle_clause->cycle_mark_column,
474 : : false);
475 : 60 : newsubquery->targetList = lappend(newsubquery->targetList, tle);
476 : :
477 : : /* ctename.cpa */
478 : 60 : var = makeVar(cte_rtindex, cpa_attno,
479 : : RECORDARRAYOID, -1, InvalidOid, 0);
480 : 120 : tle = makeTargetEntry((Expr *) var,
481 : 60 : list_length(newsubquery->targetList) + 1,
482 : 60 : cte->cycle_clause->cycle_path_column,
483 : : false);
484 : 60 : newsubquery->targetList = lappend(newsubquery->targetList, tle);
485 : : }
486 : :
487 : : /* Re-attach the resjunk entries, if any, after the new columns */
488 [ + + + + : 120 : foreach(lc, junk_tles)
+ + ]
489 : : {
490 : 12 : tle = lfirst_node(TargetEntry, lc);
491 : 12 : tle->resno = list_length(newsubquery->targetList) + 1;
492 : 12 : newsubquery->targetList = lappend(newsubquery->targetList, tle);
493 : : }
494 : :
495 : : /*
496 : : * If the subquery had SELECT DISTINCT, rather than adding the new columns
497 : : * to the DISTINCT semantics, let's just mark it DISTINCT ON. (The new
498 : : * columns should be immutable functions of the regular columns, or at
499 : : * worst stable functions, so this doesn't break any semantics.)
500 : : */
501 [ + + ]: 108 : if (newsubquery->distinctClause)
502 : 4 : newsubquery->hasDistinctOn = true;
503 : :
504 : 108 : newrte->subquery = newsubquery;
505 : 108 : newrte->inFromCl = true;
506 : 108 : newq2->rtable = list_make1(newrte);
507 : :
508 : 108 : rtr = makeNode(RangeTblRef);
509 : 108 : rtr->rtindex = 1;
510 : :
511 [ + + ]: 108 : if (cte->cycle_clause)
512 : : {
513 : : Expr *expr;
514 : :
515 : : /*
516 : : * Add cmc <> cmv condition
517 : : */
518 : 60 : expr = make_opclause(cte->cycle_clause->cycle_mark_neop, BOOLOID, false,
519 : 60 : (Expr *) makeVar(1, cmc_attno,
520 : 60 : cte->cycle_clause->cycle_mark_type,
521 : 60 : cte->cycle_clause->cycle_mark_typmod,
522 : 60 : cte->cycle_clause->cycle_mark_collation, 0),
523 : 60 : (Expr *) cte->cycle_clause->cycle_mark_value,
524 : : InvalidOid,
525 : 60 : cte->cycle_clause->cycle_mark_collation);
526 : :
527 : 60 : newq2->jointree = makeFromExpr(list_make1(rtr), (Node *) expr);
528 : : }
529 : : else
530 : 48 : newq2->jointree = makeFromExpr(list_make1(rtr), NULL);
531 : :
532 : : /*
533 : : * Make target list
534 : : */
535 [ + + ]: 336 : for (int i = 0; i < list_length(cte->ctecolnames); i++)
536 : : {
537 : : Var *var;
538 : :
539 : 228 : var = makeVar(1, i + 1,
540 : 228 : list_nth_oid(cte->ctecoltypes, i),
541 : 228 : list_nth_int(cte->ctecoltypmods, i),
542 : 228 : list_nth_oid(cte->ctecolcollations, i),
543 : : 0);
544 : 228 : tle = makeTargetEntry((Expr *) var, i + 1, strVal(list_nth(cte->ctecolnames, i)), false);
545 : 228 : tle->resorigtbl = list_nth_node(TargetEntry, rte2->subquery->targetList, i)->resorigtbl;
546 : 228 : tle->resorigcol = list_nth_node(TargetEntry, rte2->subquery->targetList, i)->resorigcol;
547 : 228 : newq2->targetList = lappend(newq2->targetList, tle);
548 : : }
549 : :
550 [ + + ]: 108 : if (cte->search_clause)
551 : : {
552 : : Expr *texpr;
553 : :
554 [ + + ]: 60 : if (cte->search_clause->search_breadth_first)
555 : : {
556 : : FieldSelect *fs;
557 : : FuncExpr *fexpr;
558 : :
559 : : /*
560 : : * ROW(sqc.depth + 1, cols)
561 : : */
562 : :
563 : 28 : search_col_rowexpr = copyObject(search_col_rowexpr);
564 : :
565 : 28 : fs = makeNode(FieldSelect);
566 : 28 : fs->arg = (Expr *) makeVar(1, sqc_attno, RECORDOID, -1, 0, 0);
567 : 28 : fs->fieldnum = 1;
568 : 28 : fs->resulttype = INT8OID;
569 : 28 : fs->resulttypmod = -1;
570 : :
571 : 28 : fexpr = makeFuncExpr(F_INT8INC, INT8OID, list_make1(fs), InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
572 : :
573 : 28 : linitial(search_col_rowexpr->args) = fexpr;
574 : :
575 : 28 : texpr = (Expr *) search_col_rowexpr;
576 : : }
577 : : else
578 : : {
579 : : /*
580 : : * sqc || ARRAY[ROW(cols)]
581 : : */
582 : 32 : texpr = make_path_cat_expr(search_col_rowexpr, sqc_attno);
583 : : }
584 : 120 : tle = makeTargetEntry(texpr,
585 : 60 : list_length(newq2->targetList) + 1,
586 : 60 : cte->search_clause->search_seq_column,
587 : : false);
588 : 60 : newq2->targetList = lappend(newq2->targetList, tle);
589 : : }
590 : :
591 [ + + ]: 108 : if (cte->cycle_clause)
592 : : {
593 : : ScalarArrayOpExpr *saoe;
594 : : CaseExpr *caseexpr;
595 : : CaseWhen *casewhen;
596 : :
597 : : /*
598 : : * CASE WHEN ROW(cols) = ANY (ARRAY[cpa]) THEN cmv ELSE cmd END
599 : : */
600 : :
601 : 60 : saoe = makeNode(ScalarArrayOpExpr);
602 : 60 : saoe->location = -1;
603 : 60 : saoe->opno = RECORD_EQ_OP;
604 : 60 : saoe->useOr = true;
605 : 60 : saoe->args = list_make2(cycle_col_rowexpr,
606 : : makeVar(1, cpa_attno, RECORDARRAYOID, -1, 0, 0));
607 : :
608 : 60 : caseexpr = makeNode(CaseExpr);
609 : 60 : caseexpr->location = -1;
610 : 60 : caseexpr->casetype = cte->cycle_clause->cycle_mark_type;
611 : 60 : caseexpr->casecollid = cte->cycle_clause->cycle_mark_collation;
612 : 60 : casewhen = makeNode(CaseWhen);
613 : 60 : casewhen->location = -1;
614 : 60 : casewhen->expr = (Expr *) saoe;
615 : 60 : casewhen->result = (Expr *) cte->cycle_clause->cycle_mark_value;
616 : 60 : caseexpr->args = list_make1(casewhen);
617 : 60 : caseexpr->defresult = (Expr *) cte->cycle_clause->cycle_mark_default;
618 : :
619 : 120 : tle = makeTargetEntry((Expr *) caseexpr,
620 : 60 : list_length(newq2->targetList) + 1,
621 : 60 : cte->cycle_clause->cycle_mark_column,
622 : : false);
623 : 60 : newq2->targetList = lappend(newq2->targetList, tle);
624 : :
625 : : /*
626 : : * cpa || ARRAY[ROW(cols)]
627 : : */
628 : 60 : tle = makeTargetEntry(make_path_cat_expr(cycle_col_rowexpr, cpa_attno),
629 : 60 : list_length(newq2->targetList) + 1,
630 : 60 : cte->cycle_clause->cycle_path_column,
631 : : false);
632 : 60 : newq2->targetList = lappend(newq2->targetList, tle);
633 : : }
634 : :
635 : 108 : rte2->subquery = newq2;
636 : :
637 [ + + ]: 108 : if (cte->search_clause)
638 : : {
639 : 60 : rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->search_clause->search_seq_column));
640 : : }
641 [ + + ]: 108 : if (cte->cycle_clause)
642 : : {
643 : 60 : rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->cycle_clause->cycle_mark_column));
644 : 60 : rte2->eref->colnames = lappend(rte2->eref->colnames, makeString(cte->cycle_clause->cycle_path_column));
645 : : }
646 : :
647 : : /*
648 : : * Add the additional columns to the SetOperationStmt
649 : : */
650 [ + + ]: 108 : if (cte->search_clause)
651 : : {
652 : 60 : sos->colTypes = lappend_oid(sos->colTypes, search_seq_type);
653 : 60 : sos->colTypmods = lappend_int(sos->colTypmods, -1);
654 : 60 : sos->colCollations = lappend_oid(sos->colCollations, InvalidOid);
655 [ + + ]: 60 : if (!sos->all)
656 : 8 : sos->groupClauses = lappend(sos->groupClauses,
657 : 8 : makeSortGroupClauseForSetOp(search_seq_type, true));
658 : : }
659 [ + + ]: 108 : if (cte->cycle_clause)
660 : : {
661 : 60 : sos->colTypes = lappend_oid(sos->colTypes, cte->cycle_clause->cycle_mark_type);
662 : 60 : sos->colTypmods = lappend_int(sos->colTypmods, cte->cycle_clause->cycle_mark_typmod);
663 : 60 : sos->colCollations = lappend_oid(sos->colCollations, cte->cycle_clause->cycle_mark_collation);
664 [ + + ]: 60 : if (!sos->all)
665 : 4 : sos->groupClauses = lappend(sos->groupClauses,
666 : 4 : makeSortGroupClauseForSetOp(cte->cycle_clause->cycle_mark_type, true));
667 : :
668 : 60 : sos->colTypes = lappend_oid(sos->colTypes, RECORDARRAYOID);
669 : 60 : sos->colTypmods = lappend_int(sos->colTypmods, -1);
670 : 60 : sos->colCollations = lappend_oid(sos->colCollations, InvalidOid);
671 [ + + ]: 60 : if (!sos->all)
672 : 4 : sos->groupClauses = lappend(sos->groupClauses,
673 : 4 : makeSortGroupClauseForSetOp(RECORDARRAYOID, true));
674 : : }
675 : :
676 : : /*
677 : : * Add the additional columns to the CTE query's target list
678 : : */
679 [ + + ]: 108 : if (cte->search_clause)
680 : : {
681 : 60 : ctequery->targetList = lappend(ctequery->targetList,
682 : 60 : makeTargetEntry((Expr *) makeVar(1, sqc_attno,
683 : : search_seq_type, -1, InvalidOid, 0),
684 : 60 : list_length(ctequery->targetList) + 1,
685 : 60 : cte->search_clause->search_seq_column,
686 : : false));
687 : : }
688 [ + + ]: 108 : if (cte->cycle_clause)
689 : : {
690 : 60 : ctequery->targetList = lappend(ctequery->targetList,
691 : 60 : makeTargetEntry((Expr *) makeVar(1, cmc_attno,
692 : 60 : cte->cycle_clause->cycle_mark_type,
693 : 60 : cte->cycle_clause->cycle_mark_typmod,
694 : 60 : cte->cycle_clause->cycle_mark_collation, 0),
695 : 60 : list_length(ctequery->targetList) + 1,
696 : 60 : cte->cycle_clause->cycle_mark_column,
697 : : false));
698 : 60 : ctequery->targetList = lappend(ctequery->targetList,
699 : 60 : makeTargetEntry((Expr *) makeVar(1, cpa_attno,
700 : : RECORDARRAYOID, -1, InvalidOid, 0),
701 : 60 : list_length(ctequery->targetList) + 1,
702 : 60 : cte->cycle_clause->cycle_path_column,
703 : : false));
704 : : }
705 : :
706 : : /*
707 : : * Add the additional columns to the CTE's output columns
708 : : */
709 : 108 : cte->ctecolnames = ewcl;
710 [ + + ]: 108 : if (cte->search_clause)
711 : : {
712 : 60 : cte->ctecoltypes = lappend_oid(cte->ctecoltypes, search_seq_type);
713 : 60 : cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, -1);
714 : 60 : cte->ctecolcollations = lappend_oid(cte->ctecolcollations, InvalidOid);
715 : : }
716 [ + + ]: 108 : if (cte->cycle_clause)
717 : : {
718 : 60 : cte->ctecoltypes = lappend_oid(cte->ctecoltypes, cte->cycle_clause->cycle_mark_type);
719 : 60 : cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, cte->cycle_clause->cycle_mark_typmod);
720 : 60 : cte->ctecolcollations = lappend_oid(cte->ctecolcollations, cte->cycle_clause->cycle_mark_collation);
721 : :
722 : 60 : cte->ctecoltypes = lappend_oid(cte->ctecoltypes, RECORDARRAYOID);
723 : 60 : cte->ctecoltypmods = lappend_int(cte->ctecoltypmods, -1);
724 : 60 : cte->ctecolcollations = lappend_oid(cte->ctecolcollations, InvalidOid);
725 : : }
726 : :
727 : 108 : return cte;
728 : : }
|