1/*-------------------------------------------------------------------------
2 *
3 * nodeFuncs.h
4 * Various general-purpose manipulations of Node trees
5 *
6 * Portions Copyright (c) 1996-2019, 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
31/* callback function for check_functions_in_node */
32typedef bool (*check_function_callback) (Oid func_id, void *context);
33
34
35extern Oid exprType(const Node *expr);
36extern int32 exprTypmod(const Node *expr);
37extern bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod);
38extern Node *relabel_to_typmod(Node *expr, int32 typmod);
39extern Node *strip_implicit_coercions(Node *node);
40extern bool expression_returns_set(Node *clause);
41
42extern Oid exprCollation(const Node *expr);
43extern Oid exprInputCollation(const Node *expr);
44extern void exprSetCollation(Node *expr, Oid collation);
45extern void exprSetInputCollation(Node *expr, Oid inputcollation);
46
47extern int exprLocation(const Node *expr);
48
49extern void fix_opfuncids(Node *node);
50extern void set_opfuncid(OpExpr *opexpr);
51extern void set_sa_opfuncid(ScalarArrayOpExpr *opexpr);
52
53/* Is clause a FuncExpr clause? */
54static inline bool
55is_funcclause(const void *clause)
56{
57 return clause != NULL && IsA(clause, FuncExpr);
58}
59
60/* Is clause an OpExpr clause? */
61static inline bool
62is_opclause(const void *clause)
63{
64 return clause != NULL && IsA(clause, OpExpr);
65}
66
67/* Extract left arg of a binary opclause, or only arg of a unary opclause */
68static inline Node *
69get_leftop(const void *clause)
70{
71 const OpExpr *expr = (const OpExpr *) clause;
72
73 if (expr->args != NIL)
74 return (Node *) linitial(expr->args);
75 else
76 return NULL;
77}
78
79/* Extract right arg of a binary opclause (NULL if it's a unary opclause) */
80static inline Node *
81get_rightop(const void *clause)
82{
83 const OpExpr *expr = (const OpExpr *) clause;
84
85 if (list_length(expr->args) >= 2)
86 return (Node *) lsecond(expr->args);
87 else
88 return NULL;
89}
90
91/* Is clause an AND clause? */
92static inline bool
93is_andclause(const void *clause)
94{
95 return (clause != NULL &&
96 IsA(clause, BoolExpr) &&
97 ((const BoolExpr *) clause)->boolop == AND_EXPR);
98}
99
100/* Is clause an OR clause? */
101static inline bool
102is_orclause(const void *clause)
103{
104 return (clause != NULL &&
105 IsA(clause, BoolExpr) &&
106 ((const BoolExpr *) clause)->boolop == OR_EXPR);
107}
108
109/* Is clause a NOT clause? */
110static inline bool
111is_notclause(const void *clause)
112{
113 return (clause != NULL &&
114 IsA(clause, BoolExpr) &&
115 ((const BoolExpr *) clause)->boolop == NOT_EXPR);
116}
117
118/* Extract argument from a clause known to be a NOT clause */
119static inline Expr *
120get_notclausearg(const void *notclause)
121{
122 return (Expr *) linitial(((const BoolExpr *) notclause)->args);
123}
124
125extern bool check_functions_in_node(Node *node, check_function_callback checker,
126 void *context);
127
128extern bool expression_tree_walker(Node *node, bool (*walker) (),
129 void *context);
130extern Node *expression_tree_mutator(Node *node, Node *(*mutator) (),
131 void *context);
132
133extern bool query_tree_walker(Query *query, bool (*walker) (),
134 void *context, int flags);
135extern Query *query_tree_mutator(Query *query, Node *(*mutator) (),
136 void *context, int flags);
137
138extern bool range_table_walker(List *rtable, bool (*walker) (),
139 void *context, int flags);
140extern List *range_table_mutator(List *rtable, Node *(*mutator) (),
141 void *context, int flags);
142
143extern bool query_or_expression_tree_walker(Node *node, bool (*walker) (),
144 void *context, int flags);
145extern Node *query_or_expression_tree_mutator(Node *node, Node *(*mutator) (),
146 void *context, int flags);
147
148extern bool raw_expression_tree_walker(Node *node, bool (*walker) (),
149 void *context);
150
151struct PlanState;
152extern bool planstate_tree_walker(struct PlanState *planstate, bool (*walker) (),
153 void *context);
154
155#endif /* NODEFUNCS_H */
156