1 /*-------------------------------------------------------------------------
2  *
3  * nodeFuncs.h
4  *		Various general-purpose manipulations of Node trees
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/nodes/nodeFuncs.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef NODEFUNCS_H
14 #define NODEFUNCS_H
15 
16 #include "nodes/parsenodes.h"
17 
18 
19 /* flags bits for query_tree_walker and query_tree_mutator */
20 #define QTW_IGNORE_RT_SUBQUERIES	0x01	/* subqueries in rtable */
21 #define QTW_IGNORE_CTE_SUBQUERIES	0x02	/* subqueries in cteList */
22 #define QTW_IGNORE_RC_SUBQUERIES	0x03	/* both of above */
23 #define QTW_IGNORE_JOINALIASES		0x04	/* JOIN alias var lists */
24 #define QTW_IGNORE_RANGE_TABLE		0x08	/* skip rangetable entirely */
25 #define QTW_EXAMINE_RTES_BEFORE		0x10	/* examine RTE nodes before their
26 											 * contents */
27 #define QTW_EXAMINE_RTES_AFTER		0x20	/* examine RTE nodes after their
28 											 * contents */
29 #define QTW_DONT_COPY_QUERY			0x40	/* do not copy top Query */
30 #define QTW_EXAMINE_SORTGROUP		0x80	/* include SortGroupNode lists */
31 
32 /* callback function for check_functions_in_node */
33 typedef bool (*check_function_callback) (Oid func_id, void *context);
34 
35 
36 extern Oid	exprType(const Node *expr);
37 extern int32 exprTypmod(const Node *expr);
38 extern bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod);
39 extern Node *applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid,
40 							  CoercionForm rformat, int rlocation,
41 							  bool overwrite_ok);
42 extern Node *relabel_to_typmod(Node *expr, int32 typmod);
43 extern Node *strip_implicit_coercions(Node *node);
44 extern bool expression_returns_set(Node *clause);
45 
46 extern Oid	exprCollation(const Node *expr);
47 extern Oid	exprInputCollation(const Node *expr);
48 extern void exprSetCollation(Node *expr, Oid collation);
49 extern void exprSetInputCollation(Node *expr, Oid inputcollation);
50 
51 extern int	exprLocation(const Node *expr);
52 
53 extern void fix_opfuncids(Node *node);
54 extern void set_opfuncid(OpExpr *opexpr);
55 extern void set_sa_opfuncid(ScalarArrayOpExpr *opexpr);
56 
57 /* Is clause a FuncExpr clause? */
58 static inline bool
is_funcclause(const void * clause)59 is_funcclause(const void *clause)
60 {
61 	return clause != NULL && IsA(clause, FuncExpr);
62 }
63 
64 /* Is clause an OpExpr clause? */
65 static inline bool
is_opclause(const void * clause)66 is_opclause(const void *clause)
67 {
68 	return clause != NULL && IsA(clause, OpExpr);
69 }
70 
71 /* Extract left arg of a binary opclause, or only arg of a unary opclause */
72 static inline Node *
get_leftop(const void * clause)73 get_leftop(const void *clause)
74 {
75 	const OpExpr *expr = (const OpExpr *) clause;
76 
77 	if (expr->args != NIL)
78 		return (Node *) linitial(expr->args);
79 	else
80 		return NULL;
81 }
82 
83 /* Extract right arg of a binary opclause (NULL if it's a unary opclause) */
84 static inline Node *
get_rightop(const void * clause)85 get_rightop(const void *clause)
86 {
87 	const OpExpr *expr = (const OpExpr *) clause;
88 
89 	if (list_length(expr->args) >= 2)
90 		return (Node *) lsecond(expr->args);
91 	else
92 		return NULL;
93 }
94 
95 /* Is clause an AND clause? */
96 static inline bool
is_andclause(const void * clause)97 is_andclause(const void *clause)
98 {
99 	return (clause != NULL &&
100 			IsA(clause, BoolExpr) &&
101 			((const BoolExpr *) clause)->boolop == AND_EXPR);
102 }
103 
104 /* Is clause an OR clause? */
105 static inline bool
is_orclause(const void * clause)106 is_orclause(const void *clause)
107 {
108 	return (clause != NULL &&
109 			IsA(clause, BoolExpr) &&
110 			((const BoolExpr *) clause)->boolop == OR_EXPR);
111 }
112 
113 /* Is clause a NOT clause? */
114 static inline bool
is_notclause(const void * clause)115 is_notclause(const void *clause)
116 {
117 	return (clause != NULL &&
118 			IsA(clause, BoolExpr) &&
119 			((const BoolExpr *) clause)->boolop == NOT_EXPR);
120 }
121 
122 /* Extract argument from a clause known to be a NOT clause */
123 static inline Expr *
get_notclausearg(const void * notclause)124 get_notclausearg(const void *notclause)
125 {
126 	return (Expr *) linitial(((const BoolExpr *) notclause)->args);
127 }
128 
129 extern bool check_functions_in_node(Node *node, check_function_callback checker,
130 									void *context);
131 
132 extern bool expression_tree_walker(Node *node, bool (*walker) (),
133 								   void *context);
134 extern Node *expression_tree_mutator(Node *node, Node *(*mutator) (),
135 									 void *context);
136 
137 extern bool query_tree_walker(Query *query, bool (*walker) (),
138 							  void *context, int flags);
139 extern Query *query_tree_mutator(Query *query, Node *(*mutator) (),
140 								 void *context, int flags);
141 
142 extern bool range_table_walker(List *rtable, bool (*walker) (),
143 							   void *context, int flags);
144 extern List *range_table_mutator(List *rtable, Node *(*mutator) (),
145 								 void *context, int flags);
146 
147 extern bool range_table_entry_walker(RangeTblEntry *rte, bool (*walker) (),
148 									 void *context, int flags);
149 
150 extern bool query_or_expression_tree_walker(Node *node, bool (*walker) (),
151 											void *context, int flags);
152 extern Node *query_or_expression_tree_mutator(Node *node, Node *(*mutator) (),
153 											  void *context, int flags);
154 
155 extern bool raw_expression_tree_walker(Node *node, bool (*walker) (),
156 									   void *context);
157 
158 struct PlanState;
159 extern bool planstate_tree_walker(struct PlanState *planstate, bool (*walker) (),
160 								  void *context);
161 
162 #endif							/* NODEFUNCS_H */
163