1 /*-------------------------------------------------------------------------
5 * Portions Copyright (c) 1996-2015, 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 "optimizer/clauses.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, LateralJoinInfo));
406 Assert(!IsA(node, PlaceHolderInfo));
407 Assert(!IsA(node, MinMaxAggInfo));
409 if (IsA(node, Query))
411 /* Recurse into subselects */
414 context->sublevels_up++;
415 result = query_tree_walker((Query *) node, OffsetVarNodes_walker,
416 (void *) context, 0);
417 context->sublevels_up--;
420 return expression_tree_walker(node, OffsetVarNodes_walker,
425 OffsetVarNodes(Node *node, int offset, int sublevels_up)
427 OffsetVarNodes_context context;
429 context.offset = offset;
430 context.sublevels_up = sublevels_up;
433 * Must be prepared to start with a Query or a bare expression tree; if
434 * it's a Query, go straight to query_tree_walker to make sure that
435 * sublevels_up doesn't get incremented prematurely.
437 if (node && IsA(node, Query))
439 Query *qry = (Query *) node;
442 * If we are starting at a Query, and sublevels_up is zero, then we
443 * must also fix rangetable indexes in the Query itself --- namely
444 * resultRelation, exclRelIndex and rowMarks entries. sublevels_up
445 * cannot be zero when recursing into a subquery, so there's no need
446 * to have the same logic inside OffsetVarNodes_walker.
448 if (sublevels_up == 0)
452 if (qry->resultRelation)
453 qry->resultRelation += offset;
455 if (qry->onConflict && qry->onConflict->exclRelIndex)
456 qry->onConflict->exclRelIndex += offset;
458 foreach(l, qry->rowMarks)
460 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
465 query_tree_walker(qry, OffsetVarNodes_walker,
466 (void *) &context, 0);
469 OffsetVarNodes_walker(node, &context);
473 offset_relid_set(Relids relids, int offset)
475 Relids result = NULL;
479 while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
480 result = bms_add_member(result, rtindex + offset);
485 * ChangeVarNodes - adjust Var nodes for a specific change of RT index
487 * Find all Var nodes in the given tree belonging to a specific relation
488 * (identified by sublevels_up and rt_index), and change their varno fields
489 * to 'new_index'. The varnoold fields are changed too. Also, adjust other
490 * nodes that contain rangetable indexes, such as RangeTblRef and JoinExpr.
492 * NOTE: although this has the form of a walker, we cheat and modify the
493 * nodes in-place. The given expression tree should have been copied
494 * earlier to ensure that no unwanted side-effects occur!
502 } ChangeVarNodes_context;
505 ChangeVarNodes_walker(Node *node, ChangeVarNodes_context *context)
511 Var *var = (Var *) node;
513 if (var->varlevelsup == context->sublevels_up &&
514 var->varno == context->rt_index)
516 var->varno = context->new_index;
517 var->varnoold = context->new_index;
521 if (IsA(node, CurrentOfExpr))
523 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
525 if (context->sublevels_up == 0 &&
526 cexpr->cvarno == context->rt_index)
527 cexpr->cvarno = context->new_index;
530 if (IsA(node, RangeTblRef))
532 RangeTblRef *rtr = (RangeTblRef *) node;
534 if (context->sublevels_up == 0 &&
535 rtr->rtindex == context->rt_index)
536 rtr->rtindex = context->new_index;
537 /* the subquery itself is visited separately */
540 if (IsA(node, JoinExpr))
542 JoinExpr *j = (JoinExpr *) node;
544 if (context->sublevels_up == 0 &&
545 j->rtindex == context->rt_index)
546 j->rtindex = context->new_index;
547 /* fall through to examine children */
549 if (IsA(node, PlaceHolderVar))
551 PlaceHolderVar *phv = (PlaceHolderVar *) node;
553 if (phv->phlevelsup == context->sublevels_up)
555 phv->phrels = adjust_relid_set(phv->phrels,
559 /* fall through to examine children */
561 if (IsA(node, PlanRowMark))
563 PlanRowMark *rowmark = (PlanRowMark *) node;
565 if (context->sublevels_up == 0)
567 if (rowmark->rti == context->rt_index)
568 rowmark->rti = context->new_index;
569 if (rowmark->prti == context->rt_index)
570 rowmark->prti = context->new_index;
574 if (IsA(node, AppendRelInfo))
576 AppendRelInfo *appinfo = (AppendRelInfo *) node;
578 if (context->sublevels_up == 0)
580 if (appinfo->parent_relid == context->rt_index)
581 appinfo->parent_relid = context->new_index;
582 if (appinfo->child_relid == context->rt_index)
583 appinfo->child_relid = context->new_index;
585 /* fall through to examine children */
587 /* Shouldn't need to handle other planner auxiliary nodes here */
588 Assert(!IsA(node, SpecialJoinInfo));
589 Assert(!IsA(node, LateralJoinInfo));
590 Assert(!IsA(node, PlaceHolderInfo));
591 Assert(!IsA(node, MinMaxAggInfo));
593 if (IsA(node, Query))
595 /* Recurse into subselects */
598 context->sublevels_up++;
599 result = query_tree_walker((Query *) node, ChangeVarNodes_walker,
600 (void *) context, 0);
601 context->sublevels_up--;
604 return expression_tree_walker(node, ChangeVarNodes_walker,
609 ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
611 ChangeVarNodes_context context;
613 context.rt_index = rt_index;
614 context.new_index = new_index;
615 context.sublevels_up = sublevels_up;
618 * Must be prepared to start with a Query or a bare expression tree; if
619 * it's a Query, go straight to query_tree_walker to make sure that
620 * sublevels_up doesn't get incremented prematurely.
622 if (node && IsA(node, Query))
624 Query *qry = (Query *) node;
627 * If we are starting at a Query, and sublevels_up is zero, then we
628 * must also fix rangetable indexes in the Query itself --- namely
629 * resultRelation and rowMarks entries. sublevels_up cannot be zero
630 * when recursing into a subquery, so there's no need to have the same
631 * logic inside ChangeVarNodes_walker.
633 if (sublevels_up == 0)
637 if (qry->resultRelation == rt_index)
638 qry->resultRelation = new_index;
640 /* this is unlikely to ever be used, but ... */
641 if (qry->onConflict && qry->onConflict->exclRelIndex == rt_index)
642 qry->onConflict->exclRelIndex = new_index;
644 foreach(l, qry->rowMarks)
646 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
648 if (rc->rti == rt_index)
652 query_tree_walker(qry, ChangeVarNodes_walker,
653 (void *) &context, 0);
656 ChangeVarNodes_walker(node, &context);
660 * Substitute newrelid for oldrelid in a Relid set
663 adjust_relid_set(Relids relids, int oldrelid, int newrelid)
665 if (bms_is_member(oldrelid, relids))
667 /* Ensure we have a modifiable copy */
668 relids = bms_copy(relids);
669 /* Remove old, add new */
670 relids = bms_del_member(relids, oldrelid);
671 relids = bms_add_member(relids, newrelid);
677 * IncrementVarSublevelsUp - adjust Var nodes when pushing them down in tree
679 * Find all Var nodes in the given tree having varlevelsup >= min_sublevels_up,
680 * and add delta_sublevels_up to their varlevelsup value. This is needed when
681 * an expression that's correct for some nesting level is inserted into a
682 * subquery. Ordinarily the initial call has min_sublevels_up == 0 so that
683 * all Vars are affected. The point of min_sublevels_up is that we can
684 * increment it when we recurse into a sublink, so that local variables in
685 * that sublink are not affected, only outer references to vars that belong
686 * to the expression's original query level or parents thereof.
688 * Likewise for other nodes containing levelsup fields, such as Aggref.
690 * NOTE: although this has the form of a walker, we cheat and modify the
691 * Var nodes in-place. The given expression tree should have been copied
692 * earlier to ensure that no unwanted side-effects occur!
697 int delta_sublevels_up;
698 int min_sublevels_up;
699 } IncrementVarSublevelsUp_context;
702 IncrementVarSublevelsUp_walker(Node *node,
703 IncrementVarSublevelsUp_context *context)
709 Var *var = (Var *) node;
711 if (var->varlevelsup >= context->min_sublevels_up)
712 var->varlevelsup += context->delta_sublevels_up;
713 return false; /* done here */
715 if (IsA(node, CurrentOfExpr))
717 /* this should not happen */
718 if (context->min_sublevels_up == 0)
719 elog(ERROR, "cannot push down CurrentOfExpr");
722 if (IsA(node, Aggref))
724 Aggref *agg = (Aggref *) node;
726 if (agg->agglevelsup >= context->min_sublevels_up)
727 agg->agglevelsup += context->delta_sublevels_up;
728 /* fall through to recurse into argument */
730 if (IsA(node, GroupingFunc))
732 GroupingFunc *grp = (GroupingFunc *) node;
734 if (grp->agglevelsup >= context->min_sublevels_up)
735 grp->agglevelsup += context->delta_sublevels_up;
736 /* fall through to recurse into argument */
738 if (IsA(node, PlaceHolderVar))
740 PlaceHolderVar *phv = (PlaceHolderVar *) node;
742 if (phv->phlevelsup >= context->min_sublevels_up)
743 phv->phlevelsup += context->delta_sublevels_up;
744 /* fall through to recurse into argument */
746 if (IsA(node, RangeTblEntry))
748 RangeTblEntry *rte = (RangeTblEntry *) node;
750 if (rte->rtekind == RTE_CTE)
752 if (rte->ctelevelsup >= context->min_sublevels_up)
753 rte->ctelevelsup += context->delta_sublevels_up;
755 return false; /* allow range_table_walker to continue */
757 if (IsA(node, Query))
759 /* Recurse into subselects */
762 context->min_sublevels_up++;
763 result = query_tree_walker((Query *) node,
764 IncrementVarSublevelsUp_walker,
767 context->min_sublevels_up--;
770 return expression_tree_walker(node, IncrementVarSublevelsUp_walker,
775 IncrementVarSublevelsUp(Node *node, int delta_sublevels_up,
776 int min_sublevels_up)
778 IncrementVarSublevelsUp_context context;
780 context.delta_sublevels_up = delta_sublevels_up;
781 context.min_sublevels_up = min_sublevels_up;
784 * Must be prepared to start with a Query or a bare expression tree; if
785 * it's a Query, we don't want to increment sublevels_up.
787 query_or_expression_tree_walker(node,
788 IncrementVarSublevelsUp_walker,
794 * IncrementVarSublevelsUp_rtable -
795 * Same as IncrementVarSublevelsUp, but to be invoked on a range table.
798 IncrementVarSublevelsUp_rtable(List *rtable, int delta_sublevels_up,
799 int min_sublevels_up)
801 IncrementVarSublevelsUp_context context;
803 context.delta_sublevels_up = delta_sublevels_up;
804 context.min_sublevels_up = min_sublevels_up;
806 range_table_walker(rtable,
807 IncrementVarSublevelsUp_walker,
814 * rangeTableEntry_used - detect whether an RTE is referenced somewhere
815 * in var nodes or join or setOp trees of a query or expression.
822 } rangeTableEntry_used_context;
825 rangeTableEntry_used_walker(Node *node,
826 rangeTableEntry_used_context *context)
832 Var *var = (Var *) node;
834 if (var->varlevelsup == context->sublevels_up &&
835 var->varno == context->rt_index)
839 if (IsA(node, CurrentOfExpr))
841 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
843 if (context->sublevels_up == 0 &&
844 cexpr->cvarno == context->rt_index)
848 if (IsA(node, RangeTblRef))
850 RangeTblRef *rtr = (RangeTblRef *) node;
852 if (rtr->rtindex == context->rt_index &&
853 context->sublevels_up == 0)
855 /* the subquery itself is visited separately */
858 if (IsA(node, JoinExpr))
860 JoinExpr *j = (JoinExpr *) node;
862 if (j->rtindex == context->rt_index &&
863 context->sublevels_up == 0)
865 /* fall through to examine children */
867 /* Shouldn't need to handle planner auxiliary nodes here */
868 Assert(!IsA(node, PlaceHolderVar));
869 Assert(!IsA(node, PlanRowMark));
870 Assert(!IsA(node, SpecialJoinInfo));
871 Assert(!IsA(node, LateralJoinInfo));
872 Assert(!IsA(node, AppendRelInfo));
873 Assert(!IsA(node, PlaceHolderInfo));
874 Assert(!IsA(node, MinMaxAggInfo));
876 if (IsA(node, Query))
878 /* Recurse into subselects */
881 context->sublevels_up++;
882 result = query_tree_walker((Query *) node, rangeTableEntry_used_walker,
883 (void *) context, 0);
884 context->sublevels_up--;
887 return expression_tree_walker(node, rangeTableEntry_used_walker,
892 rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
894 rangeTableEntry_used_context context;
896 context.rt_index = rt_index;
897 context.sublevels_up = sublevels_up;
900 * Must be prepared to start with a Query or a bare expression tree; if
901 * it's a Query, we don't want to increment sublevels_up.
903 return query_or_expression_tree_walker(node,
904 rangeTableEntry_used_walker,
911 * If the given Query is an INSERT ... SELECT construct, extract and
912 * return the sub-Query node that represents the SELECT part. Otherwise
913 * return the given Query.
915 * If subquery_ptr is not NULL, then *subquery_ptr is set to the location
916 * of the link to the SELECT subquery inside parsetree, or NULL if not an
919 * This is a hack needed because transformations on INSERT ... SELECTs that
920 * appear in rule actions should be applied to the source SELECT, not to the
921 * INSERT part. Perhaps this can be cleaned up with redesigned querytrees.
924 getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
927 RangeTblEntry *selectrte;
931 *subquery_ptr = NULL;
933 if (parsetree == NULL)
935 if (parsetree->commandType != CMD_INSERT)
939 * Currently, this is ONLY applied to rule-action queries, and so we
940 * expect to find the OLD and NEW placeholder entries in the given query.
941 * If they're not there, it must be an INSERT/SELECT in which they've been
942 * pushed down to the SELECT.
944 if (list_length(parsetree->rtable) >= 2 &&
945 strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
947 strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
950 Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
951 if (list_length(parsetree->jointree->fromlist) != 1)
952 elog(ERROR, "expected to find SELECT subquery");
953 rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
954 Assert(IsA(rtr, RangeTblRef));
955 selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
956 selectquery = selectrte->subquery;
957 if (!(selectquery && IsA(selectquery, Query) &&
958 selectquery->commandType == CMD_SELECT))
959 elog(ERROR, "expected to find SELECT subquery");
960 if (list_length(selectquery->rtable) >= 2 &&
961 strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
963 strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
967 *subquery_ptr = &(selectrte->subquery);
970 elog(ERROR, "could not find rule placeholders");
971 return NULL; /* not reached */
976 * Add the given qualifier condition to the query's WHERE clause
979 AddQual(Query *parsetree, Node *qual)
986 if (parsetree->commandType == CMD_UTILITY)
989 * There's noplace to put the qual on a utility statement.
991 * If it's a NOTIFY, silently ignore the qual; this means that the
992 * NOTIFY will execute, whether or not there are any qualifying rows.
993 * While clearly wrong, this is much more useful than refusing to
994 * execute the rule at all, and extra NOTIFY events are harmless for
995 * typical uses of NOTIFY.
997 * If it isn't a NOTIFY, error out, since unconditional execution of
998 * other utility stmts is unlikely to be wanted. (This case is not
999 * currently allowed anyway, but keep the test for safety.)
1001 if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
1005 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1006 errmsg("conditional utility statements are not implemented")));
1009 if (parsetree->setOperations != NULL)
1012 * There's noplace to put the qual on a setop statement, either. (This
1013 * could be fixed, but right now the planner simply ignores any qual
1014 * condition on a setop query.)
1017 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1018 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
1021 /* INTERSECT want's the original, but we need to copy - Jan */
1022 copy = copyObject(qual);
1024 parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
1028 * We had better not have stuck an aggregate into the WHERE clause.
1030 Assert(!contain_aggs_of_level(copy, 0));
1033 * Make sure query is marked correctly if added qual has sublinks. Need
1034 * not search qual when query is already marked.
1036 if (!parsetree->hasSubLinks)
1037 parsetree->hasSubLinks = checkExprHasSubLink(copy);
1042 * Invert the given clause and add it to the WHERE qualifications of the
1043 * given querytree. Inversion means "x IS NOT TRUE", not just "NOT x",
1044 * else we will do the wrong thing when x evaluates to NULL.
1047 AddInvertedQual(Query *parsetree, Node *qual)
1049 BooleanTest *invqual;
1054 /* Need not copy input qual, because AddQual will... */
1055 invqual = makeNode(BooleanTest);
1056 invqual->arg = (Expr *) qual;
1057 invqual->booltesttype = IS_NOT_TRUE;
1058 invqual->location = -1;
1060 AddQual(parsetree, (Node *) invqual);
1065 * replace_rte_variables() finds all Vars in an expression tree
1066 * that reference a particular RTE, and replaces them with substitute
1067 * expressions obtained from a caller-supplied callback function.
1069 * When invoking replace_rte_variables on a portion of a Query, pass the
1070 * address of the containing Query's hasSubLinks field as outer_hasSubLinks.
1071 * Otherwise, pass NULL, but inserting a SubLink into a non-Query expression
1072 * will then cause an error.
1074 * Note: the business with inserted_sublink is needed to update hasSubLinks
1075 * in subqueries when the replacement adds a subquery inside a subquery.
1076 * Messy, isn't it? We do not need to do similar pushups for hasAggs,
1077 * because it isn't possible for this transformation to insert a level-zero
1078 * aggregate reference into a subquery --- it could only insert outer aggs.
1079 * Likewise for hasWindowFuncs.
1081 * Note: usually, we'd not expose the mutator function or context struct
1082 * for a function like this. We do so because callbacks often find it
1083 * convenient to recurse directly to the mutator on sub-expressions of
1084 * what they will return.
1087 replace_rte_variables(Node *node, int target_varno, int sublevels_up,
1088 replace_rte_variables_callback callback,
1090 bool *outer_hasSubLinks)
1093 replace_rte_variables_context context;
1095 context.callback = callback;
1096 context.callback_arg = callback_arg;
1097 context.target_varno = target_varno;
1098 context.sublevels_up = sublevels_up;
1101 * We try to initialize inserted_sublink to true if there is no need to
1102 * detect new sublinks because the query already has some.
1104 if (node && IsA(node, Query))
1105 context.inserted_sublink = ((Query *) node)->hasSubLinks;
1106 else if (outer_hasSubLinks)
1107 context.inserted_sublink = *outer_hasSubLinks;
1109 context.inserted_sublink = false;
1112 * Must be prepared to start with a Query or a bare expression tree; if
1113 * it's a Query, we don't want to increment sublevels_up.
1115 result = query_or_expression_tree_mutator(node,
1116 replace_rte_variables_mutator,
1120 if (context.inserted_sublink)
1122 if (result && IsA(result, Query))
1123 ((Query *) result)->hasSubLinks = true;
1124 else if (outer_hasSubLinks)
1125 *outer_hasSubLinks = true;
1127 elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
1134 replace_rte_variables_mutator(Node *node,
1135 replace_rte_variables_context *context)
1141 Var *var = (Var *) node;
1143 if (var->varno == context->target_varno &&
1144 var->varlevelsup == context->sublevels_up)
1146 /* Found a matching variable, make the substitution */
1149 newnode = (*context->callback) (var, context);
1150 /* Detect if we are adding a sublink to query */
1151 if (!context->inserted_sublink)
1152 context->inserted_sublink = checkExprHasSubLink(newnode);
1155 /* otherwise fall through to copy the var normally */
1157 else if (IsA(node, CurrentOfExpr))
1159 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
1161 if (cexpr->cvarno == context->target_varno &&
1162 context->sublevels_up == 0)
1165 * We get here if a WHERE CURRENT OF expression turns out to apply
1166 * to a view. Someday we might be able to translate the
1167 * expression to apply to an underlying table of the view, but
1168 * right now it's not implemented.
1171 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1172 errmsg("WHERE CURRENT OF on a view is not implemented")));
1174 /* otherwise fall through to copy the expr normally */
1176 else if (IsA(node, Query))
1178 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1180 bool save_inserted_sublink;
1182 context->sublevels_up++;
1183 save_inserted_sublink = context->inserted_sublink;
1184 context->inserted_sublink = ((Query *) node)->hasSubLinks;
1185 newnode = query_tree_mutator((Query *) node,
1186 replace_rte_variables_mutator,
1189 newnode->hasSubLinks |= context->inserted_sublink;
1190 context->inserted_sublink = save_inserted_sublink;
1191 context->sublevels_up--;
1192 return (Node *) newnode;
1194 return expression_tree_mutator(node, replace_rte_variables_mutator,
1200 * map_variable_attnos() finds all user-column Vars in an expression tree
1201 * that reference a particular RTE, and adjusts their varattnos according
1202 * to the given mapping array (varattno n is replaced by attno_map[n-1]).
1203 * Vars for system columns are not modified.
1205 * A zero in the mapping array represents a dropped column, which should not
1206 * appear in the expression.
1208 * If the expression tree contains a whole-row Var for the target RTE,
1209 * the Var is not changed but *found_whole_row is returned as TRUE.
1210 * For most callers this is an error condition, but we leave it to the caller
1211 * to report the error so that useful context can be provided. (In some
1212 * usages it would be appropriate to modify the Var's vartype and insert a
1213 * ConvertRowtypeExpr node to map back to the original vartype. We might
1214 * someday extend this function's API to support that. For now, the only
1215 * concession to that future need is that this function is a tree mutator
1216 * not just a walker.)
1218 * This could be built using replace_rte_variables and a callback function,
1219 * but since we don't ever need to insert sublinks, replace_rte_variables is
1220 * overly complicated.
1225 int target_varno; /* RTE index to search for */
1226 int sublevels_up; /* (current) nesting depth */
1227 const AttrNumber *attno_map; /* map array for user attnos */
1228 int map_length; /* number of entries in attno_map[] */
1229 bool *found_whole_row; /* output flag */
1230 } map_variable_attnos_context;
1233 map_variable_attnos_mutator(Node *node,
1234 map_variable_attnos_context *context)
1240 Var *var = (Var *) node;
1242 if (var->varno == context->target_varno &&
1243 var->varlevelsup == context->sublevels_up)
1245 /* Found a matching variable, make the substitution */
1246 Var *newvar = (Var *) palloc(sizeof(Var));
1247 int attno = var->varattno;
1252 /* user-defined column, replace attno */
1253 if (attno > context->map_length ||
1254 context->attno_map[attno - 1] == 0)
1255 elog(ERROR, "unexpected varattno %d in expression to be mapped",
1257 newvar->varattno = newvar->varoattno = context->attno_map[attno - 1];
1259 else if (attno == 0)
1261 /* whole-row variable, warn caller */
1262 *(context->found_whole_row) = true;
1264 return (Node *) newvar;
1266 /* otherwise fall through to copy the var normally */
1268 else if (IsA(node, Query))
1270 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1273 context->sublevels_up++;
1274 newnode = query_tree_mutator((Query *) node,
1275 map_variable_attnos_mutator,
1278 context->sublevels_up--;
1279 return (Node *) newnode;
1281 return expression_tree_mutator(node, map_variable_attnos_mutator,
1286 map_variable_attnos(Node *node,
1287 int target_varno, int sublevels_up,
1288 const AttrNumber *attno_map, int map_length,
1289 bool *found_whole_row)
1291 map_variable_attnos_context context;
1293 context.target_varno = target_varno;
1294 context.sublevels_up = sublevels_up;
1295 context.attno_map = attno_map;
1296 context.map_length = map_length;
1297 context.found_whole_row = found_whole_row;
1299 *found_whole_row = false;
1302 * Must be prepared to start with a Query or a bare expression tree; if
1303 * it's a Query, we don't want to increment sublevels_up.
1305 return query_or_expression_tree_mutator(node,
1306 map_variable_attnos_mutator,
1313 * ReplaceVarsFromTargetList - replace Vars with items from a targetlist
1315 * Vars matching target_varno and sublevels_up are replaced by the
1316 * entry with matching resno from targetlist, if there is one.
1318 * If there is no matching resno for such a Var, the action depends on the
1320 * REPLACEVARS_REPORT_ERROR: throw an error
1321 * REPLACEVARS_CHANGE_VARNO: change Var's varno to nomatch_varno
1322 * REPLACEVARS_SUBSTITUTE_NULL: replace Var with a NULL Const of same type
1324 * The caller must also provide target_rte, the RTE describing the target
1325 * relation. This is needed to handle whole-row Vars referencing the target.
1326 * We expand such Vars into RowExpr constructs.
1328 * outer_hasSubLinks works the same as for replace_rte_variables().
1333 RangeTblEntry *target_rte;
1335 ReplaceVarsNoMatchOption nomatch_option;
1337 } ReplaceVarsFromTargetList_context;
1340 ReplaceVarsFromTargetList_callback(Var *var,
1341 replace_rte_variables_context *context)
1343 ReplaceVarsFromTargetList_context *rcon = (ReplaceVarsFromTargetList_context *) context->callback_arg;
1346 if (var->varattno == InvalidAttrNumber)
1348 /* Must expand whole-tuple reference into RowExpr */
1354 * If generating an expansion for a var of a named rowtype (ie, this
1355 * is a plain relation RTE), then we must include dummy items for
1356 * dropped columns. If the var is RECORD (ie, this is a JOIN), then
1357 * omit dropped columns. Either way, attach column names to the
1358 * RowExpr for use of ruleutils.c.
1360 expandRTE(rcon->target_rte,
1361 var->varno, var->varlevelsup, var->location,
1362 (var->vartype != RECORDOID),
1363 &colnames, &fields);
1364 /* Adjust the generated per-field Vars... */
1365 fields = (List *) replace_rte_variables_mutator((Node *) fields,
1367 rowexpr = makeNode(RowExpr);
1368 rowexpr->args = fields;
1369 rowexpr->row_typeid = var->vartype;
1370 rowexpr->row_format = COERCE_IMPLICIT_CAST;
1371 rowexpr->colnames = colnames;
1372 rowexpr->location = var->location;
1374 return (Node *) rowexpr;
1377 /* Normal case referencing one targetlist element */
1378 tle = get_tle_by_resno(rcon->targetlist, var->varattno);
1380 if (tle == NULL || tle->resjunk)
1382 /* Failed to find column in targetlist */
1383 switch (rcon->nomatch_option)
1385 case REPLACEVARS_REPORT_ERROR:
1386 /* fall through, throw error below */
1389 case REPLACEVARS_CHANGE_VARNO:
1390 var = (Var *) copyObject(var);
1391 var->varno = rcon->nomatch_varno;
1392 var->varnoold = rcon->nomatch_varno;
1393 return (Node *) var;
1395 case REPLACEVARS_SUBSTITUTE_NULL:
1398 * If Var is of domain type, we should add a CoerceToDomain
1399 * node, in case there is a NOT NULL domain constraint.
1401 return coerce_to_domain((Node *) makeNullConst(var->vartype,
1406 COERCE_IMPLICIT_CAST,
1411 elog(ERROR, "could not find replacement targetlist entry for attno %d",
1413 return NULL; /* keep compiler quiet */
1417 /* Make a copy of the tlist item to return */
1418 Node *newnode = copyObject(tle->expr);
1420 /* Must adjust varlevelsup if tlist item is from higher query */
1421 if (var->varlevelsup > 0)
1422 IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
1425 * Check to see if the tlist item contains a PARAM_MULTIEXPR Param,
1426 * and throw error if so. This case could only happen when expanding
1427 * an ON UPDATE rule's NEW variable and the referenced tlist item in
1428 * the original UPDATE command is part of a multiple assignment. There
1429 * seems no practical way to handle such cases without multiple
1430 * evaluation of the multiple assignment's sub-select, which would
1431 * create semantic oddities that users of rules would probably prefer
1432 * not to cope with. So treat it as an unimplemented feature.
1434 if (contains_multiexpr_param(newnode, NULL))
1436 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1437 errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
1444 ReplaceVarsFromTargetList(Node *node,
1445 int target_varno, int sublevels_up,
1446 RangeTblEntry *target_rte,
1448 ReplaceVarsNoMatchOption nomatch_option,
1450 bool *outer_hasSubLinks)
1452 ReplaceVarsFromTargetList_context context;
1454 context.target_rte = target_rte;
1455 context.targetlist = targetlist;
1456 context.nomatch_option = nomatch_option;
1457 context.nomatch_varno = nomatch_varno;
1459 return replace_rte_variables(node, target_varno, sublevels_up,
1460 ReplaceVarsFromTargetList_callback,