00001 /*------------------------------------------------------------------------- 00002 * 00003 * parse_node.h 00004 * Internal definitions for parser 00005 * 00006 * 00007 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group 00008 * Portions Copyright (c) 1994, Regents of the University of California 00009 * 00010 * src/include/parser/parse_node.h 00011 * 00012 *------------------------------------------------------------------------- 00013 */ 00014 #ifndef PARSE_NODE_H 00015 #define PARSE_NODE_H 00016 00017 #include "nodes/parsenodes.h" 00018 #include "utils/relcache.h" 00019 00020 00021 /* 00022 * Expression kinds distinguished by transformExpr(). Many of these are not 00023 * semantically distinct so far as expression transformation goes; rather, 00024 * we distinguish them so that context-specific error messages can be printed. 00025 * 00026 * Note: EXPR_KIND_OTHER is not used in the core code, but is left for use 00027 * by extension code that might need to call transformExpr(). The core code 00028 * will not enforce any context-driven restrictions on EXPR_KIND_OTHER 00029 * expressions, so the caller would have to check for sub-selects, aggregates, 00030 * and window functions if those need to be disallowed. 00031 */ 00032 typedef enum ParseExprKind 00033 { 00034 EXPR_KIND_NONE = 0, /* "not in an expression" */ 00035 EXPR_KIND_OTHER, /* reserved for extensions */ 00036 EXPR_KIND_JOIN_ON, /* JOIN ON */ 00037 EXPR_KIND_JOIN_USING, /* JOIN USING */ 00038 EXPR_KIND_FROM_SUBSELECT, /* sub-SELECT in FROM clause */ 00039 EXPR_KIND_FROM_FUNCTION, /* function in FROM clause */ 00040 EXPR_KIND_WHERE, /* WHERE */ 00041 EXPR_KIND_HAVING, /* HAVING */ 00042 EXPR_KIND_WINDOW_PARTITION, /* window definition PARTITION BY */ 00043 EXPR_KIND_WINDOW_ORDER, /* window definition ORDER BY */ 00044 EXPR_KIND_WINDOW_FRAME_RANGE, /* window frame clause with RANGE */ 00045 EXPR_KIND_WINDOW_FRAME_ROWS, /* window frame clause with ROWS */ 00046 EXPR_KIND_SELECT_TARGET, /* SELECT target list item */ 00047 EXPR_KIND_INSERT_TARGET, /* INSERT target list item */ 00048 EXPR_KIND_UPDATE_SOURCE, /* UPDATE assignment source item */ 00049 EXPR_KIND_UPDATE_TARGET, /* UPDATE assignment target item */ 00050 EXPR_KIND_GROUP_BY, /* GROUP BY */ 00051 EXPR_KIND_ORDER_BY, /* ORDER BY */ 00052 EXPR_KIND_DISTINCT_ON, /* DISTINCT ON */ 00053 EXPR_KIND_LIMIT, /* LIMIT */ 00054 EXPR_KIND_OFFSET, /* OFFSET */ 00055 EXPR_KIND_RETURNING, /* RETURNING */ 00056 EXPR_KIND_VALUES, /* VALUES */ 00057 EXPR_KIND_CHECK_CONSTRAINT, /* CHECK constraint for a table */ 00058 EXPR_KIND_DOMAIN_CHECK, /* CHECK constraint for a domain */ 00059 EXPR_KIND_COLUMN_DEFAULT, /* default value for a table column */ 00060 EXPR_KIND_FUNCTION_DEFAULT, /* default parameter value for function */ 00061 EXPR_KIND_INDEX_EXPRESSION, /* index expression */ 00062 EXPR_KIND_INDEX_PREDICATE, /* index predicate */ 00063 EXPR_KIND_ALTER_COL_TRANSFORM, /* transform expr in ALTER COLUMN TYPE */ 00064 EXPR_KIND_EXECUTE_PARAMETER, /* parameter value in EXECUTE */ 00065 EXPR_KIND_TRIGGER_WHEN /* WHEN condition in CREATE TRIGGER */ 00066 } ParseExprKind; 00067 00068 00069 /* 00070 * Function signatures for parser hooks 00071 */ 00072 typedef struct ParseState ParseState; 00073 00074 typedef Node *(*PreParseColumnRefHook) (ParseState *pstate, ColumnRef *cref); 00075 typedef Node *(*PostParseColumnRefHook) (ParseState *pstate, ColumnRef *cref, Node *var); 00076 typedef Node *(*ParseParamRefHook) (ParseState *pstate, ParamRef *pref); 00077 typedef Node *(*CoerceParamHook) (ParseState *pstate, Param *param, 00078 Oid targetTypeId, int32 targetTypeMod, 00079 int location); 00080 00081 00082 /* 00083 * State information used during parse analysis 00084 * 00085 * parentParseState: NULL in a top-level ParseState. When parsing a subquery, 00086 * links to current parse state of outer query. 00087 * 00088 * p_sourcetext: source string that generated the raw parsetree being 00089 * analyzed, or NULL if not available. (The string is used only to 00090 * generate cursor positions in error messages: we need it to convert 00091 * byte-wise locations in parse structures to character-wise cursor 00092 * positions.) 00093 * 00094 * p_rtable: list of RTEs that will become the rangetable of the query. 00095 * Note that neither relname nor refname of these entries are necessarily 00096 * unique; searching the rtable by name is a bad idea. 00097 * 00098 * p_joinexprs: list of JoinExpr nodes associated with p_rtable entries. 00099 * This is one-for-one with p_rtable, but contains NULLs for non-join 00100 * RTEs, and may be shorter than p_rtable if the last RTE(s) aren't joins. 00101 * 00102 * p_joinlist: list of join items (RangeTblRef and JoinExpr nodes) that 00103 * will become the fromlist of the query's top-level FromExpr node. 00104 * 00105 * p_namespace: list of ParseNamespaceItems that represents the current 00106 * namespace for table and column lookup. (The RTEs listed here may be just 00107 * a subset of the whole rtable. See ParseNamespaceItem comments below.) 00108 * 00109 * p_lateral_active: TRUE if we are currently parsing a LATERAL subexpression 00110 * of this parse level. This makes p_lateral_only namespace items visible, 00111 * whereas they are not visible when p_lateral_active is FALSE. 00112 * 00113 * p_ctenamespace: list of CommonTableExprs (WITH items) that are visible 00114 * at the moment. This is entirely different from p_namespace because a CTE 00115 * is not an RTE, rather "visibility" means you could make an RTE from it. 00116 * 00117 * p_future_ctes: list of CommonTableExprs (WITH items) that are not yet 00118 * visible due to scope rules. This is used to help improve error messages. 00119 * 00120 * p_parent_cte: CommonTableExpr that immediately contains the current query, 00121 * if any. 00122 * 00123 * p_windowdefs: list of WindowDefs representing WINDOW and OVER clauses. 00124 * We collect these while transforming expressions and then transform them 00125 * afterwards (so that any resjunk tlist items needed for the sort/group 00126 * clauses end up at the end of the query tlist). A WindowDef's location in 00127 * this list, counting from 1, is the winref number to use to reference it. 00128 */ 00129 struct ParseState 00130 { 00131 struct ParseState *parentParseState; /* stack link */ 00132 const char *p_sourcetext; /* source text, or NULL if not available */ 00133 List *p_rtable; /* range table so far */ 00134 List *p_joinexprs; /* JoinExprs for RTE_JOIN p_rtable entries */ 00135 List *p_joinlist; /* join items so far (will become FromExpr 00136 * node's fromlist) */ 00137 List *p_namespace; /* currently-referenceable RTEs (List of 00138 * ParseNamespaceItem) */ 00139 bool p_lateral_active; /* p_lateral_only items visible? */ 00140 List *p_ctenamespace; /* current namespace for common table exprs */ 00141 List *p_future_ctes; /* common table exprs not yet in namespace */ 00142 CommonTableExpr *p_parent_cte; /* this query's containing CTE */ 00143 List *p_windowdefs; /* raw representations of window clauses */ 00144 ParseExprKind p_expr_kind; /* what kind of expression we're parsing */ 00145 int p_next_resno; /* next targetlist resno to assign */ 00146 List *p_locking_clause; /* raw FOR UPDATE/FOR SHARE info */ 00147 Node *p_value_substitute; /* what to replace VALUE with, if any */ 00148 bool p_hasAggs; 00149 bool p_hasWindowFuncs; 00150 bool p_hasSubLinks; 00151 bool p_hasModifyingCTE; 00152 bool p_is_insert; 00153 bool p_is_update; 00154 bool p_locked_from_parent; 00155 Relation p_target_relation; 00156 RangeTblEntry *p_target_rangetblentry; 00157 00158 /* 00159 * Optional hook functions for parser callbacks. These are null unless 00160 * set up by the caller of make_parsestate. 00161 */ 00162 PreParseColumnRefHook p_pre_columnref_hook; 00163 PostParseColumnRefHook p_post_columnref_hook; 00164 ParseParamRefHook p_paramref_hook; 00165 CoerceParamHook p_coerce_param_hook; 00166 void *p_ref_hook_state; /* common passthrough link for above */ 00167 }; 00168 00169 /* 00170 * An element of a namespace list. 00171 * 00172 * Namespace items with p_rel_visible set define which RTEs are accessible by 00173 * qualified names, while those with p_cols_visible set define which RTEs are 00174 * accessible by unqualified names. These sets are different because a JOIN 00175 * without an alias does not hide the contained tables (so they must be 00176 * visible for qualified references) but it does hide their columns 00177 * (unqualified references to the columns refer to the JOIN, not the member 00178 * tables, so we must not complain that such a reference is ambiguous). 00179 * Various special RTEs such as NEW/OLD for rules may also appear with only 00180 * one flag set. 00181 * 00182 * While processing the FROM clause, namespace items may appear with 00183 * p_lateral_only set, meaning they are visible only to LATERAL 00184 * subexpressions. (The pstate's p_lateral_active flag tells whether we are 00185 * inside such a subexpression at the moment.) If p_lateral_ok is not set, 00186 * it's an error to actually use such a namespace item. One might think it 00187 * would be better to just exclude such items from visibility, but the wording 00188 * of SQL:2008 requires us to do it this way. 00189 * 00190 * At no time should a namespace list contain two entries that conflict 00191 * according to the rules in checkNameSpaceConflicts; but note that those 00192 * are more complicated than "must have different alias names", so in practice 00193 * code searching a namespace list has to check for ambiguous references. 00194 */ 00195 typedef struct ParseNamespaceItem 00196 { 00197 RangeTblEntry *p_rte; /* The relation's rangetable entry */ 00198 bool p_rel_visible; /* Relation name is visible? */ 00199 bool p_cols_visible; /* Column names visible as unqualified refs? */ 00200 bool p_lateral_only; /* Is only visible to LATERAL expressions? */ 00201 bool p_lateral_ok; /* If so, does join type allow use? */ 00202 } ParseNamespaceItem; 00203 00204 /* Support for parser_errposition_callback function */ 00205 typedef struct ParseCallbackState 00206 { 00207 ParseState *pstate; 00208 int location; 00209 ErrorContextCallback errcallback; 00210 } ParseCallbackState; 00211 00212 00213 extern ParseState *make_parsestate(ParseState *parentParseState); 00214 extern void free_parsestate(ParseState *pstate); 00215 extern int parser_errposition(ParseState *pstate, int location); 00216 00217 extern void setup_parser_errposition_callback(ParseCallbackState *pcbstate, 00218 ParseState *pstate, int location); 00219 extern void cancel_parser_errposition_callback(ParseCallbackState *pcbstate); 00220 00221 extern Var *make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, 00222 int location); 00223 extern Oid transformArrayType(Oid *arrayType, int32 *arrayTypmod); 00224 extern ArrayRef *transformArraySubscripts(ParseState *pstate, 00225 Node *arrayBase, 00226 Oid arrayType, 00227 Oid elementType, 00228 int32 arrayTypMod, 00229 List *indirection, 00230 Node *assignFrom); 00231 extern Const *make_const(ParseState *pstate, Value *value, int location); 00232 00233 #endif /* PARSE_NODE_H */