PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parse_node.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * parse_node.h
4  * Internal definitions for parser
5  *
6  *
7  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/parser/parse_node.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef PARSE_NODE_H
15 #define PARSE_NODE_H
16 
17 #include "nodes/parsenodes.h"
18 #include "utils/relcache.h"
19 
20 
21 /*
22  * Expression kinds distinguished by transformExpr(). Many of these are not
23  * semantically distinct so far as expression transformation goes; rather,
24  * we distinguish them so that context-specific error messages can be printed.
25  *
26  * Note: EXPR_KIND_OTHER is not used in the core code, but is left for use
27  * by extension code that might need to call transformExpr(). The core code
28  * will not enforce any context-driven restrictions on EXPR_KIND_OTHER
29  * expressions, so the caller would have to check for sub-selects, aggregates,
30  * and window functions if those need to be disallowed.
31  */
32 typedef enum ParseExprKind
33 {
34  EXPR_KIND_NONE = 0, /* "not in an expression" */
35  EXPR_KIND_OTHER, /* reserved for extensions */
36  EXPR_KIND_JOIN_ON, /* JOIN ON */
37  EXPR_KIND_JOIN_USING, /* JOIN USING */
38  EXPR_KIND_FROM_SUBSELECT, /* sub-SELECT in FROM clause */
39  EXPR_KIND_FROM_FUNCTION, /* function in FROM clause */
40  EXPR_KIND_WHERE, /* WHERE */
41  EXPR_KIND_HAVING, /* HAVING */
42  EXPR_KIND_FILTER, /* FILTER */
43  EXPR_KIND_WINDOW_PARTITION, /* window definition PARTITION BY */
44  EXPR_KIND_WINDOW_ORDER, /* window definition ORDER BY */
45  EXPR_KIND_WINDOW_FRAME_RANGE, /* window frame clause with RANGE */
46  EXPR_KIND_WINDOW_FRAME_ROWS, /* window frame clause with ROWS */
47  EXPR_KIND_SELECT_TARGET, /* SELECT target list item */
48  EXPR_KIND_INSERT_TARGET, /* INSERT target list item */
49  EXPR_KIND_UPDATE_SOURCE, /* UPDATE assignment source item */
50  EXPR_KIND_UPDATE_TARGET, /* UPDATE assignment target item */
51  EXPR_KIND_GROUP_BY, /* GROUP BY */
52  EXPR_KIND_ORDER_BY, /* ORDER BY */
53  EXPR_KIND_DISTINCT_ON, /* DISTINCT ON */
54  EXPR_KIND_LIMIT, /* LIMIT */
55  EXPR_KIND_OFFSET, /* OFFSET */
56  EXPR_KIND_RETURNING, /* RETURNING */
57  EXPR_KIND_VALUES, /* VALUES */
58  EXPR_KIND_CHECK_CONSTRAINT, /* CHECK constraint for a table */
59  EXPR_KIND_DOMAIN_CHECK, /* CHECK constraint for a domain */
60  EXPR_KIND_COLUMN_DEFAULT, /* default value for a table column */
61  EXPR_KIND_FUNCTION_DEFAULT, /* default parameter value for function */
62  EXPR_KIND_INDEX_EXPRESSION, /* index expression */
63  EXPR_KIND_INDEX_PREDICATE, /* index predicate */
64  EXPR_KIND_ALTER_COL_TRANSFORM, /* transform expr in ALTER COLUMN TYPE */
65  EXPR_KIND_EXECUTE_PARAMETER, /* parameter value in EXECUTE */
66  EXPR_KIND_TRIGGER_WHEN, /* WHEN condition in CREATE TRIGGER */
67  EXPR_KIND_POLICY /* USING or WITH CHECK expr in policy */
69 
70 
71 /*
72  * Function signatures for parser hooks
73  */
74 typedef struct ParseState ParseState;
75 
76 typedef Node *(*PreParseColumnRefHook) (ParseState *pstate, ColumnRef *cref);
77 typedef Node *(*PostParseColumnRefHook) (ParseState *pstate, ColumnRef *cref, Node *var);
78 typedef Node *(*ParseParamRefHook) (ParseState *pstate, ParamRef *pref);
79 typedef Node *(*CoerceParamHook) (ParseState *pstate, Param *param,
80  Oid targetTypeId, int32 targetTypeMod,
81  int location);
82 
83 
84 /*
85  * State information used during parse analysis
86  *
87  * parentParseState: NULL in a top-level ParseState. When parsing a subquery,
88  * links to current parse state of outer query.
89  *
90  * p_sourcetext: source string that generated the raw parsetree being
91  * analyzed, or NULL if not available. (The string is used only to
92  * generate cursor positions in error messages: we need it to convert
93  * byte-wise locations in parse structures to character-wise cursor
94  * positions.)
95  *
96  * p_rtable: list of RTEs that will become the rangetable of the query.
97  * Note that neither relname nor refname of these entries are necessarily
98  * unique; searching the rtable by name is a bad idea.
99  *
100  * p_joinexprs: list of JoinExpr nodes associated with p_rtable entries.
101  * This is one-for-one with p_rtable, but contains NULLs for non-join
102  * RTEs, and may be shorter than p_rtable if the last RTE(s) aren't joins.
103  *
104  * p_joinlist: list of join items (RangeTblRef and JoinExpr nodes) that
105  * will become the fromlist of the query's top-level FromExpr node.
106  *
107  * p_namespace: list of ParseNamespaceItems that represents the current
108  * namespace for table and column lookup. (The RTEs listed here may be just
109  * a subset of the whole rtable. See ParseNamespaceItem comments below.)
110  *
111  * p_lateral_active: TRUE if we are currently parsing a LATERAL subexpression
112  * of this parse level. This makes p_lateral_only namespace items visible,
113  * whereas they are not visible when p_lateral_active is FALSE.
114  *
115  * p_ctenamespace: list of CommonTableExprs (WITH items) that are visible
116  * at the moment. This is entirely different from p_namespace because a CTE
117  * is not an RTE, rather "visibility" means you could make an RTE from it.
118  *
119  * p_future_ctes: list of CommonTableExprs (WITH items) that are not yet
120  * visible due to scope rules. This is used to help improve error messages.
121  *
122  * p_parent_cte: CommonTableExpr that immediately contains the current query,
123  * if any.
124  *
125  * p_windowdefs: list of WindowDefs representing WINDOW and OVER clauses.
126  * We collect these while transforming expressions and then transform them
127  * afterwards (so that any resjunk tlist items needed for the sort/group
128  * clauses end up at the end of the query tlist). A WindowDef's location in
129  * this list, counting from 1, is the winref number to use to reference it.
130  */
132 {
133  struct ParseState *parentParseState; /* stack link */
134  const char *p_sourcetext; /* source text, or NULL if not available */
135  List *p_rtable; /* range table so far */
136  List *p_joinexprs; /* JoinExprs for RTE_JOIN p_rtable entries */
137  List *p_joinlist; /* join items so far (will become FromExpr
138  * node's fromlist) */
139  List *p_namespace; /* currently-referenceable RTEs (List of
140  * ParseNamespaceItem) */
141  bool p_lateral_active; /* p_lateral_only items visible? */
142  List *p_ctenamespace; /* current namespace for common table exprs */
143  List *p_future_ctes; /* common table exprs not yet in namespace */
144  CommonTableExpr *p_parent_cte; /* this query's containing CTE */
145  List *p_windowdefs; /* raw representations of window clauses */
146  ParseExprKind p_expr_kind; /* what kind of expression we're parsing */
147  int p_next_resno; /* next targetlist resno to assign */
148  List *p_multiassign_exprs; /* junk tlist entries for multiassign */
149  List *p_locking_clause; /* raw FOR UPDATE/FOR SHARE info */
150  Node *p_value_substitute; /* what to replace VALUE with, if any */
151  bool p_hasAggs;
159 
160  /*
161  * Optional hook functions for parser callbacks. These are null unless
162  * set up by the caller of make_parsestate.
163  */
168  void *p_ref_hook_state; /* common passthrough link for above */
169 };
170 
171 /*
172  * An element of a namespace list.
173  *
174  * Namespace items with p_rel_visible set define which RTEs are accessible by
175  * qualified names, while those with p_cols_visible set define which RTEs are
176  * accessible by unqualified names. These sets are different because a JOIN
177  * without an alias does not hide the contained tables (so they must be
178  * visible for qualified references) but it does hide their columns
179  * (unqualified references to the columns refer to the JOIN, not the member
180  * tables, so we must not complain that such a reference is ambiguous).
181  * Various special RTEs such as NEW/OLD for rules may also appear with only
182  * one flag set.
183  *
184  * While processing the FROM clause, namespace items may appear with
185  * p_lateral_only set, meaning they are visible only to LATERAL
186  * subexpressions. (The pstate's p_lateral_active flag tells whether we are
187  * inside such a subexpression at the moment.) If p_lateral_ok is not set,
188  * it's an error to actually use such a namespace item. One might think it
189  * would be better to just exclude such items from visibility, but the wording
190  * of SQL:2008 requires us to do it this way. We also use p_lateral_ok to
191  * forbid LATERAL references to an UPDATE/DELETE target table.
192  *
193  * At no time should a namespace list contain two entries that conflict
194  * according to the rules in checkNameSpaceConflicts; but note that those
195  * are more complicated than "must have different alias names", so in practice
196  * code searching a namespace list has to check for ambiguous references.
197  */
198 typedef struct ParseNamespaceItem
199 {
200  RangeTblEntry *p_rte; /* The relation's rangetable entry */
201  bool p_rel_visible; /* Relation name is visible? */
202  bool p_cols_visible; /* Column names visible as unqualified refs? */
203  bool p_lateral_only; /* Is only visible to LATERAL expressions? */
204  bool p_lateral_ok; /* If so, does join type allow use? */
206 
207 /* Support for parser_errposition_callback function */
208 typedef struct ParseCallbackState
209 {
211  int location;
214 
215 
216 extern ParseState *make_parsestate(ParseState *parentParseState);
217 extern void free_parsestate(ParseState *pstate);
218 extern int parser_errposition(ParseState *pstate, int location);
219 
221  ParseState *pstate, int location);
223 
224 extern Var *make_var(ParseState *pstate, RangeTblEntry *rte, int attrno,
225  int location);
226 extern Oid transformArrayType(Oid *arrayType, int32 *arrayTypmod);
228  Node *arrayBase,
229  Oid arrayType,
230  Oid elementType,
231  int32 arrayTypMod,
232  List *indirection,
233  Node *assignFrom);
234 extern Const *make_const(ParseState *pstate, Value *value, int location);
235 
236 #endif /* PARSE_NODE_H */
static struct @76 value
bool p_hasSubLinks
Definition: parse_node.h:153
struct ParseNamespaceItem ParseNamespaceItem
Node * p_value_substitute
Definition: parse_node.h:150
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:167
Definition: nodes.h:491
Node *(* PreParseColumnRefHook)(ParseState *pstate, ColumnRef *cref)
Definition: parse_node.h:76
List * p_multiassign_exprs
Definition: parse_node.h:148
bool p_hasAggs
Definition: parse_node.h:151
Oid transformArrayType(Oid *arrayType, int32 *arrayTypmod)
Definition: parse_node.c:213
ErrorContextCallback errcallback
Definition: parse_node.h:212
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:148
CommonTableExpr * p_parent_cte
Definition: parse_node.h:144
ParseExprKind
Definition: parse_node.h:32
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:165
signed int int32
Definition: c.h:253
bool p_hasWindowFuncs
Definition: parse_node.h:152
bool p_locked_from_parent
Definition: parse_node.h:156
RangeTblEntry * p_rte
Definition: parse_node.h:200
List * p_windowdefs
Definition: parse_node.h:145
List * p_namespace
Definition: parse_node.h:139
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:166
int p_next_resno
Definition: parse_node.h:147
List * p_locking_clause
Definition: parse_node.h:149
const char * p_sourcetext
Definition: parse_node.h:134
ArrayRef * transformArraySubscripts(ParseState *pstate, Node *arrayBase, Oid arrayType, Oid elementType, int32 arrayTypMod, List *indirection, Node *assignFrom)
Definition: parse_node.c:291
bool p_hasModifyingCTE
Definition: parse_node.h:154
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
ParseState * pstate
Definition: parse_node.h:210
struct ParseState * parentParseState
Definition: parse_node.h:133
List * p_future_ctes
Definition: parse_node.h:143
Node *(* CoerceParamHook)(ParseState *pstate, Param *param, Oid targetTypeId, int32 targetTypeMod, int location)
Definition: parse_node.h:79
RangeTblEntry * p_target_rangetblentry
Definition: parse_node.h:158
ParseExprKind p_expr_kind
Definition: parse_node.h:146
bool p_lateral_active
Definition: parse_node.h:141
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:108
void * p_ref_hook_state
Definition: parse_node.h:168
Definition: value.h:42
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:142
Var * make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
Definition: parse_node.c:186
struct ParseCallbackState ParseCallbackState
Relation p_target_relation
Definition: parse_node.h:157
Node *(* PostParseColumnRefHook)(ParseState *pstate, ColumnRef *cref, Node *var)
Definition: parse_node.h:77
bool p_is_insert
Definition: parse_node.h:155
Const * make_const(ParseState *pstate, Value *value, int location)
Definition: parse_node.c:468
List * p_ctenamespace
Definition: parse_node.h:142
List * p_joinlist
Definition: parse_node.h:137
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:74
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:164
Definition: pg_list.h:45
Node *(* ParseParamRefHook)(ParseState *pstate, ParamRef *pref)
Definition: parse_node.h:78
List * p_joinexprs
Definition: parse_node.h:136
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:158
List * p_rtable
Definition: parse_node.h:135