PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
nodeFuncs.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeFuncs.h
4 * Various general-purpose manipulations of Node trees
5 *
6 * Portions Copyright (c) 1996-2025, 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
18struct PlanState; /* avoid including execnodes.h too */
19
20
21/* flags bits for query_tree_walker and query_tree_mutator */
22#define QTW_IGNORE_RT_SUBQUERIES 0x01 /* subqueries in rtable */
23#define QTW_IGNORE_CTE_SUBQUERIES 0x02 /* subqueries in cteList */
24#define QTW_IGNORE_RC_SUBQUERIES 0x03 /* both of above */
25#define QTW_IGNORE_JOINALIASES 0x04 /* JOIN alias var lists */
26#define QTW_IGNORE_RANGE_TABLE 0x08 /* skip rangetable entirely */
27#define QTW_EXAMINE_RTES_BEFORE 0x10 /* examine RTE nodes before their
28 * contents */
29#define QTW_EXAMINE_RTES_AFTER 0x20 /* examine RTE nodes after their
30 * contents */
31#define QTW_DONT_COPY_QUERY 0x40 /* do not copy top Query */
32#define QTW_EXAMINE_SORTGROUP 0x80 /* include SortGroupClause lists */
33
34#define QTW_IGNORE_GROUPEXPRS 0x100 /* GROUP expressions list */
36/* callback function for check_functions_in_node */
37typedef bool (*check_function_callback) (Oid func_id, void *context);
39/* callback functions for tree walkers */
40typedef bool (*tree_walker_callback) (Node *node, void *context);
41typedef bool (*planstate_tree_walker_callback) (struct PlanState *planstate,
42 void *context);
44/* callback functions for tree mutators */
45typedef Node *(*tree_mutator_callback) (Node *node, void *context);
46
47
48extern Oid exprType(const Node *expr);
49extern int32 exprTypmod(const Node *expr);
50extern bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod);
51extern Node *applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid,
52 CoercionForm rformat, int rlocation,
53 bool overwrite_ok);
54extern Node *relabel_to_typmod(Node *expr, int32 typmod);
56extern bool expression_returns_set(Node *clause);
57
58extern Oid exprCollation(const Node *expr);
59extern Oid exprInputCollation(const Node *expr);
60extern void exprSetCollation(Node *expr, Oid collation);
61extern void exprSetInputCollation(Node *expr, Oid inputcollation);
62
63extern int exprLocation(const Node *expr);
64
65extern void fix_opfuncids(Node *node);
66extern void set_opfuncid(OpExpr *opexpr);
67extern void set_sa_opfuncid(ScalarArrayOpExpr *opexpr);
68
69/* Is clause a FuncExpr clause? */
70static inline bool
71is_funcclause(const void *clause)
72{
73 return clause != NULL && IsA(clause, FuncExpr);
74}
75
76/* Is clause an OpExpr clause? */
77static inline bool
78is_opclause(const void *clause)
79{
80 return clause != NULL && IsA(clause, OpExpr);
81}
82
83/* Extract left arg of a binary opclause, or only arg of a unary opclause */
84static inline Node *
85get_leftop(const void *clause)
86{
87 const OpExpr *expr = (const OpExpr *) clause;
88
89 if (expr->args != NIL)
90 return (Node *) linitial(expr->args);
91 else
92 return NULL;
93}
94
95/* Extract right arg of a binary opclause (NULL if it's a unary opclause) */
96static inline Node *
97get_rightop(const void *clause)
98{
99 const OpExpr *expr = (const OpExpr *) clause;
100
101 if (list_length(expr->args) >= 2)
102 return (Node *) lsecond(expr->args);
103 else
104 return NULL;
105}
106
107/* Is clause an AND clause? */
108static inline bool
109is_andclause(const void *clause)
110{
111 return (clause != NULL &&
112 IsA(clause, BoolExpr) &&
113 ((const BoolExpr *) clause)->boolop == AND_EXPR);
114}
115
116/* Is clause an OR clause? */
117static inline bool
118is_orclause(const void *clause)
119{
120 return (clause != NULL &&
121 IsA(clause, BoolExpr) &&
122 ((const BoolExpr *) clause)->boolop == OR_EXPR);
123}
124
125/* Is clause a NOT clause? */
126static inline bool
127is_notclause(const void *clause)
128{
129 return (clause != NULL &&
130 IsA(clause, BoolExpr) &&
131 ((const BoolExpr *) clause)->boolop == NOT_EXPR);
132}
133
134/* Extract argument from a clause known to be a NOT clause */
135static inline Expr *
136get_notclausearg(const void *notclause)
137{
138 return (Expr *) linitial(((const BoolExpr *) notclause)->args);
139}
140
141extern bool check_functions_in_node(Node *node, check_function_callback checker,
142 void *context);
143
144/*
145 * The following functions are usually passed walker or mutator callbacks
146 * that are declared like "bool walker(Node *node, my_struct *context)"
147 * rather than "bool walker(Node *node, void *context)" as a strict reading
148 * of the C standard would require. Changing the callbacks' declarations
149 * to "void *" would create serious hazards of passing them the wrong context
150 * struct type, so we respectfully decline to support the standard's position
151 * that a pointer to struct is incompatible with "void *". Instead, silence
152 * related compiler warnings by inserting casts into these macro wrappers.
153 */
154
155#define expression_tree_walker(n, w, c) \
156 expression_tree_walker_impl(n, (tree_walker_callback) (w), c)
157#define expression_tree_mutator(n, m, c) \
158 expression_tree_mutator_impl(n, (tree_mutator_callback) (m), c)
159
160#define query_tree_walker(q, w, c, f) \
161 query_tree_walker_impl(q, (tree_walker_callback) (w), c, f)
162#define query_tree_mutator(q, m, c, f) \
163 query_tree_mutator_impl(q, (tree_mutator_callback) (m), c, f)
164
165#define range_table_walker(rt, w, c, f) \
166 range_table_walker_impl(rt, (tree_walker_callback) (w), c, f)
167#define range_table_mutator(rt, m, c, f) \
168 range_table_mutator_impl(rt, (tree_mutator_callback) (m), c, f)
169
170#define range_table_entry_walker(r, w, c, f) \
171 range_table_entry_walker_impl(r, (tree_walker_callback) (w), c, f)
172
173#define query_or_expression_tree_walker(n, w, c, f) \
174 query_or_expression_tree_walker_impl(n, (tree_walker_callback) (w), c, f)
175#define query_or_expression_tree_mutator(n, m, c, f) \
176 query_or_expression_tree_mutator_impl(n, (tree_mutator_callback) (m), c, f)
177
178#define raw_expression_tree_walker(n, w, c) \
179 raw_expression_tree_walker_impl(n, (tree_walker_callback) (w), c)
180
181#define planstate_tree_walker(ps, w, c) \
182 planstate_tree_walker_impl(ps, (planstate_tree_walker_callback) (w), c)
183
184extern bool expression_tree_walker_impl(Node *node,
186 void *context);
188 tree_mutator_callback mutator,
189 void *context);
190
191extern bool query_tree_walker_impl(Query *query,
193 void *context, int flags);
194extern Query *query_tree_mutator_impl(Query *query,
195 tree_mutator_callback mutator,
196 void *context, int flags);
197
198extern bool range_table_walker_impl(List *rtable,
200 void *context, int flags);
201extern List *range_table_mutator_impl(List *rtable,
202 tree_mutator_callback mutator,
203 void *context, int flags);
204
207 void *context, int flags);
208
211 void *context, int flags);
213 tree_mutator_callback mutator,
214 void *context, int flags);
215
216extern bool raw_expression_tree_walker_impl(Node *node,
218 void *context);
219
220extern bool planstate_tree_walker_impl(struct PlanState *planstate,
222 void *context);
223
224#endif /* NODEFUNCS_H */
Datum boolop(PG_FUNCTION_ARGS)
Definition: _int_bool.c:417
int32_t int32
Definition: c.h:498
bool query_tree_walker_impl(Query *query, tree_walker_callback walker, void *context, int flags)
Definition: nodeFuncs.c:2698
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:107
bool raw_expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context)
Definition: nodeFuncs.c:4000
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:116
Node *(* tree_mutator_callback)(Node *node, void *context)
Definition: nodeFuncs.h:43
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:95
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool(* planstate_tree_walker_callback)(struct PlanState *planstate, void *context)
Definition: nodeFuncs.h:39
bool planstate_tree_walker_impl(struct PlanState *planstate, planstate_tree_walker_callback walker, void *context)
Definition: nodeFuncs.c:4723
bool range_table_entry_walker_impl(RangeTblEntry *rte, tree_walker_callback walker, void *context, int flags)
Definition: nodeFuncs.c:2822
bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
Definition: nodeFuncs.c:557
void exprSetCollation(Node *expr, Oid collation)
Definition: nodeFuncs.c:1124
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:1076
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:76
bool(* tree_walker_callback)(Node *node, void *context)
Definition: nodeFuncs.h:38
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1910
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
void exprSetInputCollation(Node *expr, Oid inputcollation)
Definition: nodeFuncs.c:1324
bool query_or_expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context, int flags)
Definition: nodeFuncs.c:3946
bool expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *context)
Definition: nodeFuncs.c:2093
static bool is_funcclause(const void *clause)
Definition: nodeFuncs.h:69
bool range_table_walker_impl(List *rtable, tree_walker_callback walker, void *context, int flags)
Definition: nodeFuncs.c:2801
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:125
bool(* check_function_callback)(Oid func_id, void *context)
Definition: nodeFuncs.h:35
Node * query_or_expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *context, int flags)
Definition: nodeFuncs.c:3969
Query * query_tree_mutator_impl(Query *query, tree_mutator_callback mutator, void *context, int flags)
Definition: nodeFuncs.c:3777
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
Definition: nodeFuncs.c:636
static Expr * get_notclausearg(const void *notclause)
Definition: nodeFuncs.h:134
Node * strip_implicit_coercions(Node *node)
Definition: nodeFuncs.c:705
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1388
List * range_table_mutator_impl(List *rtable, tree_mutator_callback mutator, void *context, int flags)
Definition: nodeFuncs.c:3868
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:763
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1841
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:83
Node * expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *context)
Definition: nodeFuncs.c:2950
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:689
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1883
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1872
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
void * arg
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
unsigned int Oid
Definition: postgres_ext.h:30
@ AND_EXPR
Definition: primnodes.h:948
@ OR_EXPR
Definition: primnodes.h:948
@ NOT_EXPR
Definition: primnodes.h:948
CoercionForm
Definition: primnodes.h:750
Definition: pg_list.h:54
Definition: nodes.h:135
List * args
Definition: primnodes.h:853