1 /*-------------------------------------------------------------------------
2  *
3  * rewriteManip.h
4  *		Querytree manipulation subroutines for query rewriter.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/rewrite/rewriteManip.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef REWRITEMANIP_H
15 #define REWRITEMANIP_H
16 
17 #include "nodes/parsenodes.h"
18 
19 
20 typedef struct replace_rte_variables_context replace_rte_variables_context;
21 
22 typedef Node *(*replace_rte_variables_callback) (Var *var,
23 												 replace_rte_variables_context *context);
24 
25 struct replace_rte_variables_context
26 {
27 	replace_rte_variables_callback callback;	/* callback function */
28 	void	   *callback_arg;	/* context data for callback function */
29 	int			target_varno;	/* RTE index to search for */
30 	int			sublevels_up;	/* (current) nesting depth */
31 	bool		inserted_sublink;	/* have we inserted a SubLink? */
32 };
33 
34 typedef enum ReplaceVarsNoMatchOption
35 {
36 	REPLACEVARS_REPORT_ERROR,	/* throw error if no match */
37 	REPLACEVARS_CHANGE_VARNO,	/* change the Var's varno, nothing else */
38 	REPLACEVARS_SUBSTITUTE_NULL /* replace with a NULL Const */
39 } ReplaceVarsNoMatchOption;
40 
41 
42 extern void OffsetVarNodes(Node *node, int offset, int sublevels_up);
43 extern void ChangeVarNodes(Node *node, int old_varno, int new_varno,
44 			   int sublevels_up);
45 extern void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up,
46 						int min_sublevels_up);
47 extern void IncrementVarSublevelsUp_rtable(List *rtable,
48 							   int delta_sublevels_up, int min_sublevels_up);
49 
50 extern bool rangeTableEntry_used(Node *node, int rt_index,
51 					 int sublevels_up);
52 
53 extern Query *getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr);
54 
55 extern void AddQual(Query *parsetree, Node *qual);
56 extern void AddInvertedQual(Query *parsetree, Node *qual);
57 
58 extern bool contain_aggs_of_level(Node *node, int levelsup);
59 extern int	locate_agg_of_level(Node *node, int levelsup);
60 extern bool contain_windowfuncs(Node *node);
61 extern int	locate_windowfunc(Node *node);
62 extern bool checkExprHasSubLink(Node *node);
63 
64 extern Node *replace_rte_variables(Node *node,
65 					  int target_varno, int sublevels_up,
66 					  replace_rte_variables_callback callback,
67 					  void *callback_arg,
68 					  bool *outer_hasSubLinks);
69 extern Node *replace_rte_variables_mutator(Node *node,
70 							  replace_rte_variables_context *context);
71 
72 extern Node *map_variable_attnos(Node *node,
73 					int target_varno, int sublevels_up,
74 					const AttrNumber *attno_map, int map_length,
75 					Oid to_rowtype, bool *found_whole_row);
76 
77 extern Node *ReplaceVarsFromTargetList(Node *node,
78 						  int target_varno, int sublevels_up,
79 						  RangeTblEntry *target_rte,
80 						  List *targetlist,
81 						  ReplaceVarsNoMatchOption nomatch_option,
82 						  int nomatch_varno,
83 						  bool *outer_hasSubLinks);
84 
85 #endif							/* REWRITEMANIP_H */
86