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