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