gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

289 行
12KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * parse_node.h
  4. * Internal definitions for parser
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, 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/queryenvironment.h"
  18. #include "utils/relcache.h"
  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: 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 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. */
  30. typedef enum ParseExprKind
  31. {
  32. EXPR_KIND_NONE = 0, /* "not in an expression" */
  33. EXPR_KIND_OTHER, /* reserved for extensions */
  34. EXPR_KIND_JOIN_ON, /* JOIN ON */
  35. EXPR_KIND_JOIN_USING, /* JOIN USING */
  36. EXPR_KIND_FROM_SUBSELECT, /* sub-SELECT in FROM clause */
  37. EXPR_KIND_FROM_FUNCTION, /* function in FROM clause */
  38. EXPR_KIND_WHERE, /* WHERE */
  39. EXPR_KIND_HAVING, /* HAVING */
  40. EXPR_KIND_FILTER, /* FILTER */
  41. EXPR_KIND_WINDOW_PARTITION, /* window definition PARTITION BY */
  42. EXPR_KIND_WINDOW_ORDER, /* window definition ORDER BY */
  43. EXPR_KIND_WINDOW_FRAME_RANGE, /* window frame clause with RANGE */
  44. EXPR_KIND_WINDOW_FRAME_ROWS, /* window frame clause with ROWS */
  45. EXPR_KIND_WINDOW_FRAME_GROUPS, /* window frame clause with GROUPS */
  46. EXPR_KIND_SELECT_TARGET, /* SELECT target list item */
  47. EXPR_KIND_INSERT_TARGET, /* INSERT target list item */
  48. EXPR_KIND_UPDATE_SOURCE, /* UPDATE assignment source item */
  49. EXPR_KIND_UPDATE_TARGET, /* UPDATE assignment target item */
  50. EXPR_KIND_GROUP_BY, /* GROUP BY */
  51. EXPR_KIND_ORDER_BY, /* ORDER BY */
  52. EXPR_KIND_DISTINCT_ON, /* DISTINCT ON */
  53. EXPR_KIND_LIMIT, /* LIMIT */
  54. EXPR_KIND_OFFSET, /* OFFSET */
  55. EXPR_KIND_RETURNING, /* RETURNING */
  56. EXPR_KIND_VALUES, /* VALUES */
  57. EXPR_KIND_VALUES_SINGLE, /* single-row VALUES (in INSERT only) */
  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 */
  67. EXPR_KIND_POLICY, /* USING or WITH CHECK expr in policy */
  68. EXPR_KIND_PARTITION_BOUND, /* partition bound expression */
  69. EXPR_KIND_PARTITION_EXPRESSION, /* PARTITION BY expression */
  70. EXPR_KIND_CALL_ARGUMENT, /* procedure argument in CALL */
  71. EXPR_KIND_COPY_WHERE, /* WHERE condition in COPY FROM */
  72. EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */
  73. } ParseExprKind;
  74. /*
  75. * Function signatures for parser hooks
  76. */
  77. typedef struct ParseState ParseState;
  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. * 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 JoinExpr 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 (RangeTblRef and JoinExpr nodes) that
  105. * will become the fromlist of the query's top-level FromExpr 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 ParseNamespaceItem 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: CommonTableExpr 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. * 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 MultiAssignRef 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 FuncExpr or OpExpr 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. */
  165. struct ParseState
  166. {
  167. struct ParseState *parentParseState; /* stack link */
  168. const char *p_sourcetext; /* source text, or NULL if not available */
  169. List *p_rtable; /* range table so far */
  170. List *p_joinexprs; /* JoinExprs for RTE_JOIN p_rtable entries */
  171. List *p_joinlist; /* join items so far (will become FromExpr
  172. * node's fromlist) */
  173. List *p_namespace; /* currently-referenceable RTEs (List of
  174. * ParseNamespaceItem) */
  175. bool p_lateral_active; /* p_lateral_only items visible? */
  176. List *p_ctenamespace; /* current namespace for common table exprs */
  177. List *p_future_ctes; /* common table exprs not yet in namespace */
  178. CommonTableExpr *p_parent_cte; /* this query's containing CTE */
  179. Relation p_target_relation; /* INSERT/UPDATE/DELETE target rel */
  180. RangeTblEntry *p_target_rangetblentry; /* target rel's RTE */
  181. bool p_is_insert; /* process assignment like INSERT not UPDATE */
  182. List *p_windowdefs; /* raw representations of window clauses */
  183. ParseExprKind p_expr_kind; /* what kind of expression we're parsing */
  184. int p_next_resno; /* next targetlist resno to assign */
  185. List *p_multiassign_exprs; /* junk tlist entries for multiassign */
  186. List *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. QueryEnvironment *p_queryEnv; /* curr env, incl refs to enclosing env */
  192. /* Flags telling about things found in the query: */
  193. bool p_hasAggs;
  194. bool p_hasWindowFuncs;
  195. bool p_hasTargetSRFs;
  196. bool p_hasSubLinks;
  197. bool p_hasModifyingCTE;
  198. Node *p_last_srf; /* most recent set-returning func/op found */
  199. /*
  200. * Optional hook functions for parser callbacks. These are null unless
  201. * set up by the caller of make_parsestate.
  202. */
  203. PreParseColumnRefHook p_pre_columnref_hook;
  204. PostParseColumnRefHook p_post_columnref_hook;
  205. ParseParamRefHook p_paramref_hook;
  206. CoerceParamHook p_coerce_param_hook;
  207. void *p_ref_hook_state; /* common passthrough link for above */
  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? */
  243. } ParseNamespaceItem;
  244. /* Support for parser_errposition_callback function */
  245. typedef struct ParseCallbackState
  246. {
  247. ParseState *pstate;
  248. int location;
  249. ErrorContextCallback errcallback;
  250. } ParseCallbackState;
  251. extern ParseState *make_parsestate(ParseState *parentParseState);
  252. extern void free_parsestate(ParseState *pstate);
  253. extern int parser_errposition(ParseState *pstate, int location);
  254. extern void setup_parser_errposition_callback(ParseCallbackState *pcbstate,
  255. ParseState *pstate, int location);
  256. extern void cancel_parser_errposition_callback(ParseCallbackState *pcbstate);
  257. extern Var *make_var(ParseState *pstate, RangeTblEntry *rte, int attrno,
  258. int location);
  259. extern Oid transformContainerType(Oid *containerType, int32 *containerTypmod);
  260. extern SubscriptingRef *transformContainerSubscripts(ParseState *pstate,
  261. Node *containerBase,
  262. Oid containerType,
  263. Oid elementType,
  264. int32 containerTypMod,
  265. List *indirection,
  266. Node *assignFrom);
  267. extern Const *make_const(ParseState *pstate, Value *value, int location);
  268. #endif /* PARSE_NODE_H */
上海开阖软件有限公司 沪ICP备12045867号-1