parse_node.h 10 KB

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