1 /*-------------------------------------------------------------------------
4 * Internal definitions for parser
7 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/parser/parse_node.h
12 *-------------------------------------------------------------------------
17 #include "nodes/parsenodes.h"
18 #include "utils/relcache.h"
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.
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.
32 typedef enum ParseExprKind
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 */
71 * Function signatures for parser hooks
73 typedef struct ParseState ParseState;
75 typedef Node *(*PreParseColumnRefHook) (ParseState *pstate, ColumnRef *cref);
76 typedef Node *(*PostParseColumnRefHook) (ParseState *pstate, ColumnRef *cref, Node *var);
77 typedef Node *(*ParseParamRefHook) (ParseState *pstate, ParamRef *pref);
78 typedef Node *(*CoerceParamHook) (ParseState *pstate, Param *param,
79 Oid targetTypeId, int32 targetTypeMod,
84 * State information used during parse analysis
86 * parentParseState: NULL in a top-level ParseState. When parsing a subquery,
87 * links to current parse state of outer query.
89 * p_sourcetext: source string that generated the raw parsetree being
90 * analyzed, or NULL if not available. (The string is used only to
91 * generate cursor positions in error messages: we need it to convert
92 * byte-wise locations in parse structures to character-wise cursor
95 * p_rtable: list of RTEs that will become the rangetable of the query.
96 * Note that neither relname nor refname of these entries are necessarily
97 * unique; searching the rtable by name is a bad idea.
99 * p_joinexprs: list of JoinExpr nodes associated with p_rtable entries.
100 * This is one-for-one with p_rtable, but contains NULLs for non-join
101 * RTEs, and may be shorter than p_rtable if the last RTE(s) aren't joins.
103 * p_joinlist: list of join items (RangeTblRef and JoinExpr nodes) that
104 * will become the fromlist of the query's top-level FromExpr node.
106 * p_namespace: list of ParseNamespaceItems that represents the current
107 * namespace for table and column lookup. (The RTEs listed here may be just
108 * a subset of the whole rtable. See ParseNamespaceItem comments below.)
110 * p_lateral_active: TRUE if we are currently parsing a LATERAL subexpression
111 * of this parse level. This makes p_lateral_only namespace items visible,
112 * whereas they are not visible when p_lateral_active is FALSE.
114 * p_ctenamespace: list of CommonTableExprs (WITH items) that are visible
115 * at the moment. This is entirely different from p_namespace because a CTE
116 * is not an RTE, rather "visibility" means you could make an RTE from it.
118 * p_future_ctes: list of CommonTableExprs (WITH items) that are not yet
119 * visible due to scope rules. This is used to help improve error messages.
121 * p_parent_cte: CommonTableExpr that immediately contains the current query,
124 * p_windowdefs: list of WindowDefs representing WINDOW and OVER clauses.
125 * We collect these while transforming expressions and then transform them
126 * afterwards (so that any resjunk tlist items needed for the sort/group
127 * clauses end up at the end of the query tlist). A WindowDef's location in
128 * this list, counting from 1, is the winref number to use to reference it.
132 struct ParseState *parentParseState; /* stack link */
133 const char *p_sourcetext; /* source text, or NULL if not available */
134 List *p_rtable; /* range table so far */
135 List *p_joinexprs; /* JoinExprs for RTE_JOIN p_rtable entries */
136 List *p_joinlist; /* join items so far (will become FromExpr
137 * node's fromlist) */
138 List *p_namespace; /* currently-referenceable RTEs (List of
139 * ParseNamespaceItem) */
140 bool p_lateral_active; /* p_lateral_only items visible? */
141 List *p_ctenamespace; /* current namespace for common table exprs */
142 List *p_future_ctes; /* common table exprs not yet in namespace */
143 CommonTableExpr *p_parent_cte; /* this query's containing CTE */
144 List *p_windowdefs; /* raw representations of window clauses */
145 ParseExprKind p_expr_kind; /* what kind of expression we're parsing */
146 int p_next_resno; /* next targetlist resno to assign */
147 List *p_locking_clause; /* raw FOR UPDATE/FOR SHARE info */
148 Node *p_value_substitute; /* what to replace VALUE with, if any */
150 bool p_hasWindowFuncs;
152 bool p_hasModifyingCTE;
155 bool p_locked_from_parent;
156 Relation p_target_relation;
157 RangeTblEntry *p_target_rangetblentry;
160 * Optional hook functions for parser callbacks. These are null unless
161 * set up by the caller of make_parsestate.
163 PreParseColumnRefHook p_pre_columnref_hook;
164 PostParseColumnRefHook p_post_columnref_hook;
165 ParseParamRefHook p_paramref_hook;
166 CoerceParamHook p_coerce_param_hook;
167 void *p_ref_hook_state; /* common passthrough link for above */
171 * An element of a namespace list.
173 * Namespace items with p_rel_visible set define which RTEs are accessible by
174 * qualified names, while those with p_cols_visible set define which RTEs are
175 * accessible by unqualified names. These sets are different because a JOIN
176 * without an alias does not hide the contained tables (so they must be
177 * visible for qualified references) but it does hide their columns
178 * (unqualified references to the columns refer to the JOIN, not the member
179 * tables, so we must not complain that such a reference is ambiguous).
180 * Various special RTEs such as NEW/OLD for rules may also appear with only
183 * While processing the FROM clause, namespace items may appear with
184 * p_lateral_only set, meaning they are visible only to LATERAL
185 * subexpressions. (The pstate's p_lateral_active flag tells whether we are
186 * inside such a subexpression at the moment.) If p_lateral_ok is not set,
187 * it's an error to actually use such a namespace item. One might think it
188 * would be better to just exclude such items from visibility, but the wording
189 * of SQL:2008 requires us to do it this way. We also use p_lateral_ok to
190 * forbid LATERAL references to an UPDATE/DELETE target table.
192 * At no time should a namespace list contain two entries that conflict
193 * according to the rules in checkNameSpaceConflicts; but note that those
194 * are more complicated than "must have different alias names", so in practice
195 * code searching a namespace list has to check for ambiguous references.
197 typedef struct ParseNamespaceItem
199 RangeTblEntry *p_rte; /* The relation's rangetable entry */
200 bool p_rel_visible; /* Relation name is visible? */
201 bool p_cols_visible; /* Column names visible as unqualified refs? */
202 bool p_lateral_only; /* Is only visible to LATERAL expressions? */
203 bool p_lateral_ok; /* If so, does join type allow use? */
204 } ParseNamespaceItem;
206 /* Support for parser_errposition_callback function */
207 typedef struct ParseCallbackState
211 ErrorContextCallback errcallback;
212 } ParseCallbackState;
215 extern ParseState *make_parsestate(ParseState *parentParseState);
216 extern void free_parsestate(ParseState *pstate);
217 extern int parser_errposition(ParseState *pstate, int location);
219 extern void setup_parser_errposition_callback(ParseCallbackState *pcbstate,
220 ParseState *pstate, int location);
221 extern void cancel_parser_errposition_callback(ParseCallbackState *pcbstate);
223 extern Var *make_var(ParseState *pstate, RangeTblEntry *rte, int attrno,
225 extern Oid transformArrayType(Oid *arrayType, int32 *arrayTypmod);
226 extern ArrayRef *transformArraySubscripts(ParseState *pstate,
233 extern Const *make_const(ParseState *pstate, Value *value, int location);
235 #endif /* PARSE_NODE_H */