| 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 */ |
| 32 | typedef bool (*check_function_callback) (Oid func_id, void *context); |
| 33 | |
| 34 | |
| 35 | extern Oid exprType(const Node *expr); |
| 36 | extern int32 exprTypmod(const Node *expr); |
| 37 | extern bool exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod); |
| 38 | extern Node *relabel_to_typmod(Node *expr, int32 typmod); |
| 39 | extern Node *strip_implicit_coercions(Node *node); |
| 40 | extern bool expression_returns_set(Node *clause); |
| 41 | |
| 42 | extern Oid exprCollation(const Node *expr); |
| 43 | extern Oid exprInputCollation(const Node *expr); |
| 44 | extern void exprSetCollation(Node *expr, Oid collation); |
| 45 | extern void exprSetInputCollation(Node *expr, Oid inputcollation); |
| 46 | |
| 47 | extern int exprLocation(const Node *expr); |
| 48 | |
| 49 | extern void fix_opfuncids(Node *node); |
| 50 | extern void set_opfuncid(OpExpr *opexpr); |
| 51 | extern void set_sa_opfuncid(ScalarArrayOpExpr *opexpr); |
| 52 | |
| 53 | /* Is clause a FuncExpr clause? */ |
| 54 | static inline bool |
| 55 | is_funcclause(const void *clause) |
| 56 | { |
| 57 | return clause != NULL && IsA(clause, FuncExpr); |
| 58 | } |
| 59 | |
| 60 | /* Is clause an OpExpr clause? */ |
| 61 | static inline bool |
| 62 | is_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 */ |
| 68 | static inline Node * |
| 69 | get_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) */ |
| 80 | static inline Node * |
| 81 | get_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? */ |
| 92 | static inline bool |
| 93 | is_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? */ |
| 101 | static inline bool |
| 102 | is_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? */ |
| 110 | static inline bool |
| 111 | is_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 */ |
| 119 | static inline Expr * |
| 120 | get_notclausearg(const void *notclause) |
| 121 | { |
| 122 | return (Expr *) linitial(((const BoolExpr *) notclause)->args); |
| 123 | } |
| 124 | |
| 125 | extern bool check_functions_in_node(Node *node, check_function_callback checker, |
| 126 | void *context); |
| 127 | |
| 128 | extern bool expression_tree_walker(Node *node, bool (*walker) (), |
| 129 | void *context); |
| 130 | extern Node *expression_tree_mutator(Node *node, Node *(*mutator) (), |
| 131 | void *context); |
| 132 | |
| 133 | extern bool query_tree_walker(Query *query, bool (*walker) (), |
| 134 | void *context, int flags); |
| 135 | extern Query *query_tree_mutator(Query *query, Node *(*mutator) (), |
| 136 | void *context, int flags); |
| 137 | |
| 138 | extern bool range_table_walker(List *rtable, bool (*walker) (), |
| 139 | void *context, int flags); |
| 140 | extern List *range_table_mutator(List *rtable, Node *(*mutator) (), |
| 141 | void *context, int flags); |
| 142 | |
| 143 | extern bool query_or_expression_tree_walker(Node *node, bool (*walker) (), |
| 144 | void *context, int flags); |
| 145 | extern Node *query_or_expression_tree_mutator(Node *node, Node *(*mutator) (), |
| 146 | void *context, int flags); |
| 147 | |
| 148 | extern bool raw_expression_tree_walker(Node *node, bool (*walker) (), |
| 149 | void *context); |
| 150 | |
| 151 | struct PlanState; |
| 152 | extern bool planstate_tree_walker(struct PlanState *planstate, bool (*walker) (), |
| 153 | void *context); |
| 154 | |
| 155 | #endif /* NODEFUNCS_H */ |
| 156 | |