16 #include "catalog/pg_operator_d.h"
17 #include "catalog/pg_type_d.h"
26 #include "utils/fmgroids.h"
123 rowexpr->row_typeid = RECORDOID;
127 foreach(lc, col_list)
135 if (strcmp(colname, colname2) == 0)
164 arr->array_typeid = RECORDARRAYOID;
165 arr->element_typeid = RECORDOID;
186 arr->array_typeid = RECORDARRAYOID;
187 arr->element_typeid = RECORDOID;
196 return (
Expr *) fexpr;
221 RowExpr *cycle_col_rowexpr = NULL;
222 RowExpr *search_col_rowexpr = NULL;
224 int cte_rtindex = -1;
226 Assert(cte->search_clause || cte->cycle_clause);
252 if (cte->search_clause)
254 if (cte->search_clause->search_breadth_first)
255 search_seq_type = RECORDOID;
257 search_seq_type = RECORDARRAYOID;
263 if (cte->search_clause)
265 if (cte->cycle_clause)
269 if (cte->search_clause)
281 newq1->canSetTag =
true;
285 newrte->alias =
makeAlias(
"*TLOCRN*", cte->ctecolnames);
286 newrte->eref = newrte->alias;
290 newrte->inFromCl =
true;
315 if (cte->search_clause)
319 search_col_rowexpr =
make_path_rowexpr(cte, cte->search_clause->search_col_list);
320 if (cte->search_clause->search_breadth_first)
324 search_col_rowexpr->
args);
325 search_col_rowexpr->colnames =
lcons(
makeString(
"*DEPTH*"), search_col_rowexpr->colnames);
326 texpr = (
Expr *) search_col_rowexpr;
332 cte->search_clause->search_seq_column,
336 if (cte->cycle_clause)
340 cte->cycle_clause->cycle_mark_column,
346 cte->cycle_clause->cycle_path_column,
353 if (cte->search_clause)
355 rte1->eref->colnames =
lappend(rte1->eref->colnames,
makeString(cte->search_clause->search_seq_column));
357 if (cte->cycle_clause)
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));
368 newq2->canSetTag =
true;
373 if (cte->search_clause)
377 if (cte->cycle_clause)
382 newrte->alias =
makeAlias(
"*TROCRN*", ewcl);
383 newrte->eref = newrte->alias;
400 strcmp(cte->
ctename,
e->ctename) == 0 &&
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",
419 if (cte->search_clause)
424 var =
makeVar(cte_rtindex, sqc_attno,
428 cte->search_clause->search_seq_column,
432 if (cte->cycle_clause)
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,
448 var =
makeVar(cte_rtindex, cpa_attno,
452 cte->cycle_clause->cycle_path_column,
458 newrte->inFromCl =
true;
464 if (cte->cycle_clause)
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);
503 if (cte->search_clause)
507 if (cte->search_clause->search_breadth_first)
516 search_col_rowexpr =
copyObject(search_col_rowexpr);
521 fs->resulttype = INT8OID;
522 fs->resulttypmod = -1;
528 texpr = (
Expr *) search_col_rowexpr;
539 cte->search_clause->search_seq_column,
544 if (cte->cycle_clause)
556 saoe->
opno = RECORD_EQ_OP;
559 makeVar(1, cpa_attno, RECORDARRAYOID, -1, 0, 0));
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;
574 cte->cycle_clause->cycle_mark_column,
583 cte->cycle_clause->cycle_path_column,
590 if (cte->search_clause)
592 rte2->eref->colnames =
lappend(rte2->eref->colnames,
makeString(cte->search_clause->search_seq_column));
594 if (cte->cycle_clause)
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));
603 if (cte->search_clause)
605 sos->colTypes =
lappend_oid(sos->colTypes, search_seq_type);
606 sos->colTypmods =
lappend_int(sos->colTypmods, -1);
609 sos->groupClauses =
lappend(sos->groupClauses,
612 if (cte->cycle_clause)
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,
621 sos->colTypes =
lappend_oid(sos->colTypes, RECORDARRAYOID);
622 sos->colTypmods =
lappend_int(sos->colTypmods, -1);
625 sos->groupClauses =
lappend(sos->groupClauses,
632 if (cte->search_clause)
638 cte->search_clause->search_seq_column,
641 if (cte->cycle_clause)
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,
655 cte->cycle_clause->cycle_path_column,
662 cte->ctecolnames = ewcl;
663 if (cte->search_clause)
665 cte->ctecoltypes =
lappend_oid(cte->ctecoltypes, search_seq_type);
666 cte->ctecoltypmods =
lappend_int(cte->ctecoltypmods, -1);
669 if (cte->cycle_clause)
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);
675 cte->ctecoltypes =
lappend_oid(cte->ctecoltypes, RECORDARRAYOID);
676 cte->ctecoltypmods =
lappend_int(cte->ctecoltypmods, -1);
#define InvalidAttrNumber
#define Assert(condition)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
Datum Int64GetDatum(int64 X)
List * lappend(List *list, void *datum)
List * lappend_int(List *list, int datum)
List * lappend_oid(List *list, Oid datum)
List * lcons(void *datum, List *list)
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Alias * makeAlias(const char *aliasname, List *colnames)
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
#define castNode(_type_, nodeptr)
SortGroupClause * makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
#define rt_fetch(rangetable_index, rangetable)
static int list_length(const List *l)
static Oid list_nth_oid(const List *list, int n)
static void * list_nth(const List *list, int n)
#define list_nth_node(type, list, n)
#define list_make2(x1, x2)
static int list_nth_int(const List *list, int n)
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
static Expr * make_path_cat_expr(RowExpr *rowexpr, AttrNumber path_varattno)
static RowExpr * make_path_rowexpr(const CommonTableExpr *cte, const List *col_list)
CommonTableExpr * rewriteSearchAndCycle(CommonTableExpr *cte)
static Expr * make_path_initial_array(RowExpr *rowexpr)
String * makeString(char *str)