1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/rewrite/rewriteManip.c
12 *-------------------------------------------------------------------------
16 #include "catalog/pg_type.h"
17 #include "nodes/makefuncs.h"
18 #include "nodes/nodeFuncs.h"
19 #include "nodes/plannodes.h"
20 #include "nodes/relation.h"
21 #include "parser/parse_coerce.h"
22 #include "parser/parse_relation.h"
23 #include "parser/parsetree.h"
24 #include "rewrite/rewriteManip.h"
30 } contain_aggs_of_level_context;
36 } locate_agg_of_level_context;
41 } locate_windowfunc_context;
43 static bool contain_aggs_of_level_walker(Node *node,
44 contain_aggs_of_level_context *context);
45 static bool locate_agg_of_level_walker(Node *node,
46 locate_agg_of_level_context *context);
47 static bool contain_windowfuncs_walker(Node *node, void *context);
48 static bool locate_windowfunc_walker(Node *node,
49 locate_windowfunc_context *context);
50 static bool checkExprHasSubLink_walker(Node *node, void *context);
51 static Relids offset_relid_set(Relids relids, int offset);
52 static Relids adjust_relid_set(Relids relids, int oldrelid, int newrelid);
56 * contain_aggs_of_level -
57 * Check if an expression contains an aggregate function call of a
58 * specified query level.
60 * The objective of this routine is to detect whether there are aggregates
61 * belonging to the given query level. Aggregates belonging to subqueries
62 * or outer queries do NOT cause a true result. We must recurse into
63 * subqueries to detect outer-reference aggregates that logically belong to
64 * the specified query level.
67 contain_aggs_of_level(Node *node, int levelsup)
69 contain_aggs_of_level_context context;
71 context.sublevels_up = levelsup;
74 * Must be prepared to start with a Query or a bare expression tree; if
75 * it's a Query, we don't want to increment sublevels_up.
77 return query_or_expression_tree_walker(node,
78 contain_aggs_of_level_walker,
84 contain_aggs_of_level_walker(Node *node,
85 contain_aggs_of_level_context *context)
89 if (IsA(node, Aggref))
91 if (((Aggref *) node)->agglevelsup == context->sublevels_up)
92 return true; /* abort the tree traversal and return true */
93 /* else fall through to examine argument */
95 if (IsA(node, GroupingFunc))
97 if (((GroupingFunc *) node)->agglevelsup == context->sublevels_up)
99 /* else fall through to examine argument */
101 if (IsA(node, Query))
103 /* Recurse into subselects */
106 context->sublevels_up++;
107 result = query_tree_walker((Query *) node,
108 contain_aggs_of_level_walker,
109 (void *) context, 0);
110 context->sublevels_up--;
113 return expression_tree_walker(node, contain_aggs_of_level_walker,
118 * locate_agg_of_level -
119 * Find the parse location of any aggregate of the specified query level.
121 * Returns -1 if no such agg is in the querytree, or if they all have
122 * unknown parse location. (The former case is probably caller error,
123 * but we don't bother to distinguish it from the latter case.)
125 * Note: it might seem appropriate to merge this functionality into
126 * contain_aggs_of_level, but that would complicate that function's API.
127 * Currently, the only uses of this function are for error reporting,
128 * and so shaving cycles probably isn't very important.
131 locate_agg_of_level(Node *node, int levelsup)
133 locate_agg_of_level_context context;
135 context.agg_location = -1; /* in case we find nothing */
136 context.sublevels_up = levelsup;
139 * Must be prepared to start with a Query or a bare expression tree; if
140 * it's a Query, we don't want to increment sublevels_up.
142 (void) query_or_expression_tree_walker(node,
143 locate_agg_of_level_walker,
147 return context.agg_location;
151 locate_agg_of_level_walker(Node *node,
152 locate_agg_of_level_context *context)
156 if (IsA(node, Aggref))
158 if (((Aggref *) node)->agglevelsup == context->sublevels_up &&
159 ((Aggref *) node)->location >= 0)
161 context->agg_location = ((Aggref *) node)->location;
162 return true; /* abort the tree traversal and return true */
164 /* else fall through to examine argument */
166 if (IsA(node, GroupingFunc))
168 if (((GroupingFunc *) node)->agglevelsup == context->sublevels_up &&
169 ((GroupingFunc *) node)->location >= 0)
171 context->agg_location = ((GroupingFunc *) node)->location;
172 return true; /* abort the tree traversal and return true */
175 if (IsA(node, Query))
177 /* Recurse into subselects */
180 context->sublevels_up++;
181 result = query_tree_walker((Query *) node,
182 locate_agg_of_level_walker,
183 (void *) context, 0);
184 context->sublevels_up--;
187 return expression_tree_walker(node, locate_agg_of_level_walker,
192 * contain_windowfuncs -
193 * Check if an expression contains a window function call of the
194 * current query level.
197 contain_windowfuncs(Node *node)
200 * Must be prepared to start with a Query or a bare expression tree; if
201 * it's a Query, we don't want to increment sublevels_up.
203 return query_or_expression_tree_walker(node,
204 contain_windowfuncs_walker,
210 contain_windowfuncs_walker(Node *node, void *context)
214 if (IsA(node, WindowFunc))
215 return true; /* abort the tree traversal and return true */
216 /* Mustn't recurse into subselects */
217 return expression_tree_walker(node, contain_windowfuncs_walker,
222 * locate_windowfunc -
223 * Find the parse location of any windowfunc of the current query level.
225 * Returns -1 if no such windowfunc is in the querytree, or if they all have
226 * unknown parse location. (The former case is probably caller error,
227 * but we don't bother to distinguish it from the latter case.)
229 * Note: it might seem appropriate to merge this functionality into
230 * contain_windowfuncs, but that would complicate that function's API.
231 * Currently, the only uses of this function are for error reporting,
232 * and so shaving cycles probably isn't very important.
235 locate_windowfunc(Node *node)
237 locate_windowfunc_context context;
239 context.win_location = -1; /* in case we find nothing */
242 * Must be prepared to start with a Query or a bare expression tree; if
243 * it's a Query, we don't want to increment sublevels_up.
245 (void) query_or_expression_tree_walker(node,
246 locate_windowfunc_walker,
250 return context.win_location;
254 locate_windowfunc_walker(Node *node, locate_windowfunc_context *context)
258 if (IsA(node, WindowFunc))
260 if (((WindowFunc *) node)->location >= 0)
262 context->win_location = ((WindowFunc *) node)->location;
263 return true; /* abort the tree traversal and return true */
265 /* else fall through to examine argument */
267 /* Mustn't recurse into subselects */
268 return expression_tree_walker(node, locate_windowfunc_walker,
273 * checkExprHasSubLink -
274 * Check if an expression contains a SubLink.
277 checkExprHasSubLink(Node *node)
280 * If a Query is passed, examine it --- but we should not recurse into
281 * sub-Queries that are in its rangetable or CTE list.
283 return query_or_expression_tree_walker(node,
284 checkExprHasSubLink_walker,
286 QTW_IGNORE_RC_SUBQUERIES);
290 checkExprHasSubLink_walker(Node *node, void *context)
294 if (IsA(node, SubLink))
295 return true; /* abort the tree traversal and return true */
296 return expression_tree_walker(node, checkExprHasSubLink_walker, context);
300 * Check for MULTIEXPR Param within expression tree
302 * We intentionally don't descend into SubLinks: only Params at the current
303 * query level are of interest.
306 contains_multiexpr_param(Node *node, void *context)
310 if (IsA(node, Param))
312 if (((Param *) node)->paramkind == PARAM_MULTIEXPR)
313 return true; /* abort the tree traversal and return true */
316 return expression_tree_walker(node, contains_multiexpr_param, context);
321 * OffsetVarNodes - adjust Vars when appending one query's RT to another
323 * Find all Var nodes in the given tree with varlevelsup == sublevels_up,
324 * and increment their varno fields (rangetable indexes) by 'offset'.
325 * The varnoold fields are adjusted similarly. Also, adjust other nodes
326 * that contain rangetable indexes, such as RangeTblRef and JoinExpr.
328 * NOTE: although this has the form of a walker, we cheat and modify the
329 * nodes in-place. The given expression tree should have been copied
330 * earlier to ensure that no unwanted side-effects occur!
337 } OffsetVarNodes_context;
340 OffsetVarNodes_walker(Node *node, OffsetVarNodes_context *context)
346 Var *var = (Var *) node;
348 if (var->varlevelsup == context->sublevels_up)
350 var->varno += context->offset;
351 var->varnoold += context->offset;
355 if (IsA(node, CurrentOfExpr))
357 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
359 if (context->sublevels_up == 0)
360 cexpr->cvarno += context->offset;
363 if (IsA(node, RangeTblRef))
365 RangeTblRef *rtr = (RangeTblRef *) node;
367 if (context->sublevels_up == 0)
368 rtr->rtindex += context->offset;
369 /* the subquery itself is visited separately */
372 if (IsA(node, JoinExpr))
374 JoinExpr *j = (JoinExpr *) node;
376 if (j->rtindex && context->sublevels_up == 0)
377 j->rtindex += context->offset;
378 /* fall through to examine children */
380 if (IsA(node, PlaceHolderVar))
382 PlaceHolderVar *phv = (PlaceHolderVar *) node;
384 if (phv->phlevelsup == context->sublevels_up)
386 phv->phrels = offset_relid_set(phv->phrels,
389 /* fall through to examine children */
391 if (IsA(node, AppendRelInfo))
393 AppendRelInfo *appinfo = (AppendRelInfo *) node;
395 if (context->sublevels_up == 0)
397 appinfo->parent_relid += context->offset;
398 appinfo->child_relid += context->offset;
400 /* fall through to examine children */
402 /* Shouldn't need to handle other planner auxiliary nodes here */
403 Assert(!IsA(node, PlanRowMark));
404 Assert(!IsA(node, SpecialJoinInfo));
405 Assert(!IsA(node, PlaceHolderInfo));
406 Assert(!IsA(node, MinMaxAggInfo));
408 if (IsA(node, Query))
410 /* Recurse into subselects */
413 context->sublevels_up++;
414 result = query_tree_walker((Query *) node, OffsetVarNodes_walker,
415 (void *) context, 0);
416 context->sublevels_up--;
419 return expression_tree_walker(node, OffsetVarNodes_walker,
424 OffsetVarNodes(Node *node, int offset, int sublevels_up)
426 OffsetVarNodes_context context;
428 context.offset = offset;
429 context.sublevels_up = sublevels_up;
432 * Must be prepared to start with a Query or a bare expression tree; if
433 * it's a Query, go straight to query_tree_walker to make sure that
434 * sublevels_up doesn't get incremented prematurely.
436 if (node && IsA(node, Query))
438 Query *qry = (Query *) node;
441 * If we are starting at a Query, and sublevels_up is zero, then we
442 * must also fix rangetable indexes in the Query itself --- namely
443 * resultRelation, exclRelIndex and rowMarks entries. sublevels_up
444 * cannot be zero when recursing into a subquery, so there's no need
445 * to have the same logic inside OffsetVarNodes_walker.
447 if (sublevels_up == 0)
451 if (qry->resultRelation)
452 qry->resultRelation += offset;
454 if (qry->onConflict && qry->onConflict->exclRelIndex)
455 qry->onConflict->exclRelIndex += offset;
457 foreach(l, qry->rowMarks)
459 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
464 query_tree_walker(qry, OffsetVarNodes_walker,
465 (void *) &context, 0);
468 OffsetVarNodes_walker(node, &context);
472 offset_relid_set(Relids relids, int offset)
474 Relids result = NULL;
478 while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
479 result = bms_add_member(result, rtindex + offset);
484 * ChangeVarNodes - adjust Var nodes for a specific change of RT index
486 * Find all Var nodes in the given tree belonging to a specific relation
487 * (identified by sublevels_up and rt_index), and change their varno fields
488 * to 'new_index'. The varnoold fields are changed too. Also, adjust other
489 * nodes that contain rangetable indexes, such as RangeTblRef and JoinExpr.
491 * NOTE: although this has the form of a walker, we cheat and modify the
492 * nodes in-place. The given expression tree should have been copied
493 * earlier to ensure that no unwanted side-effects occur!
501 } ChangeVarNodes_context;
504 ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
510 Var *var = (Var *) node;
512 if (var->varlevelsup == context->sublevels_up &&
513 var->varno == context->rt_index)
515 var->varno = context->new_index;
516 var->varnoold = context->new_index;
520 if (IsA(node, CurrentOfExpr))
522 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
524 if (context->sublevels_up == 0 &&
525 cexpr->cvarno == context->rt_index)
526 cexpr->cvarno = context->new_index;
529 if (IsA(node, RangeTblRef))
531 RangeTblRef *rtr = (RangeTblRef *) node;
533 if (context->sublevels_up == 0 &&
534 rtr->rtindex == context->rt_index)
535 rtr->rtindex = context->new_index;
536 /* the subquery itself is visited separately */
539 if (IsA(node, JoinExpr))
541 JoinExpr *j = (JoinExpr *) node;
543 if (context->sublevels_up == 0 &&
544 j->rtindex == context->rt_index)
545 j->rtindex = context->new_index;
546 /* fall through to examine children */
548 if (IsA(node, PlaceHolderVar))
550 PlaceHolderVar *phv = (PlaceHolderVar *) node;
552 if (phv->phlevelsup == context->sublevels_up)
554 phv->phrels = adjust_relid_set(phv->phrels,
558 /* fall through to examine children */
560 if (IsA(node, PlanRowMark))
562 PlanRowMark *rowmark = (PlanRowMark *) node;
564 if (context->sublevels_up == 0)
566 if (rowmark->rti == context->rt_index)
567 rowmark->rti = context->new_index;
568 if (rowmark->prti == context->rt_index)
569 rowmark->prti = context->new_index;
573 if (IsA(node, AppendRelInfo))
575 AppendRelInfo *appinfo = (AppendRelInfo *) node;
577 if (context->sublevels_up == 0)
579 if (appinfo->parent_relid == context->rt_index)
580 appinfo->parent_relid = context->new_index;
581 if (appinfo->child_relid == context->rt_index)
582 appinfo->child_relid = context->new_index;
584 /* fall through to examine children */
586 /* Shouldn't need to handle other planner auxiliary nodes here */
587 Assert(!IsA(node, SpecialJoinInfo));
588 Assert(!IsA(node, PlaceHolderInfo));
589 Assert(!IsA(node, MinMaxAggInfo));
591 if (IsA(node, Query))
593 /* Recurse into subselects */
596 context->sublevels_up++;
597 result = query_tree_walker((Query *) node, ChangeVarNodes_walker,
598 (void *) context, 0);
599 context->sublevels_up--;
602 return expression_tree_walker(node, ChangeVarNodes_walker,
607 ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
609 ChangeVarNodes_context context;
611 context.rt_index = rt_index;
612 context.new_index = new_index;
613 context.sublevels_up = sublevels_up;
616 * Must be prepared to start with a Query or a bare expression tree; if
617 * it's a Query, go straight to query_tree_walker to make sure that
618 * sublevels_up doesn't get incremented prematurely.
620 if (node && IsA(node, Query))
622 Query *qry = (Query *) node;
625 * If we are starting at a Query, and sublevels_up is zero, then we
626 * must also fix rangetable indexes in the Query itself --- namely
627 * resultRelation and rowMarks entries. sublevels_up cannot be zero
628 * when recursing into a subquery, so there's no need to have the same
629 * logic inside ChangeVarNodes_walker.
631 if (sublevels_up == 0)
635 if (qry->resultRelation == rt_index)
636 qry->resultRelation = new_index;
638 /* this is unlikely to ever be used, but ... */
639 if (qry->onConflict && qry->onConflict->exclRelIndex == rt_index)
640 qry->onConflict->exclRelIndex = new_index;
642 foreach(l, qry->rowMarks)
644 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
646 if (rc->rti == rt_index)
650 query_tree_walker(qry, ChangeVarNodes_walker,
651 (void *) &context, 0);
654 ChangeVarNodes_walker(node, &context);
658 * Substitute newrelid for oldrelid in a Relid set
661 adjust_relid_set(Relids relids, int oldrelid, int newrelid)
663 if (bms_is_member(oldrelid, relids))
665 /* Ensure we have a modifiable copy */
666 relids = bms_copy(relids);
667 /* Remove old, add new */
668 relids = bms_del_member(relids, oldrelid);
669 relids = bms_add_member(relids, newrelid);
675 * IncrementVarSublevelsUp - adjust Var nodes when pushing them down in tree
677 * Find all Var nodes in the given tree having varlevelsup >= min_sublevels_up,
678 * and add delta_sublevels_up to their varlevelsup value. This is needed when
679 * an expression that's correct for some nesting level is inserted into a
680 * subquery. Ordinarily the initial call has min_sublevels_up == 0 so that
681 * all Vars are affected. The point of min_sublevels_up is that we can
682 * increment it when we recurse into a sublink, so that local variables in
683 * that sublink are not affected, only outer references to vars that belong
684 * to the expression's original query level or parents thereof.
686 * Likewise for other nodes containing levelsup fields, such as Aggref.
688 * NOTE: although this has the form of a walker, we cheat and modify the
689 * Var nodes in-place. The given expression tree should have been copied
690 * earlier to ensure that no unwanted side-effects occur!
695 int delta_sublevels_up;
696 int min_sublevels_up;
697 } IncrementVarSublevelsUp_context;
700 IncrementVarSublevelsUp_walker(Node *node,
701 IncrementVarSublevelsUp_context *context)
707 Var *var = (Var *) node;
709 if (var->varlevelsup >= context->min_sublevels_up)
710 var->varlevelsup += context->delta_sublevels_up;
711 return false; /* done here */
713 if (IsA(node, CurrentOfExpr))
715 /* this should not happen */
716 if (context->min_sublevels_up == 0)
717 elog(ERROR, "cannot push down CurrentOfExpr");
720 if (IsA(node, Aggref))
722 Aggref *agg = (Aggref *) node;
724 if (agg->agglevelsup >= context->min_sublevels_up)
725 agg->agglevelsup += context->delta_sublevels_up;
726 /* fall through to recurse into argument */
728 if (IsA(node, GroupingFunc))
730 GroupingFunc *grp = (GroupingFunc *) node;
732 if (grp->agglevelsup >= context->min_sublevels_up)
733 grp->agglevelsup += context->delta_sublevels_up;
734 /* fall through to recurse into argument */
736 if (IsA(node, PlaceHolderVar))
738 PlaceHolderVar *phv = (PlaceHolderVar *) node;
740 if (phv->phlevelsup >= context->min_sublevels_up)
741 phv->phlevelsup += context->delta_sublevels_up;
742 /* fall through to recurse into argument */
744 if (IsA(node, RangeTblEntry))
746 RangeTblEntry *rte = (RangeTblEntry *) node;
748 if (rte->rtekind == RTE_CTE)
750 if (rte->ctelevelsup >= context->min_sublevels_up)
751 rte->ctelevelsup += context->delta_sublevels_up;
753 return false; /* allow range_table_walker to continue */
755 if (IsA(node, Query))
757 /* Recurse into subselects */
760 context->min_sublevels_up++;
761 result = query_tree_walker((Query *) node,
762 IncrementVarSublevelsUp_walker,
764 QTW_EXAMINE_RTES_BEFORE);
765 context->min_sublevels_up--;
768 return expression_tree_walker(node, IncrementVarSublevelsUp_walker,
773 IncrementVarSublevelsUp(Node *node, int delta_sublevels_up,
774 int min_sublevels_up)
776 IncrementVarSublevelsUp_context context;
778 context.delta_sublevels_up = delta_sublevels_up;
779 context.min_sublevels_up = min_sublevels_up;
782 * Must be prepared to start with a Query or a bare expression tree; if
783 * it's a Query, we don't want to increment sublevels_up.
785 query_or_expression_tree_walker(node,
786 IncrementVarSublevelsUp_walker,
788 QTW_EXAMINE_RTES_BEFORE);
792 * IncrementVarSublevelsUp_rtable -
793 * Same as IncrementVarSublevelsUp, but to be invoked on a range table.
796 IncrementVarSublevelsUp_rtable(List *rtable, int delta_sublevels_up,
797 int min_sublevels_up)
799 IncrementVarSublevelsUp_context context;
801 context.delta_sublevels_up = delta_sublevels_up;
802 context.min_sublevels_up = min_sublevels_up;
804 range_table_walker(rtable,
805 IncrementVarSublevelsUp_walker,
807 QTW_EXAMINE_RTES_BEFORE);
812 * rangeTableEntry_used - detect whether an RTE is referenced somewhere
813 * in var nodes or join or setOp trees of a query or expression.
820 } rangeTableEntry_used_context;
823 rangeTableEntry_used_walker(Node *node,
824 rangeTableEntry_used_context *context)
830 Var *var = (Var *) node;
832 if (var->varlevelsup == context->sublevels_up &&
833 var->varno == context->rt_index)
837 if (IsA(node, CurrentOfExpr))
839 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
841 if (context->sublevels_up == 0 &&
842 cexpr->cvarno == context->rt_index)
846 if (IsA(node, RangeTblRef))
848 RangeTblRef *rtr = (RangeTblRef *) node;
850 if (rtr->rtindex == context->rt_index &&
851 context->sublevels_up == 0)
853 /* the subquery itself is visited separately */
856 if (IsA(node, JoinExpr))
858 JoinExpr *j = (JoinExpr *) node;
860 if (j->rtindex == context->rt_index &&
861 context->sublevels_up == 0)
863 /* fall through to examine children */
865 /* Shouldn't need to handle planner auxiliary nodes here */
866 Assert(!IsA(node, PlaceHolderVar));
867 Assert(!IsA(node, PlanRowMark));
868 Assert(!IsA(node, SpecialJoinInfo));
869 Assert(!IsA(node, AppendRelInfo));
870 Assert(!IsA(node, PlaceHolderInfo));
871 Assert(!IsA(node, MinMaxAggInfo));
873 if (IsA(node, Query))
875 /* Recurse into subselects */
878 context->sublevels_up++;
879 result = query_tree_walker((Query *) node, rangeTableEntry_used_walker,
880 (void *) context, 0);
881 context->sublevels_up--;
884 return expression_tree_walker(node, rangeTableEntry_used_walker,
889 rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
891 rangeTableEntry_used_context context;
893 context.rt_index = rt_index;
894 context.sublevels_up = sublevels_up;
897 * Must be prepared to start with a Query or a bare expression tree; if
898 * it's a Query, we don't want to increment sublevels_up.
900 return query_or_expression_tree_walker(node,
901 rangeTableEntry_used_walker,
908 * If the given Query is an INSERT ... SELECT construct, extract and
909 * return the sub-Query node that represents the SELECT part. Otherwise
910 * return the given Query.
912 * If subquery_ptr is not NULL, then *subquery_ptr is set to the location
913 * of the link to the SELECT subquery inside parsetree, or NULL if not an
916 * This is a hack needed because transformations on INSERT ... SELECTs that
917 * appear in rule actions should be applied to the source SELECT, not to the
918 * INSERT part. Perhaps this can be cleaned up with redesigned querytrees.
921 getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
924 RangeTblEntry *selectrte;
928 *subquery_ptr = NULL;
930 if (parsetree == NULL)
932 if (parsetree->commandType != CMD_INSERT)
936 * Currently, this is ONLY applied to rule-action queries, and so we
937 * expect to find the OLD and NEW placeholder entries in the given query.
938 * If they're not there, it must be an INSERT/SELECT in which they've been
939 * pushed down to the SELECT.
941 if (list_length(parsetree->rtable) >= 2 &&
942 strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
944 strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
947 Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
948 if (list_length(parsetree->jointree->fromlist) != 1)
949 elog(ERROR, "expected to find SELECT subquery");
950 rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
951 Assert(IsA(rtr, RangeTblRef));
952 selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
953 selectquery = selectrte->subquery;
954 if (!(selectquery && IsA(selectquery, Query) &&
955 selectquery->commandType == CMD_SELECT))
956 elog(ERROR, "expected to find SELECT subquery");
957 if (list_length(selectquery->rtable) >= 2 &&
958 strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
960 strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
964 *subquery_ptr = &(selectrte->subquery);
967 elog(ERROR, "could not find rule placeholders");
968 return NULL; /* not reached */
973 * Add the given qualifier condition to the query's WHERE clause
976 AddQual(Query *parsetree, Node *qual)
983 if (parsetree->commandType == CMD_UTILITY)
986 * There's noplace to put the qual on a utility statement.
988 * If it's a NOTIFY, silently ignore the qual; this means that the
989 * NOTIFY will execute, whether or not there are any qualifying rows.
990 * While clearly wrong, this is much more useful than refusing to
991 * execute the rule at all, and extra NOTIFY events are harmless for
992 * typical uses of NOTIFY.
994 * If it isn't a NOTIFY, error out, since unconditional execution of
995 * other utility stmts is unlikely to be wanted. (This case is not
996 * currently allowed anyway, but keep the test for safety.)
998 if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
1002 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1003 errmsg("conditional utility statements are not implemented")));
1006 if (parsetree->setOperations != NULL)
1009 * There's noplace to put the qual on a setop statement, either. (This
1010 * could be fixed, but right now the planner simply ignores any qual
1011 * condition on a setop query.)
1014 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1015 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1018 /* INTERSECT want's the original, but we need to copy - Jan */
1019 copy = copyObject(qual);
1021 parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1025 * We had better not have stuck an aggregate into the WHERE clause.
1027 Assert(!contain_aggs_of_level(copy, 0));
1030 * Make sure query is marked correctly if added qual has sublinks. Need
1031 * not search qual when query is already marked.
1033 if (!parsetree->hasSubLinks)
1034 parsetree->hasSubLinks = checkExprHasSubLink(copy);
1039 * Invert the given clause and add it to the WHERE qualifications of the
1040 * given querytree. Inversion means "x IS NOT TRUE", not just "NOT x",
1041 * else we will do the wrong thing when x evaluates to NULL.
1044 AddInvertedQual(Query *parsetree, Node *qual)
1046 BooleanTest *invqual;
1051 /* Need not copy input qual, because AddQual will... */
1052 invqual = makeNode(BooleanTest);
1053 invqual->arg = (Expr *) qual;
1054 invqual->booltesttype = IS_NOT_TRUE;
1055 invqual->location = -1;
1057 AddQual(parsetree, (Node *) invqual);
1062 * replace_rte_variables() finds all Vars in an expression tree
1063 * that reference a particular RTE, and replaces them with substitute
1064 * expressions obtained from a caller-supplied callback function.
1066 * When invoking replace_rte_variables on a portion of a Query, pass the
1067 * address of the containing Query's hasSubLinks field as outer_hasSubLinks.
1068 * Otherwise, pass NULL, but inserting a SubLink into a non-Query expression
1069 * will then cause an error.
1071 * Note: the business with inserted_sublink is needed to update hasSubLinks
1072 * in subqueries when the replacement adds a subquery inside a subquery.
1073 * Messy, isn't it? We do not need to do similar pushups for hasAggs,
1074 * because it isn't possible for this transformation to insert a level-zero
1075 * aggregate reference into a subquery --- it could only insert outer aggs.
1076 * Likewise for hasWindowFuncs.
1078 * Note: usually, we'd not expose the mutator function or context struct
1079 * for a function like this. We do so because callbacks often find it
1080 * convenient to recurse directly to the mutator on sub-expressions of
1081 * what they will return.
1084 replace_rte_variables(Node *node, int target_varno, int sublevels_up,
1085 replace_rte_variables_callback callback,
1087 bool *outer_hasSubLinks)
1090 replace_rte_variables_context context;
1092 context.callback = callback;
1093 context.callback_arg = callback_arg;
1094 context.target_varno = target_varno;
1095 context.sublevels_up = sublevels_up;
1098 * We try to initialize inserted_sublink to true if there is no need to
1099 * detect new sublinks because the query already has some.
1101 if (node && IsA(node, Query))
1102 context.inserted_sublink = ((Query *) node)->hasSubLinks;
1103 else if (outer_hasSubLinks)
1104 context.inserted_sublink = *outer_hasSubLinks;
1106 context.inserted_sublink = false;
1109 * Must be prepared to start with a Query or a bare expression tree; if
1110 * it's a Query, we don't want to increment sublevels_up.
1112 result = query_or_expression_tree_mutator(node,
1113 replace_rte_variables_mutator,
1117 if (context.inserted_sublink)
1119 if (result && IsA(result, Query))
1120 ((Query *) result)->hasSubLinks = true;
1121 else if (outer_hasSubLinks)
1122 *outer_hasSubLinks = true;
1124 elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1131 replace_rte_variables_mutator(Node *node,
1132 replace_rte_variables_context *context)
1138 Var *var = (Var *) node;
1140 if (var->varno == context->target_varno &&
1141 var->varlevelsup == context->sublevels_up)
1143 /* Found a matching variable, make the substitution */
1146 newnode = context->callback(var, context);
1147 /* Detect if we are adding a sublink to query */
1148 if (!context->inserted_sublink)
1149 context->inserted_sublink = checkExprHasSubLink(newnode);
1152 /* otherwise fall through to copy the var normally */
1154 else if (IsA(node, CurrentOfExpr))
1156 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1158 if (cexpr->cvarno == context->target_varno &&
1159 context->sublevels_up == 0)
1162 * We get here if a WHERE CURRENT OF expression turns out to apply
1163 * to a view. Someday we might be able to translate the
1164 * expression to apply to an underlying table of the view, but
1165 * right now it's not implemented.
1168 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1169 errmsg("WHERE CURRENT OF on a view is not implemented")));
1171 /* otherwise fall through to copy the expr normally */
1173 else if (IsA(node, Query))
1175 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1177 bool save_inserted_sublink;
1179 context->sublevels_up++;
1180 save_inserted_sublink = context->inserted_sublink;
1181 context->inserted_sublink = ((Query *) node)->hasSubLinks;
1182 newnode = query_tree_mutator((Query *) node,
1183 replace_rte_variables_mutator,
1186 newnode->hasSubLinks |= context->inserted_sublink;
1187 context->inserted_sublink = save_inserted_sublink;
1188 context->sublevels_up--;
1189 return (Node *) newnode;
1191 return expression_tree_mutator(node, replace_rte_variables_mutator,
1197 * map_variable_attnos() finds all user-column Vars in an expression tree
1198 * that reference a particular RTE, and adjusts their varattnos according
1199 * to the given mapping array (varattno n is replaced by attno_map[n-1]).
1200 * Vars for system columns are not modified.
1202 * A zero in the mapping array represents a dropped column, which should not
1203 * appear in the expression.
1205 * If the expression tree contains a whole-row Var for the target RTE,
1206 * *found_whole_row is set to true. In addition, if to_rowtype is
1207 * not InvalidOid, we replace the Var with a Var of that vartype, inserting
1208 * a ConvertRowtypeExpr to map back to the rowtype expected by the expression.
1209 * (Therefore, to_rowtype had better be a child rowtype of the rowtype of the
1210 * RTE we're changing references to.) Callers that don't provide to_rowtype
1211 * should report an error if *found_row_type is true; we don't do that here
1212 * because we don't know exactly what wording for the error message would
1213 * be most appropriate. The caller will be aware of the context.
1215 * This could be built using replace_rte_variables and a callback function,
1216 * but since we don't ever need to insert sublinks, replace_rte_variables is
1217 * overly complicated.
1222 int target_varno; /* RTE index to search for */
1223 int sublevels_up; /* (current) nesting depth */
1224 const AttrNumber *attno_map; /* map array for user attnos */
1225 int map_length; /* number of entries in attno_map[] */
1226 Oid to_rowtype; /* change whole-row Vars to this type */
1227 bool *found_whole_row; /* output flag */
1228 } map_variable_attnos_context;
1231 map_variable_attnos_mutator(Node *node,
1232 map_variable_attnos_context *context)
1238 Var *var = (Var *) node;
1240 if (var->varno == context->target_varno &&
1241 var->varlevelsup == context->sublevels_up)
1243 /* Found a matching variable, make the substitution */
1244 Var *newvar = (Var *) palloc(sizeof(Var));
1245 int attno = var->varattno;
1247 *newvar = *var; /* initially copy all fields of the Var */
1251 /* user-defined column, replace attno */
1252 if (attno > context->map_length ||
1253 context->attno_map[attno - 1] == 0)
1254 elog(ERROR, "unexpected varattno %d in expression to be mapped",
1256 newvar->varattno = newvar->varoattno = context->attno_map[attno - 1];
1258 else if (attno == 0)
1260 /* whole-row variable, warn caller */
1261 *(context->found_whole_row) = true;
1263 /* If the caller expects us to convert the Var, do so. */
1264 if (OidIsValid(context->to_rowtype) &&
1265 context->to_rowtype != var->vartype)
1267 ConvertRowtypeExpr *r;
1269 /* This certainly won't work for a RECORD variable. */
1270 Assert(var->vartype != RECORDOID);
1272 /* Var itself is changed to the requested type. */
1273 newvar->vartype = context->to_rowtype;
1276 * Add a conversion node on top to convert back to the
1277 * original type expected by the expression.
1279 r = makeNode(ConvertRowtypeExpr);
1280 r->arg = (Expr *) newvar;
1281 r->resulttype = var->vartype;
1282 r->convertformat = COERCE_IMPLICIT_CAST;
1288 return (Node *) newvar;
1290 /* otherwise fall through to copy the var normally */
1292 else if (IsA(node, ConvertRowtypeExpr))
1294 ConvertRowtypeExpr *r = (ConvertRowtypeExpr *) node;
1295 Var *var = (Var *) r->arg;
1298 * If this is coercing a whole-row Var that we need to convert, then
1299 * just convert the Var without adding an extra ConvertRowtypeExpr.
1300 * Effectively we're simplifying var::parenttype::grandparenttype into
1301 * just var::grandparenttype. This avoids building stacks of CREs if
1302 * this function is applied repeatedly.
1304 if (IsA(var, Var) &&
1305 var->varno == context->target_varno &&
1306 var->varlevelsup == context->sublevels_up &&
1307 var->varattno == 0 &&
1308 OidIsValid(context->to_rowtype) &&
1309 context->to_rowtype != var->vartype)
1311 ConvertRowtypeExpr *newnode;
1312 Var *newvar = (Var *) palloc(sizeof(Var));
1314 /* whole-row variable, warn caller */
1315 *(context->found_whole_row) = true;
1317 *newvar = *var; /* initially copy all fields of the Var */
1319 /* This certainly won't work for a RECORD variable. */
1320 Assert(var->vartype != RECORDOID);
1322 /* Var itself is changed to the requested type. */
1323 newvar->vartype = context->to_rowtype;
1325 newnode = (ConvertRowtypeExpr *) palloc(sizeof(ConvertRowtypeExpr));
1326 *newnode = *r; /* initially copy all fields of the CRE */
1327 newnode->arg = (Expr *) newvar;
1329 return (Node *) newnode;
1331 /* otherwise fall through to process the expression normally */
1333 else if (IsA(node, Query))
1335 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1338 context->sublevels_up++;
1339 newnode = query_tree_mutator((Query *) node,
1340 map_variable_attnos_mutator,
1343 context->sublevels_up--;
1344 return (Node *) newnode;
1346 return expression_tree_mutator(node, map_variable_attnos_mutator,
1351 map_variable_attnos(Node *node,
1352 int target_varno, int sublevels_up,
1353 const AttrNumber *attno_map, int map_length,
1354 Oid to_rowtype, bool *found_whole_row)
1356 map_variable_attnos_context context;
1358 context.target_varno = target_varno;
1359 context.sublevels_up = sublevels_up;
1360 context.attno_map = attno_map;
1361 context.map_length = map_length;
1362 context.to_rowtype = to_rowtype;
1363 context.found_whole_row = found_whole_row;
1365 *found_whole_row = false;
1368 * Must be prepared to start with a Query or a bare expression tree; if
1369 * it's a Query, we don't want to increment sublevels_up.
1371 return query_or_expression_tree_mutator(node,
1372 map_variable_attnos_mutator,
1379 * ReplaceVarsFromTargetList - replace Vars with items from a targetlist
1381 * Vars matching target_varno and sublevels_up are replaced by the
1382 * entry with matching resno from targetlist, if there is one.
1384 * If there is no matching resno for such a Var, the action depends on the
1386 * REPLACEVARS_REPORT_ERROR: throw an error
1387 * REPLACEVARS_CHANGE_VARNO: change Var's varno to nomatch_varno
1388 * REPLACEVARS_SUBSTITUTE_NULL: replace Var with a NULL Const of same type
1390 * The caller must also provide target_rte, the RTE describing the target
1391 * relation. This is needed to handle whole-row Vars referencing the target.
1392 * We expand such Vars into RowExpr constructs.
1394 * outer_hasSubLinks works the same as for replace_rte_variables().
1399 RangeTblEntry *target_rte;
1401 ReplaceVarsNoMatchOption nomatch_option;
1403 } ReplaceVarsFromTargetList_context;
1406 ReplaceVarsFromTargetList_callback(Var *var,
1407 replace_rte_variables_context *context)
1409 ReplaceVarsFromTargetList_context *rcon = (ReplaceVarsFromTargetList_context *) context->callback_arg;
1412 if (var->varattno == InvalidAttrNumber)
1414 /* Must expand whole-tuple reference into RowExpr */
1420 * If generating an expansion for a var of a named rowtype (ie, this
1421 * is a plain relation RTE), then we must include dummy items for
1422 * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1423 * omit dropped columns. Either way, attach column names to the
1424 * RowExpr for use of ruleutils.c.
1426 expandRTE(rcon->target_rte,
1427 var->varno, var->varlevelsup, var->location,
1428 (var->vartype != RECORDOID),
1429 &colnames, &fields);
1430 /* Adjust the generated per-field Vars... */
1431 fields = (List *) replace_rte_variables_mutator((Node *) fields,
1433 rowexpr = makeNode(RowExpr);
1434 rowexpr->args = fields;
1435 rowexpr->row_typeid = var->vartype;
1436 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1437 rowexpr->colnames = colnames;
1438 rowexpr->location = var->location;
1440 return (Node *) rowexpr;
1443 /* Normal case referencing one targetlist element */
1444 tle = get_tle_by_resno(rcon->targetlist, var->varattno);
1446 if (tle == NULL || tle->resjunk)
1448 /* Failed to find column in targetlist */
1449 switch (rcon->nomatch_option)
1451 case REPLACEVARS_REPORT_ERROR:
1452 /* fall through, throw error below */
1455 case REPLACEVARS_CHANGE_VARNO:
1456 var = (Var *) copyObject(var);
1457 var->varno = rcon->nomatch_varno;
1458 var->varnoold = rcon->nomatch_varno;
1459 return (Node *) var;
1461 case REPLACEVARS_SUBSTITUTE_NULL:
1464 * If Var is of domain type, we should add a CoerceToDomain
1465 * node, in case there is a NOT NULL domain constraint.
1467 return coerce_to_domain((Node *) makeNullConst(var->vartype,
1473 COERCE_IMPLICIT_CAST,
1477 elog(ERROR, "could not find replacement targetlist entry for attno %d",
1479 return NULL; /* keep compiler quiet */
1483 /* Make a copy of the tlist item to return */
1484 Expr *newnode = copyObject(tle->expr);
1486 /* Must adjust varlevelsup if tlist item is from higher query */
1487 if (var->varlevelsup > 0)
1488 IncrementVarSublevelsUp((Node *) newnode, var->varlevelsup, 0);
1491 * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1492 * and throw error if so. This case could only happen when expanding
1493 * an ON UPDATE rule's NEW variable and the referenced tlist item in
1494 * the original UPDATE command is part of a multiple assignment. There
1495 * seems no practical way to handle such cases without multiple
1496 * evaluation of the multiple assignment's sub-select, which would
1497 * create semantic oddities that users of rules would probably prefer
1498 * not to cope with. So treat it as an unimplemented feature.
1500 if (contains_multiexpr_param((Node *) newnode, NULL))
1502 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1503 errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1505 return (Node *) newnode;
1510 ReplaceVarsFromTargetList(Node *node,
1511 int target_varno, int sublevels_up,
1512 RangeTblEntry *target_rte,
1514 ReplaceVarsNoMatchOption nomatch_option,
1516 bool *outer_hasSubLinks)
1518 ReplaceVarsFromTargetList_context context;
1520 context.target_rte = target_rte;
1521 context.targetlist = targetlist;
1522 context.nomatch_option = nomatch_option;
1523 context.nomatch_varno = nomatch_varno;
1525 return replace_rte_variables(node, target_varno, sublevels_up,
1526 ReplaceVarsFromTargetList_callback,