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-2022, 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 /* Forward references for some structs declared below */
23 typedef struct ParseState ParseState;
26 
27 /*
28  * Expression kinds distinguished by transformExpr(). Many of these are not
29  * semantically distinct so far as expression transformation goes; rather,
30  * we distinguish them so that context-specific error messages can be printed.
31  *
32  * Note: EXPR_KIND_OTHER is not used in the core code, but is left for use
33  * by extension code that might need to call transformExpr(). The core code
34  * will not enforce any context-driven restrictions on EXPR_KIND_OTHER
35  * expressions, so the caller would have to check for sub-selects, aggregates,
36  * window functions, SRFs, etc if those need to be disallowed.
37  */
38 typedef enum ParseExprKind
39 {
40  EXPR_KIND_NONE = 0, /* "not in an expression" */
41  EXPR_KIND_OTHER, /* reserved for extensions */
42  EXPR_KIND_JOIN_ON, /* JOIN ON */
43  EXPR_KIND_JOIN_USING, /* JOIN USING */
44  EXPR_KIND_FROM_SUBSELECT, /* sub-SELECT in FROM clause */
45  EXPR_KIND_FROM_FUNCTION, /* function in FROM clause */
46  EXPR_KIND_WHERE, /* WHERE */
47  EXPR_KIND_HAVING, /* HAVING */
48  EXPR_KIND_FILTER, /* FILTER */
49  EXPR_KIND_WINDOW_PARTITION, /* window definition PARTITION BY */
50  EXPR_KIND_WINDOW_ORDER, /* window definition ORDER BY */
51  EXPR_KIND_WINDOW_FRAME_RANGE, /* window frame clause with RANGE */
52  EXPR_KIND_WINDOW_FRAME_ROWS, /* window frame clause with ROWS */
53  EXPR_KIND_WINDOW_FRAME_GROUPS, /* window frame clause with GROUPS */
54  EXPR_KIND_SELECT_TARGET, /* SELECT target list item */
55  EXPR_KIND_INSERT_TARGET, /* INSERT target list item */
56  EXPR_KIND_UPDATE_SOURCE, /* UPDATE assignment source item */
57  EXPR_KIND_UPDATE_TARGET, /* UPDATE assignment target item */
58  EXPR_KIND_MERGE_WHEN, /* MERGE WHEN [NOT] MATCHED condition */
59  EXPR_KIND_GROUP_BY, /* GROUP BY */
60  EXPR_KIND_ORDER_BY, /* ORDER BY */
61  EXPR_KIND_DISTINCT_ON, /* DISTINCT ON */
62  EXPR_KIND_LIMIT, /* LIMIT */
63  EXPR_KIND_OFFSET, /* OFFSET */
64  EXPR_KIND_RETURNING, /* RETURNING */
65  EXPR_KIND_VALUES, /* VALUES */
66  EXPR_KIND_VALUES_SINGLE, /* single-row VALUES (in INSERT only) */
67  EXPR_KIND_CHECK_CONSTRAINT, /* CHECK constraint for a table */
68  EXPR_KIND_DOMAIN_CHECK, /* CHECK constraint for a domain */
69  EXPR_KIND_COLUMN_DEFAULT, /* default value for a table column */
70  EXPR_KIND_FUNCTION_DEFAULT, /* default parameter value for function */
71  EXPR_KIND_INDEX_EXPRESSION, /* index expression */
72  EXPR_KIND_INDEX_PREDICATE, /* index predicate */
73  EXPR_KIND_STATS_EXPRESSION, /* extended statistics expression */
74  EXPR_KIND_ALTER_COL_TRANSFORM, /* transform expr in ALTER COLUMN TYPE */
75  EXPR_KIND_EXECUTE_PARAMETER, /* parameter value in EXECUTE */
76  EXPR_KIND_TRIGGER_WHEN, /* WHEN condition in CREATE TRIGGER */
77  EXPR_KIND_POLICY, /* USING or WITH CHECK expr in policy */
78  EXPR_KIND_PARTITION_BOUND, /* partition bound expression */
79  EXPR_KIND_PARTITION_EXPRESSION, /* PARTITION BY expression */
80  EXPR_KIND_CALL_ARGUMENT, /* procedure argument in CALL */
81  EXPR_KIND_COPY_WHERE, /* WHERE condition in COPY FROM */
82  EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */
83  EXPR_KIND_CYCLE_MARK, /* cycle mark value */
85 
86 
87 /*
88  * Function signatures for parser hooks
89  */
90 typedef Node *(*PreParseColumnRefHook) (ParseState *pstate, ColumnRef *cref);
91 typedef Node *(*PostParseColumnRefHook) (ParseState *pstate, ColumnRef *cref, Node *var);
92 typedef Node *(*ParseParamRefHook) (ParseState *pstate, ParamRef *pref);
93 typedef Node *(*CoerceParamHook) (ParseState *pstate, Param *param,
94  Oid targetTypeId, int32 targetTypeMod,
95  int location);
96 
97 
98 /*
99  * State information used during parse analysis
100  *
101  * parentParseState: NULL in a top-level ParseState. When parsing a subquery,
102  * links to current parse state of outer query.
103  *
104  * p_sourcetext: source string that generated the raw parsetree being
105  * analyzed, or NULL if not available. (The string is used only to
106  * generate cursor positions in error messages: we need it to convert
107  * byte-wise locations in parse structures to character-wise cursor
108  * positions.)
109  *
110  * p_rtable: list of RTEs that will become the rangetable of the query.
111  * Note that neither relname nor refname of these entries are necessarily
112  * unique; searching the rtable by name is a bad idea.
113  *
114  * p_joinexprs: list of JoinExpr nodes associated with p_rtable entries.
115  * This is one-for-one with p_rtable, but contains NULLs for non-join
116  * RTEs, and may be shorter than p_rtable if the last RTE(s) aren't joins.
117  *
118  * p_joinlist: list of join items (RangeTblRef and JoinExpr nodes) that
119  * will become the fromlist of the query's top-level FromExpr node.
120  *
121  * p_namespace: list of ParseNamespaceItems that represents the current
122  * namespace for table and column lookup. (The RTEs listed here may be just
123  * a subset of the whole rtable. See ParseNamespaceItem comments below.)
124  *
125  * p_lateral_active: true if we are currently parsing a LATERAL subexpression
126  * of this parse level. This makes p_lateral_only namespace items visible,
127  * whereas they are not visible when p_lateral_active is FALSE.
128  *
129  * p_ctenamespace: list of CommonTableExprs (WITH items) that are visible
130  * at the moment. This is entirely different from p_namespace because a CTE
131  * is not an RTE, rather "visibility" means you could make an RTE from it.
132  *
133  * p_future_ctes: list of CommonTableExprs (WITH items) that are not yet
134  * visible due to scope rules. This is used to help improve error messages.
135  *
136  * p_parent_cte: CommonTableExpr that immediately contains the current query,
137  * if any.
138  *
139  * p_target_relation: target relation, if query is INSERT/UPDATE/DELETE/MERGE
140  *
141  * p_target_nsitem: target relation's ParseNamespaceItem.
142  *
143  * p_is_insert: true to process assignment expressions like INSERT, false
144  * to process them like UPDATE. (Note this can change intra-statement, for
145  * cases like INSERT ON CONFLICT UPDATE.)
146  *
147  * p_windowdefs: list of WindowDefs representing WINDOW and OVER clauses.
148  * We collect these while transforming expressions and then transform them
149  * afterwards (so that any resjunk tlist items needed for the sort/group
150  * clauses end up at the end of the query tlist). A WindowDef's location in
151  * this list, counting from 1, is the winref number to use to reference it.
152  *
153  * p_expr_kind: kind of expression we're currently parsing, as per enum above;
154  * EXPR_KIND_NONE when not in an expression.
155  *
156  * p_next_resno: next TargetEntry.resno to assign, starting from 1.
157  *
158  * p_multiassign_exprs: partially-processed MultiAssignRef source expressions.
159  *
160  * p_locking_clause: query's FOR UPDATE/FOR SHARE clause, if any.
161  *
162  * p_locked_from_parent: true if parent query level applies FOR UPDATE/SHARE
163  * to this subquery as a whole.
164  *
165  * p_resolve_unknowns: resolve unknown-type SELECT output columns as type TEXT
166  * (this is true by default).
167  *
168  * p_hasAggs, p_hasWindowFuncs, etc: true if we've found any of the indicated
169  * constructs in the query.
170  *
171  * p_last_srf: the set-returning FuncExpr or OpExpr most recently found in
172  * the query, or NULL if none.
173  *
174  * p_pre_columnref_hook, etc: optional parser hook functions for modifying the
175  * interpretation of ColumnRefs and ParamRefs.
176  *
177  * p_ref_hook_state: passthrough state for the parser hook functions.
178  */
180 {
181  ParseState *parentParseState; /* stack link */
182  const char *p_sourcetext; /* source text, or NULL if not available */
183  List *p_rtable; /* range table so far */
184  List *p_joinexprs; /* JoinExprs for RTE_JOIN p_rtable entries */
185  List *p_joinlist; /* join items so far (will become FromExpr
186  * node's fromlist) */
187  List *p_namespace; /* currently-referenceable RTEs (List of
188  * ParseNamespaceItem) */
189  bool p_lateral_active; /* p_lateral_only items visible? */
190  List *p_ctenamespace; /* current namespace for common table exprs */
191  List *p_future_ctes; /* common table exprs not yet in namespace */
192  CommonTableExpr *p_parent_cte; /* this query's containing CTE */
193  Relation p_target_relation; /* INSERT/UPDATE/DELETE/MERGE target rel */
194  ParseNamespaceItem *p_target_nsitem; /* target rel's NSItem, or NULL */
195  bool p_is_insert; /* process assignment like INSERT not UPDATE */
196  List *p_windowdefs; /* raw representations of window clauses */
197  ParseExprKind p_expr_kind; /* what kind of expression we're parsing */
198  int p_next_resno; /* next targetlist resno to assign */
199  List *p_multiassign_exprs; /* junk tlist entries for multiassign */
200  List *p_locking_clause; /* raw FOR UPDATE/FOR SHARE info */
201  bool p_locked_from_parent; /* parent has marked this subquery
202  * with FOR UPDATE/FOR SHARE */
203  bool p_resolve_unknowns; /* resolve unknown-type SELECT outputs as
204  * type text */
205 
206  QueryEnvironment *p_queryEnv; /* curr env, incl refs to enclosing env */
207 
208  /* Flags telling about things found in the query: */
209  bool p_hasAggs;
214 
215  Node *p_last_srf; /* most recent set-returning func/op found */
216 
217  /*
218  * Optional hook functions for parser callbacks. These are null unless
219  * set up by the caller of make_parsestate.
220  */
225  void *p_ref_hook_state; /* common passthrough link for above */
226 };
227 
228 /*
229  * An element of a namespace list.
230  *
231  * p_names contains the table name and column names exposed by this nsitem.
232  * (Typically it's equal to p_rte->eref, but for a JOIN USING alias it's
233  * equal to p_rte->join_using_alias. Since the USING columns will be the
234  * join's first N columns, the net effect is just that we expose only those
235  * join columns via this nsitem.)
236  *
237  * p_rte and p_rtindex link to the underlying rangetable entry.
238  *
239  * The p_nscolumns array contains info showing how to construct Vars
240  * referencing the names appearing in the p_names->colnames list.
241  *
242  * Namespace items with p_rel_visible set define which RTEs are accessible by
243  * qualified names, while those with p_cols_visible set define which RTEs are
244  * accessible by unqualified names. These sets are different because a JOIN
245  * without an alias does not hide the contained tables (so they must be
246  * visible for qualified references) but it does hide their columns
247  * (unqualified references to the columns refer to the JOIN, not the member
248  * tables, so we must not complain that such a reference is ambiguous).
249  * Various special RTEs such as NEW/OLD for rules may also appear with only
250  * one flag set.
251  *
252  * While processing the FROM clause, namespace items may appear with
253  * p_lateral_only set, meaning they are visible only to LATERAL
254  * subexpressions. (The pstate's p_lateral_active flag tells whether we are
255  * inside such a subexpression at the moment.) If p_lateral_ok is not set,
256  * it's an error to actually use such a namespace item. One might think it
257  * would be better to just exclude such items from visibility, but the wording
258  * of SQL:2008 requires us to do it this way. We also use p_lateral_ok to
259  * forbid LATERAL references to an UPDATE/DELETE target table.
260  *
261  * At no time should a namespace list contain two entries that conflict
262  * according to the rules in checkNameSpaceConflicts; but note that those
263  * are more complicated than "must have different alias names", so in practice
264  * code searching a namespace list has to check for ambiguous references.
265  */
267 {
268  Alias *p_names; /* Table and column names */
269  RangeTblEntry *p_rte; /* The relation's rangetable entry */
270  int p_rtindex; /* The relation's index in the rangetable */
271  /* array of same length as p_names->colnames: */
272  ParseNamespaceColumn *p_nscolumns; /* per-column data */
273  bool p_rel_visible; /* Relation name is visible? */
274  bool p_cols_visible; /* Column names visible as unqualified refs? */
275  bool p_lateral_only; /* Is only visible to LATERAL expressions? */
276  bool p_lateral_ok; /* If so, does join type allow use? */
277 };
278 
279 /*
280  * Data about one column of a ParseNamespaceItem.
281  *
282  * We track the info needed to construct a Var referencing the column
283  * (but only for user-defined columns; system column references and
284  * whole-row references are handled separately).
285  *
286  * p_varno and p_varattno identify the semantic referent, which is a
287  * base-relation column unless the reference is to a join USING column that
288  * isn't semantically equivalent to either join input column (because it is a
289  * FULL join or the input column requires a type coercion). In those cases
290  * p_varno and p_varattno refer to the JOIN RTE.
291  *
292  * p_varnosyn and p_varattnosyn are either identical to p_varno/p_varattno,
293  * or they specify the column's position in an aliased JOIN RTE that hides
294  * the semantic referent RTE's refname. (That could be either the JOIN RTE
295  * in which this ParseNamespaceColumn entry exists, or some lower join level.)
296  *
297  * If an RTE contains a dropped column, its ParseNamespaceColumn struct
298  * is all-zeroes. (Conventionally, test for p_varno == 0 to detect this.)
299  */
301 {
302  Index p_varno; /* rangetable index */
303  AttrNumber p_varattno; /* attribute number of the column */
304  Oid p_vartype; /* pg_type OID */
305  int32 p_vartypmod; /* type modifier value */
306  Oid p_varcollid; /* OID of collation, or InvalidOid */
307  Index p_varnosyn; /* rangetable index of syntactic referent */
308  AttrNumber p_varattnosyn; /* attribute number of syntactic referent */
309  bool p_dontexpand; /* not included in star expansion */
310 };
311 
312 /* Support for parser_errposition_callback function */
313 typedef struct ParseCallbackState
314 {
316  int location;
319 
320 
321 extern ParseState *make_parsestate(ParseState *parentParseState);
322 extern void free_parsestate(ParseState *pstate);
323 extern int parser_errposition(ParseState *pstate, int location);
324 
326  ParseState *pstate, int location);
328 
329 extern void transformContainerType(Oid *containerType, int32 *containerTypmod);
330 
332  Node *containerBase,
333  Oid containerType,
334  int32 containerTypMod,
335  List *indirection,
336  bool isAssignment);
337 extern Const *make_const(ParseState *pstate, A_Const *aconst);
338 
339 #endif /* PARSE_NODE_H */
int16 AttrNumber
Definition: attnum.h:21
signed int int32
Definition: c.h:429
unsigned int Index
Definition: c.h:549
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:160
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:76
Node *(* PreParseColumnRefHook)(ParseState *pstate, ColumnRef *cref)
Definition: parse_node.h:90
Node *(* PostParseColumnRefHook)(ParseState *pstate, ColumnRef *cref, Node *var)
Definition: parse_node.h:91
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition: parse_node.c:247
Node *(* ParseParamRefHook)(ParseState *pstate, ParamRef *pref)
Definition: parse_node.h:92
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:144
ParseExprKind
Definition: parse_node.h:39
@ EXPR_KIND_EXECUTE_PARAMETER
Definition: parse_node.h:75
@ EXPR_KIND_DOMAIN_CHECK
Definition: parse_node.h:68
@ EXPR_KIND_COPY_WHERE
Definition: parse_node.h:81
@ EXPR_KIND_COLUMN_DEFAULT
Definition: parse_node.h:69
@ EXPR_KIND_DISTINCT_ON
Definition: parse_node.h:61
@ EXPR_KIND_MERGE_WHEN
Definition: parse_node.h:58
@ EXPR_KIND_STATS_EXPRESSION
Definition: parse_node.h:73
@ EXPR_KIND_INDEX_EXPRESSION
Definition: parse_node.h:71
@ EXPR_KIND_PARTITION_BOUND
Definition: parse_node.h:78
@ EXPR_KIND_FUNCTION_DEFAULT
Definition: parse_node.h:70
@ EXPR_KIND_WINDOW_FRAME_RANGE
Definition: parse_node.h:51
@ EXPR_KIND_VALUES
Definition: parse_node.h:65
@ EXPR_KIND_FROM_SUBSELECT
Definition: parse_node.h:44
@ EXPR_KIND_POLICY
Definition: parse_node.h:77
@ EXPR_KIND_WINDOW_FRAME_GROUPS
Definition: parse_node.h:53
@ EXPR_KIND_PARTITION_EXPRESSION
Definition: parse_node.h:79
@ EXPR_KIND_JOIN_USING
Definition: parse_node.h:43
@ EXPR_KIND_INDEX_PREDICATE
Definition: parse_node.h:72
@ EXPR_KIND_ORDER_BY
Definition: parse_node.h:60
@ EXPR_KIND_OFFSET
Definition: parse_node.h:63
@ EXPR_KIND_JOIN_ON
Definition: parse_node.h:42
@ EXPR_KIND_HAVING
Definition: parse_node.h:47
@ EXPR_KIND_INSERT_TARGET
Definition: parse_node.h:55
@ EXPR_KIND_ALTER_COL_TRANSFORM
Definition: parse_node.h:74
@ EXPR_KIND_LIMIT
Definition: parse_node.h:62
@ EXPR_KIND_WHERE
Definition: parse_node.h:46
@ EXPR_KIND_UPDATE_TARGET
Definition: parse_node.h:57
@ EXPR_KIND_SELECT_TARGET
Definition: parse_node.h:54
@ EXPR_KIND_RETURNING
Definition: parse_node.h:64
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:82
@ EXPR_KIND_NONE
Definition: parse_node.h:40
@ EXPR_KIND_CALL_ARGUMENT
Definition: parse_node.h:80
@ EXPR_KIND_GROUP_BY
Definition: parse_node.h:59
@ EXPR_KIND_OTHER
Definition: parse_node.h:41
@ EXPR_KIND_FROM_FUNCTION
Definition: parse_node.h:45
@ EXPR_KIND_TRIGGER_WHEN
Definition: parse_node.h:76
@ EXPR_KIND_FILTER
Definition: parse_node.h:48
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
@ EXPR_KIND_CHECK_CONSTRAINT
Definition: parse_node.h:67
@ EXPR_KIND_WINDOW_PARTITION
Definition: parse_node.h:49
@ EXPR_KIND_CYCLE_MARK
Definition: parse_node.h:83
@ EXPR_KIND_WINDOW_FRAME_ROWS
Definition: parse_node.h:52
@ EXPR_KIND_WINDOW_ORDER
Definition: parse_node.h:50
@ EXPR_KIND_VALUES_SINGLE
Definition: parse_node.h:66
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:43
void transformContainerType(Oid *containerType, int32 *containerTypmod)
Definition: parse_node.c:193
struct ParseCallbackState ParseCallbackState
Const * make_const(ParseState *pstate, A_Const *aconst)
Definition: parse_node.c:351
Node *(* CoerceParamHook)(ParseState *pstate, Param *param, Oid targetTypeId, int32 targetTypeMod, int location)
Definition: parse_node.h:93
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:51
Definition: nodes.h:574
ErrorContextCallback errcallback
Definition: parse_node.h:317
ParseState * pstate
Definition: parse_node.h:315
AttrNumber p_varattno
Definition: parse_node.h:303
AttrNumber p_varattnosyn
Definition: parse_node.h:308
RangeTblEntry * p_rte
Definition: parse_node.h:269
ParseNamespaceColumn * p_nscolumns
Definition: parse_node.h:272
bool p_hasTargetSRFs
Definition: parse_node.h:211
ParseState * parentParseState
Definition: parse_node.h:181
void * p_ref_hook_state
Definition: parse_node.h:225
List * p_ctenamespace
Definition: parse_node.h:190
bool p_hasWindowFuncs
Definition: parse_node.h:210
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:194
ParseExprKind p_expr_kind
Definition: parse_node.h:197
List * p_multiassign_exprs
Definition: parse_node.h:199
bool p_locked_from_parent
Definition: parse_node.h:201
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:223
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:221
List * p_namespace
Definition: parse_node.h:187
bool p_is_insert
Definition: parse_node.h:195
QueryEnvironment * p_queryEnv
Definition: parse_node.h:206
const char * p_sourcetext
Definition: parse_node.h:182
List * p_windowdefs
Definition: parse_node.h:196
bool p_resolve_unknowns
Definition: parse_node.h:203
int p_next_resno
Definition: parse_node.h:198
bool p_hasModifyingCTE
Definition: parse_node.h:213
List * p_joinexprs
Definition: parse_node.h:184
Relation p_target_relation
Definition: parse_node.h:193
CommonTableExpr * p_parent_cte
Definition: parse_node.h:192
List * p_future_ctes
Definition: parse_node.h:191
bool p_hasSubLinks
Definition: parse_node.h:212
Node * p_last_srf
Definition: parse_node.h:215
List * p_joinlist
Definition: parse_node.h:185
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:224
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:222
List * p_locking_clause
Definition: parse_node.h:200
bool p_lateral_active
Definition: parse_node.h:189
List * p_rtable
Definition: parse_node.h:183
bool p_hasAggs
Definition: parse_node.h:209