204{
207 int rti1,
208 rti2;
210 *rte2,
211 *newrte;
213 *newq2;
221 RowExpr *cycle_col_rowexpr = NULL;
222 RowExpr *search_col_rowexpr = NULL;
224 int cte_rtindex = -1;
225
226 Assert(cte->search_clause || cte->cycle_clause);
227
229
231
232 /*
233 * The top level of the CTE's query should be a UNION. Find the two
234 * subqueries.
235 */
239
242
245
248
249 /*
250 * We'll need this a few times later.
251 */
252 if (cte->search_clause)
253 {
254 if (cte->search_clause->search_breadth_first)
255 search_seq_type = RECORDOID;
256 else
257 search_seq_type = RECORDARRAYOID;
258 }
259
260 /*
261 * Attribute numbers of the added columns in the CTE's column list
262 */
263 if (cte->search_clause)
265 if (cte->cycle_clause)
266 {
269 if (cte->search_clause)
270 {
271 cmc_attno++;
272 cpa_attno++;
273 }
274 }
275
276 /*
277 * Make new left subquery
278 */
281 newq1->canSetTag = true;
282
285 newrte->alias = NULL;
286 newrte->eref =
makeAlias(
"*TLOCRN*", cte->ctecolnames);
290 newrte->inFromCl = true;
292
296
297 /*
298 * Make target list
299 */
301 {
303
308 0);
313 }
314
315 if (cte->search_clause)
316 {
318
319 search_col_rowexpr =
make_path_rowexpr(cte, cte->search_clause->search_col_list);
320 if (cte->search_clause->search_breadth_first)
321 {
324 search_col_rowexpr->
args);
325 search_col_rowexpr->colnames =
lcons(
makeString(
"*DEPTH*"), search_col_rowexpr->colnames);
326 texpr = (
Expr *) search_col_rowexpr;
327 }
328 else
332 cte->search_clause->search_seq_column,
333 false);
335 }
336 if (cte->cycle_clause)
337 {
340 cte->cycle_clause->cycle_mark_column,
341 false);
346 cte->cycle_clause->cycle_path_column,
347 false);
349 }
350
352
353 if (cte->search_clause)
354 {
355 rte1->eref->colnames =
lappend(rte1->eref->colnames,
makeString(cte->search_clause->search_seq_column));
356 }
357 if (cte->cycle_clause)
358 {
359 rte1->eref->colnames =
lappend(rte1->eref->colnames,
makeString(cte->cycle_clause->cycle_mark_column));
360 rte1->eref->colnames =
lappend(rte1->eref->colnames,
makeString(cte->cycle_clause->cycle_path_column));
361 }
362
363 /*
364 * Make new right subquery
365 */
368 newq2->canSetTag = true;
369
373 if (cte->search_clause)
374 {
376 }
377 if (cte->cycle_clause)
378 {
381 }
382 newrte->alias = NULL;
383 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 */
396 {
398
400 strcmp(cte->
ctename,
e->ctename) == 0 &&
402 {
403 cte_rtindex = rti;
404 break;
405 }
406 }
407 if (cte_rtindex <= 0)
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",
412
415
416 /*
417 * Add extra columns to target list of subquery of right subquery
418 */
419 if (cte->search_clause)
420 {
422
423 /* ctename.sqc */
424 var =
makeVar(cte_rtindex, sqc_attno,
428 cte->search_clause->search_seq_column,
429 false);
431 }
432 if (cte->cycle_clause)
433 {
435
436 /* ctename.cmc */
437 var =
makeVar(cte_rtindex, cmc_attno,
438 cte->cycle_clause->cycle_mark_type,
439 cte->cycle_clause->cycle_mark_typmod,
440 cte->cycle_clause->cycle_mark_collation, 0);
443 cte->cycle_clause->cycle_mark_column,
444 false);
446
447 /* ctename.cpa */
448 var =
makeVar(cte_rtindex, cpa_attno,
452 cte->cycle_clause->cycle_path_column,
453 false);
455 }
456
458 newrte->inFromCl = true;
460
463
464 if (cte->cycle_clause)
465 {
467
468 /*
469 * Add cmc <> cmv condition
470 */
471 expr =
make_opclause(cte->cycle_clause->cycle_mark_neop, BOOLOID,
false,
473 cte->cycle_clause->cycle_mark_type,
474 cte->cycle_clause->cycle_mark_typmod,
475 cte->cycle_clause->cycle_mark_collation, 0),
476 (
Expr *) cte->cycle_clause->cycle_mark_value,
478 cte->cycle_clause->cycle_mark_collation);
479
481 }
482 else
484
485 /*
486 * Make target list
487 */
489 {
491
496 0);
501 }
502
503 if (cte->search_clause)
504 {
506
507 if (cte->search_clause->search_breadth_first)
508 {
511
512 /*
513 * ROW(sqc.depth + 1, cols)
514 */
515
516 search_col_rowexpr =
copyObject(search_col_rowexpr);
517
521 fs->resulttype = INT8OID;
522 fs->resulttypmod = -1;
523
525
527
528 texpr = (
Expr *) search_col_rowexpr;
529 }
530 else
531 {
532 /*
533 * sqc || ARRAY[ROW(cols)]
534 */
536 }
539 cte->search_clause->search_seq_column,
540 false);
542 }
543
544 if (cte->cycle_clause)
545 {
549
550 /*
551 * CASE WHEN ROW(cols) = ANY (ARRAY[cpa]) THEN cmv ELSE cmd END
552 */
553
556 saoe->
opno = RECORD_EQ_OP;
559 makeVar(1, cpa_attno, RECORDARRAYOID, -1, 0, 0));
560
563 caseexpr->casetype = cte->cycle_clause->cycle_mark_type;
564 caseexpr->casecollid = cte->cycle_clause->cycle_mark_collation;
568 casewhen->
result = (
Expr *) cte->cycle_clause->cycle_mark_value;
570 caseexpr->
defresult = (
Expr *) cte->cycle_clause->cycle_mark_default;
571
574 cte->cycle_clause->cycle_mark_column,
575 false);
577
578 /*
579 * cpa || ARRAY[ROW(cols)]
580 */
583 cte->cycle_clause->cycle_path_column,
584 false);
586 }
587
589
590 if (cte->search_clause)
591 {
592 rte2->eref->colnames =
lappend(rte2->eref->colnames,
makeString(cte->search_clause->search_seq_column));
593 }
594 if (cte->cycle_clause)
595 {
596 rte2->eref->colnames =
lappend(rte2->eref->colnames,
makeString(cte->cycle_clause->cycle_mark_column));
597 rte2->eref->colnames =
lappend(rte2->eref->colnames,
makeString(cte->cycle_clause->cycle_path_column));
598 }
599
600 /*
601 * Add the additional columns to the SetOperationStmt
602 */
603 if (cte->search_clause)
604 {
605 sos->colTypes =
lappend_oid(sos->colTypes, search_seq_type);
606 sos->colTypmods =
lappend_int(sos->colTypmods, -1);
609 sos->groupClauses =
lappend(sos->groupClauses,
611 }
612 if (cte->cycle_clause)
613 {
614 sos->colTypes =
lappend_oid(sos->colTypes, cte->cycle_clause->cycle_mark_type);
615 sos->colTypmods =
lappend_int(sos->colTypmods, cte->cycle_clause->cycle_mark_typmod);
616 sos->colCollations =
lappend_oid(sos->colCollations, cte->cycle_clause->cycle_mark_collation);
618 sos->groupClauses =
lappend(sos->groupClauses,
620
621 sos->colTypes =
lappend_oid(sos->colTypes, RECORDARRAYOID);
622 sos->colTypmods =
lappend_int(sos->colTypmods, -1);
625 sos->groupClauses =
lappend(sos->groupClauses,
627 }
628
629 /*
630 * Add the additional columns to the CTE query's target list
631 */
632 if (cte->search_clause)
633 {
638 cte->search_clause->search_seq_column,
639 false));
640 }
641 if (cte->cycle_clause)
642 {
645 cte->cycle_clause->cycle_mark_type,
646 cte->cycle_clause->cycle_mark_typmod,
647 cte->cycle_clause->cycle_mark_collation, 0),
649 cte->cycle_clause->cycle_mark_column,
650 false));
655 cte->cycle_clause->cycle_path_column,
656 false));
657 }
658
659 /*
660 * Add the additional columns to the CTE's output columns
661 */
662 cte->ctecolnames = ewcl;
663 if (cte->search_clause)
664 {
665 cte->ctecoltypes =
lappend_oid(cte->ctecoltypes, search_seq_type);
666 cte->ctecoltypmods =
lappend_int(cte->ctecoltypmods, -1);
668 }
669 if (cte->cycle_clause)
670 {
671 cte->ctecoltypes =
lappend_oid(cte->ctecoltypes, cte->cycle_clause->cycle_mark_type);
672 cte->ctecoltypmods =
lappend_int(cte->ctecoltypmods, cte->cycle_clause->cycle_mark_typmod);
673 cte->ctecolcollations =
lappend_oid(cte->ctecolcollations, cte->cycle_clause->cycle_mark_collation);
674
675 cte->ctecoltypes =
lappend_oid(cte->ctecoltypes, RECORDARRAYOID);
676 cte->ctecoltypmods =
lappend_int(cte->ctecoltypmods, -1);
678 }
679
680 return cte;
681}
#define InvalidAttrNumber
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
Assert(PointerIsAligned(start, uint64))
List * lappend_int(List *list, int datum)
List * lappend_oid(List *list, Oid datum)
List * lcons(void *datum, List *list)
Alias * makeAlias(const char *aliasname, List *colnames)
FromExpr * makeFromExpr(List *fromlist, Node *quals)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
#define castNode(_type_, nodeptr)
SortGroupClause * makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
#define rt_fetch(rangetable_index, rangetable)
#define list_nth_node(type, list, n)
static Datum Int64GetDatum(int64 X)
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
static RowExpr * make_path_rowexpr(const CommonTableExpr *cte, const List *col_list)
static Expr * make_path_initial_array(RowExpr *rowexpr)
static Expr * make_path_cat_expr(RowExpr *rowexpr, AttrNumber path_varattno)