1/*-------------------------------------------------------------------------
2 *
3 * parse_node.h
4 * Internal definitions for parser
5 *
6 *
7 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development PGGroup
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/parser/parse_node.h
11 *
12 *-------------------------------------------------------------------------
13 */
14#pragma once
15
16#include "nodes/parsenodes.hpp"
17
18
19/*
20 * Expression kinds distinguished by transformExpr(). Many of these are not
21 * semantically distinct so far as expression transformation goes; rather,
22 * we distinguish them so that context-specific error messages can be printed.
23 *
24 * Note: PG_EXPR_KIND_OTHER is not used in the core code, but is left for use
25 * by extension code that might need to call transformExpr(). The core code
26 * will not enforce any context-driven restrictions on PG_EXPR_KIND_OTHER
27 * expressions, so the caller would have to check for sub-selects, aggregates,
28 * window functions, SRFs, etc if those need to be disallowed.
29 */
30typedef enum PGParseExprKind
31{
32 PG_EXPR_KIND_NONE = 0, /* "not in an expression" */
33 PG_EXPR_KIND_OTHER, /* reserved for extensions */
34 PG_EXPR_KIND_JOIN_ON, /* JOIN ON */
35 PG_EXPR_KIND_JOIN_USING, /* JOIN USING */
36 PG_EXPR_KIND_FROM_SUBSELECT, /* sub-SELECT in FROM clause */
37 PG_EXPR_KIND_FROM_FUNCTION, /* function in FROM clause */
38 PG_EXPR_KIND_WHERE, /* WHERE */
39 PG_EXPR_KIND_HAVING, /* HAVING */
40 PG_EXPR_KIND_FILTER, /* FILTER */
41 PG_EXPR_KIND_WINDOW_PARTITION, /* window definition PARTITION BY */
42 PG_EXPR_KIND_WINDOW_ORDER, /* window definition ORDER BY */
43 PG_EXPR_KIND_WINDOW_FRAME_RANGE, /* window frame clause with RANGE */
44 PG_EXPR_KIND_WINDOW_FRAME_ROWS, /* window frame clause with ROWS */
45 PG_EXPR_KIND_SELECT_TARGET, /* SELECT target list item */
46 PG_EXPR_KIND_INSERT_TARGET, /* INSERT target list item */
47 PG_EXPR_KIND_UPDATE_SOURCE, /* UPDATE assignment source item */
48 PG_EXPR_KIND_UPDATE_TARGET, /* UPDATE assignment target item */
49 PG_EXPR_KIND_GROUP_BY, /* GROUP BY */
50 PG_EXPR_KIND_ORDER_BY, /* ORDER BY */
51 PG_EXPR_KIND_DISTINCT_ON, /* DISTINCT ON */
52 PG_EXPR_KIND_LIMIT, /* LIMIT */
53 PG_EXPR_KIND_OFFSET, /* OFFSET */
54 PG_EXPR_KIND_RETURNING, /* RETURNING */
55 PG_EXPR_KIND_VALUES, /* VALUES */
56 PG_EXPR_KIND_VALUES_SINGLE, /* single-row VALUES (in INSERT only) */
57 PG_EXPR_KIND_CHECK_CONSTRAINT, /* CHECK constraint for a table */
58 PG_EXPR_KIND_DOMAIN_CHECK, /* CHECK constraint for a domain */
59 PG_EXPR_KIND_COLUMN_DEFAULT, /* default value for a table column */
60 PG_EXPR_KIND_FUNCTION_DEFAULT, /* default parameter value for function */
61 PG_EXPR_KIND_INDEX_EXPRESSION, /* index expression */
62 PG_EXPR_KIND_INDEX_PREDICATE, /* index predicate */
63 PG_EXPR_KIND_ALTER_COL_TRANSFORM, /* transform expr in ALTER COLUMN TYPE */
64 PG_EXPR_KIND_EXECUTE_PARAMETER, /* parameter value in EXECUTE */
65 PG_EXPR_KIND_TRIGGER_WHEN, /* WHEN condition in CREATE TRIGGER */
66 PG_EXPR_KIND_POLICY, /* USING or WITH CHECK expr in policy */
67 EXPR_KIND_PARTITION_EXPRESSION /* PARTITION BY expression */
68} PGParseExprKind;
69
70
71/*
72 * Function signatures for parser hooks
73 */
74typedef struct PGParseState ParseState;
75
76typedef PGNode *(*PreParseColumnRefHook) (PGParseState *pstate, PGColumnRef *cref);
77typedef PGNode *(*PostParseColumnRefHook) (PGParseState *pstate, PGColumnRef *cref, PGNode *var);
78typedef PGNode *(*ParseParamRefHook) (PGParseState *pstate, PGParamRef *pref);
79typedef PGNode *(*CoerceParamHook) (PGParseState *pstate, PGParam *param,
80 PGOid targetTypeId, int32_t 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 PGJoinExpr 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 (PGRangeTblRef and PGJoinExpr nodes) that
105 * will become the fromlist of the query's top-level PGFromExpr 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 PGParseNamespaceItem 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: PGCommonTableExpr that immediately contains the current query,
123 * if any.
124 *
125 * p_target_relation: target relation, if query is INSERT, UPDATE, or DELETE.
126 *
127 * p_target_rangetblentry: target relation's entry in the rtable list.
128 *
129 * p_is_insert: true to process assignment expressions like INSERT, false
130 * to process them like UPDATE. (Note this can change intra-statement, for
131 * cases like INSERT ON CONFLICT UPDATE.)
132 *
133 * p_windowdefs: list of WindowDefs representing WINDOW and OVER clauses.
134 * We collect these while transforming expressions and then transform them
135 * afterwards (so that any resjunk tlist items needed for the sort/group
136 * clauses end up at the end of the query tlist). A WindowDef's location in
137 * this list, counting from 1, is the winref number to use to reference it.
138 *
139 * p_expr_kind: kind of expression we're currently parsing, as per enum above;
140 * PG_EXPR_KIND_NONE when not in an expression.
141 *
142 * p_next_resno: next TargetEntry.resno to assign, starting from 1.
143 *
144 * p_multiassign_exprs: partially-processed PGMultiAssignRef source expressions.
145 *
146 * p_locking_clause: query's FOR UPDATE/FOR SHARE clause, if any.
147 *
148 * p_locked_from_parent: true if parent query level applies FOR UPDATE/SHARE
149 * to this subquery as a whole.
150 *
151 * p_resolve_unknowns: resolve unknown-type SELECT output columns as type TEXT
152 * (this is true by default).
153 *
154 * p_hasAggs, p_hasWindowFuncs, etc: true if we've found any of the indicated
155 * constructs in the query.
156 *
157 * p_last_srf: the set-returning PGFuncExpr or PGOpExpr most recently found in
158 * the query, or NULL if none.
159 *
160 * p_pre_columnref_hook, etc: optional parser hook functions for modifying the
161 * interpretation of ColumnRefs and ParamRefs.
162 *
163 * p_ref_hook_state: passthrough state for the parser hook functions.
164 */
165struct PGParseState
166{
167 struct PGParseState *parentParseState; /* stack link */
168 const char *p_sourcetext; /* source text, or NULL if not available */
169 PGList *p_rtable; /* range table so far */
170 PGList *p_joinexprs; /* JoinExprs for PG_RTE_JOIN p_rtable entries */
171 PGList *p_joinlist; /* join items so far (will become PGFromExpr
172 * node's fromlist) */
173 PGList *p_namespace; /* currently-referenceable RTEs (PGList of
174 * PGParseNamespaceItem) */
175 bool p_lateral_active; /* p_lateral_only items visible? */
176 PGList *p_ctenamespace; /* current namespace for common table exprs */
177 PGList *p_future_ctes; /* common table exprs not yet in namespace */
178 PGCommonTableExpr *p_parent_cte; /* this query's containing CTE */
179 void* p_target_relation; /* INSERT/UPDATE/DELETE target rel */
180 PGRangeTblEntry *p_target_rangetblentry; /* target rel's RTE */
181 bool p_is_insert; /* process assignment like INSERT not UPDATE */
182 PGList *p_windowdefs; /* raw representations of window clauses */
183 PGParseExprKind p_expr_kind; /* what kind of expression we're parsing */
184 int p_next_resno; /* next targetlist resno to assign */
185 PGList *p_multiassign_exprs; /* junk tlist entries for multiassign */
186 PGList *p_locking_clause; /* raw FOR UPDATE/FOR SHARE info */
187 bool p_locked_from_parent; /* parent has marked this subquery
188 * with FOR UPDATE/FOR SHARE */
189 bool p_resolve_unknowns; /* resolve unknown-type SELECT outputs as
190 * type text */
191
192 void *p_queryEnv; /* curr env, incl refs to enclosing env */
193
194 /* Flags telling about things found in the query: */
195 bool p_hasAggs;
196 bool p_hasWindowFuncs;
197 bool p_hasTargetSRFs;
198 bool p_hasSubLinks;
199 bool p_hasModifyingCTE;
200
201 PGNode *p_last_srf; /* most recent set-returning func/op found */
202
203 /*
204 * Optional hook functions for parser callbacks. These are null unless
205 * set up by the caller of make_parsestate.
206 */
207 PreParseColumnRefHook p_pre_columnref_hook;
208 PostParseColumnRefHook p_post_columnref_hook;
209 ParseParamRefHook p_paramref_hook;
210 CoerceParamHook p_coerce_param_hook;
211 void *p_ref_hook_state; /* common passthrough link for above */
212};
213
214/*
215 * An element of a namespace list.
216 *
217 * Namespace items with p_rel_visible set define which RTEs are accessible by
218 * qualified names, while those with p_cols_visible set define which RTEs are
219 * accessible by unqualified names. These sets are different because a JOIN
220 * without an alias does not hide the contained tables (so they must be
221 * visible for qualified references) but it does hide their columns
222 * (unqualified references to the columns refer to the JOIN, not the member
223 * tables, so we must not complain that such a reference is ambiguous).
224 * Various special RTEs such as NEW/OLD for rules may also appear with only
225 * one flag set.
226 *
227 * While processing the FROM clause, namespace items may appear with
228 * p_lateral_only set, meaning they are visible only to LATERAL
229 * subexpressions. (The pstate's p_lateral_active flag tells whether we are
230 * inside such a subexpression at the moment.) If p_lateral_ok is not set,
231 * it's an error to actually use such a namespace item. One might think it
232 * would be better to just exclude such items from visibility, but the wording
233 * of SQL:2008 requires us to do it this way. We also use p_lateral_ok to
234 * forbid LATERAL references to an UPDATE/DELETE target table.
235 *
236 * At no time should a namespace list contain two entries that conflict
237 * according to the rules in checkNameSpaceConflicts; but note that those
238 * are more complicated than "must have different alias names", so in practice
239 * code searching a namespace list has to check for ambiguous references.
240 */
241typedef struct PGParseNamespaceItem
242{
243 PGRangeTblEntry *p_rte; /* The relation's rangetable entry */
244 bool p_rel_visible; /* Relation name is visible? */
245 bool p_cols_visible; /* Column names visible as unqualified refs? */
246 bool p_lateral_only; /* Is only visible to LATERAL expressions? */
247 bool p_lateral_ok; /* If so, does join type allow use? */
248} PGParseNamespaceItem;
249
250/* Support for parser_errposition_callback function */
251typedef struct PGParseCallbackState
252{
253 PGParseState *pstate;
254 int location;
255 void* errcallback;
256} PGParseCallbackState;
257
258
259extern PGParseState *make_parsestate(PGParseState *parentParseState);
260extern void free_parsestate(PGParseState *pstate);
261extern int parser_errposition(PGParseState *pstate, int location);
262
263extern void setup_parser_errposition_callback(PGParseCallbackState *pcbstate,
264 PGParseState *pstate, int location);
265extern void cancel_parser_errposition_callback(PGParseCallbackState *pcbstate);
266
267extern PGVar *make_var(PGParseState *pstate, PGRangeTblEntry *rte, int attrno,
268 int location);
269extern PGOid transformArrayType(PGOid *arrayType, int32_t *arrayTypmod);
270extern PGArrayRef *transformArraySubscripts(PGParseState *pstate,
271 PGNode *arrayBase,
272 PGOid arrayType,
273 PGOid elementType,
274 int32_t arrayTypMod,
275 PGList *indirection,
276 PGNode *assignFrom);
277extern PGConst *make_const(PGParseState *pstate, PGValue *value, int location);
278