primnodes.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*-------------------------------------------------------------------------
  2. *
  3. * primnodes.h
  4. * Definitions for "primitive" node types, those that are used in more
  5. * than one of the parse/plan/execute stages of the query pipeline.
  6. * Currently, these are mostly nodes for executable expressions
  7. * and join trees.
  8. *
  9. *
  10. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  11. * Portions Copyright (c) 1994, Regents of the University of California
  12. *
  13. * src/include/nodes/primnodes.h
  14. *
  15. *-------------------------------------------------------------------------
  16. */
  17. #ifndef PRIMNODES_H
  18. #define PRIMNODES_H
  19. #include "access/attnum.h"
  20. #include "nodes/pg_list.h"
  21. /* ----------------------------------------------------------------
  22. * node definitions
  23. * ----------------------------------------------------------------
  24. */
  25. /*
  26. * Alias -
  27. * specifies an alias for a range variable; the alias might also
  28. * specify renaming of columns within the table.
  29. *
  30. * Note: colnames is a list of Value nodes (always strings). In Alias structs
  31. * associated with RTEs, there may be entries corresponding to dropped
  32. * columns; these are normally empty strings (""). See parsenodes.h for info.
  33. */
  34. typedef struct Alias
  35. {
  36. NodeTag type;
  37. char *aliasname; /* aliased rel name (never qualified) */
  38. List *colnames; /* optional list of column aliases */
  39. } Alias;
  40. typedef enum InhOption
  41. {
  42. INH_NO, /* Do NOT scan child tables */
  43. INH_YES, /* DO scan child tables */
  44. INH_DEFAULT /* Use current SQL_inheritance option */
  45. } InhOption;
  46. /* What to do at commit time for temporary relations */
  47. typedef enum OnCommitAction
  48. {
  49. ONCOMMIT_NOOP, /* No ON COMMIT clause (do nothing) */
  50. ONCOMMIT_PRESERVE_ROWS, /* ON COMMIT PRESERVE ROWS (do nothing) */
  51. ONCOMMIT_DELETE_ROWS, /* ON COMMIT DELETE ROWS */
  52. ONCOMMIT_DROP /* ON COMMIT DROP */
  53. } OnCommitAction;
  54. /*
  55. * RangeVar - range variable, used in FROM clauses
  56. *
  57. * Also used to represent table names in utility statements; there, the alias
  58. * field is not used, and inhOpt shows whether to apply the operation
  59. * recursively to child tables. In some contexts it is also useful to carry
  60. * a TEMP table indication here.
  61. */
  62. typedef struct RangeVar
  63. {
  64. NodeTag type;
  65. char *catalogname; /* the catalog (database) name, or NULL */
  66. char *schemaname; /* the schema name, or NULL */
  67. char *relname; /* the relation/sequence name */
  68. InhOption inhOpt; /* expand rel by inheritance? recursively act
  69. * on children? */
  70. char relpersistence; /* see RELPERSISTENCE_* in pg_class.h */
  71. Alias *alias; /* table alias & optional column aliases */
  72. int location; /* token location, or -1 if unknown */
  73. } RangeVar;
  74. /*
  75. * IntoClause - target information for SELECT INTO, CREATE TABLE AS, and
  76. * CREATE MATERIALIZED VIEW
  77. *
  78. * For CREATE MATERIALIZED VIEW, viewQuery is the parsed-but-not-rewritten
  79. * SELECT Query for the view; otherwise it's NULL. (Although it's actually
  80. * Query*, we declare it as Node* to avoid a forward reference.)
  81. */
  82. typedef struct IntoClause
  83. {
  84. NodeTag type;
  85. RangeVar *rel; /* target relation name */
  86. List *colNames; /* column names to assign, or NIL */
  87. List *options; /* options from WITH clause */
  88. OnCommitAction onCommit; /* what do we do at COMMIT? */
  89. char *tableSpaceName; /* table space to use, or NULL */
  90. Node *viewQuery; /* materialized view's SELECT query */
  91. bool skipData; /* true for WITH NO DATA */
  92. } IntoClause;
  93. /* ----------------------------------------------------------------
  94. * node types for executable expressions
  95. * ----------------------------------------------------------------
  96. */
  97. /*
  98. * Expr - generic superclass for executable-expression nodes
  99. *
  100. * All node types that are used in executable expression trees should derive
  101. * from Expr (that is, have Expr as their first field). Since Expr only
  102. * contains NodeTag, this is a formality, but it is an easy form of
  103. * documentation. See also the ExprState node types in execnodes.h.
  104. */
  105. typedef struct Expr
  106. {
  107. NodeTag type;
  108. } Expr;
  109. /*
  110. * Var - expression node representing a variable (ie, a table column)
  111. *
  112. * Note: during parsing/planning, varnoold/varoattno are always just copies
  113. * of varno/varattno. At the tail end of planning, Var nodes appearing in
  114. * upper-level plan nodes are reassigned to point to the outputs of their
  115. * subplans; for example, in a join node varno becomes INNER_VAR or OUTER_VAR
  116. * and varattno becomes the index of the proper element of that subplan's
  117. * target list. Similarly, INDEX_VAR is used to identify Vars that reference
  118. * an index column rather than a heap column. (In ForeignScan and CustomScan
  119. * plan nodes, INDEX_VAR is abused to signify references to columns of a
  120. * custom scan tuple type.) In all these cases, varnoold/varoattno hold the
  121. * original values. The code doesn't really need varnoold/varoattno, but they
  122. * are very useful for debugging and interpreting completed plans, so we keep
  123. * them around.
  124. */
  125. #define INNER_VAR 65000 /* reference to inner subplan */
  126. #define OUTER_VAR 65001 /* reference to outer subplan */
  127. #define INDEX_VAR 65002 /* reference to index column */
  128. #define IS_SPECIAL_VARNO(varno) ((varno) >= INNER_VAR)
  129. /* Symbols for the indexes of the special RTE entries in rules */
  130. #define PRS2_OLD_VARNO 1
  131. #define PRS2_NEW_VARNO 2
  132. typedef struct Var
  133. {
  134. Expr xpr;
  135. Index varno; /* index of this var's relation in the range
  136. * table, or INNER_VAR/OUTER_VAR/INDEX_VAR */
  137. AttrNumber varattno; /* attribute number of this var, or zero for
  138. * all */
  139. Oid vartype; /* pg_type OID for the type of this var */
  140. int32 vartypmod; /* pg_attribute typmod value */
  141. Oid varcollid; /* OID of collation, or InvalidOid if none */
  142. Index varlevelsup; /* for subquery variables referencing outer
  143. * relations; 0 in a normal var, >0 means N
  144. * levels up */
  145. Index varnoold; /* original value of varno, for debugging */
  146. AttrNumber varoattno; /* original value of varattno */
  147. int location; /* token location, or -1 if unknown */
  148. } Var;
  149. /*
  150. * Const
  151. *
  152. * Note: for varlena data types, we make a rule that a Const node's value
  153. * must be in non-extended form (4-byte header, no compression or external
  154. * references). This ensures that the Const node is self-contained and makes
  155. * it more likely that equal() will see logically identical values as equal.
  156. */
  157. typedef struct Const
  158. {
  159. Expr xpr;
  160. Oid consttype; /* pg_type OID of the constant's datatype */
  161. int32 consttypmod; /* typmod value, if any */
  162. Oid constcollid; /* OID of collation, or InvalidOid if none */
  163. int constlen; /* typlen of the constant's datatype */
  164. Datum constvalue; /* the constant's value */
  165. bool constisnull; /* whether the constant is null (if true,
  166. * constvalue is undefined) */
  167. bool constbyval; /* whether this datatype is passed by value.
  168. * If true, then all the information is stored
  169. * in the Datum. If false, then the Datum
  170. * contains a pointer to the information. */
  171. int location; /* token location, or -1 if unknown */
  172. } Const;
  173. /*
  174. * Param
  175. *
  176. * paramkind specifies the kind of parameter. The possible values
  177. * for this field are:
  178. *
  179. * PARAM_EXTERN: The parameter value is supplied from outside the plan.
  180. * Such parameters are numbered from 1 to n.
  181. *
  182. * PARAM_EXEC: The parameter is an internal executor parameter, used
  183. * for passing values into and out of sub-queries or from
  184. * nestloop joins to their inner scans.
  185. * For historical reasons, such parameters are numbered from 0.
  186. * These numbers are independent of PARAM_EXTERN numbers.
  187. *
  188. * PARAM_SUBLINK: The parameter represents an output column of a SubLink
  189. * node's sub-select. The column number is contained in the
  190. * `paramid' field. (This type of Param is converted to
  191. * PARAM_EXEC during planning.)
  192. *
  193. * PARAM_MULTIEXPR: Like PARAM_SUBLINK, the parameter represents an
  194. * output column of a SubLink node's sub-select, but here, the
  195. * SubLink is always a MULTIEXPR SubLink. The high-order 16 bits
  196. * of the `paramid' field contain the SubLink's subLinkId, and
  197. * the low-order 16 bits contain the column number. (This type
  198. * of Param is also converted to PARAM_EXEC during planning.)
  199. */
  200. typedef enum ParamKind
  201. {
  202. PARAM_EXTERN,
  203. PARAM_EXEC,
  204. PARAM_SUBLINK,
  205. PARAM_MULTIEXPR
  206. } ParamKind;
  207. typedef struct Param
  208. {
  209. Expr xpr;
  210. ParamKind paramkind; /* kind of parameter. See above */
  211. int paramid; /* numeric ID for parameter */
  212. Oid paramtype; /* pg_type OID of parameter's datatype */
  213. int32 paramtypmod; /* typmod value, if known */
  214. Oid paramcollid; /* OID of collation, or InvalidOid if none */
  215. int location; /* token location, or -1 if unknown */
  216. } Param;
  217. /*
  218. * Aggref
  219. *
  220. * The aggregate's args list is a targetlist, ie, a list of TargetEntry nodes.
  221. *
  222. * For a normal (non-ordered-set) aggregate, the non-resjunk TargetEntries
  223. * represent the aggregate's regular arguments (if any) and resjunk TLEs can
  224. * be added at the end to represent ORDER BY expressions that are not also
  225. * arguments. As in a top-level Query, the TLEs can be marked with
  226. * ressortgroupref indexes to let them be referenced by SortGroupClause
  227. * entries in the aggorder and/or aggdistinct lists. This represents ORDER BY
  228. * and DISTINCT operations to be applied to the aggregate input rows before
  229. * they are passed to the transition function. The grammar only allows a
  230. * simple "DISTINCT" specifier for the arguments, but we use the full
  231. * query-level representation to allow more code sharing.
  232. *
  233. * For an ordered-set aggregate, the args list represents the WITHIN GROUP
  234. * (aggregated) arguments, all of which will be listed in the aggorder list.
  235. * DISTINCT is not supported in this case, so aggdistinct will be NIL.
  236. * The direct arguments appear in aggdirectargs (as a list of plain
  237. * expressions, not TargetEntry nodes).
  238. *
  239. * aggtranstype is the data type of the state transition values for this
  240. * aggregate (resolved to an actual type, if agg's transtype is polymorphic).
  241. * This is determined during planning and is InvalidOid before that.
  242. *
  243. * aggargtypes is an OID list of the data types of the direct and regular
  244. * arguments. Normally it's redundant with the aggdirectargs and args lists,
  245. * but in a combining aggregate, it's not because the args list has been
  246. * replaced with a single argument representing the partial-aggregate
  247. * transition values.
  248. *
  249. * aggsplit indicates the expected partial-aggregation mode for the Aggref's
  250. * parent plan node. It's always set to AGGSPLIT_SIMPLE in the parser, but
  251. * the planner might change it to something else. We use this mainly as
  252. * a crosscheck that the Aggrefs match the plan; but note that when aggsplit
  253. * indicates a non-final mode, aggtype reflects the transition data type
  254. * not the SQL-level output type of the aggregate.
  255. */
  256. typedef struct Aggref
  257. {
  258. Expr xpr;
  259. Oid aggfnoid; /* pg_proc Oid of the aggregate */
  260. Oid aggtype; /* type Oid of result of the aggregate */
  261. Oid aggcollid; /* OID of collation of result */
  262. Oid inputcollid; /* OID of collation that function should use */
  263. Oid aggtranstype; /* type Oid of aggregate's transition value */
  264. List *aggargtypes; /* type Oids of direct and aggregated args */
  265. List *aggdirectargs; /* direct arguments, if an ordered-set agg */
  266. List *args; /* aggregated arguments and sort expressions */
  267. List *aggorder; /* ORDER BY (list of SortGroupClause) */
  268. List *aggdistinct; /* DISTINCT (list of SortGroupClause) */
  269. Expr *aggfilter; /* FILTER expression, if any */
  270. bool aggstar; /* TRUE if argument list was really '*' */
  271. bool aggvariadic; /* true if variadic arguments have been
  272. * combined into an array last argument */
  273. char aggkind; /* aggregate kind (see pg_aggregate.h) */
  274. Index agglevelsup; /* > 0 if agg belongs to outer query */
  275. AggSplit aggsplit; /* expected agg-splitting mode of parent Agg */
  276. int location; /* token location, or -1 if unknown */
  277. } Aggref;
  278. /*
  279. * GroupingFunc
  280. *
  281. * A GroupingFunc is a GROUPING(...) expression, which behaves in many ways
  282. * like an aggregate function (e.g. it "belongs" to a specific query level,
  283. * which might not be the one immediately containing it), but also differs in
  284. * an important respect: it never evaluates its arguments, they merely
  285. * designate expressions from the GROUP BY clause of the query level to which
  286. * it belongs.
  287. *
  288. * The spec defines the evaluation of GROUPING() purely by syntactic
  289. * replacement, but we make it a real expression for optimization purposes so
  290. * that one Agg node can handle multiple grouping sets at once. Evaluating the
  291. * result only needs the column positions to check against the grouping set
  292. * being projected. However, for EXPLAIN to produce meaningful output, we have
  293. * to keep the original expressions around, since expression deparse does not
  294. * give us any feasible way to get at the GROUP BY clause.
  295. *
  296. * Also, we treat two GroupingFunc nodes as equal if they have equal arguments
  297. * lists and agglevelsup, without comparing the refs and cols annotations.
  298. *
  299. * In raw parse output we have only the args list; parse analysis fills in the
  300. * refs list, and the planner fills in the cols list.
  301. */
  302. typedef struct GroupingFunc
  303. {
  304. Expr xpr;
  305. List *args; /* arguments, not evaluated but kept for
  306. * benefit of EXPLAIN etc. */
  307. List *refs; /* ressortgrouprefs of arguments */
  308. List *cols; /* actual column positions set by planner */
  309. Index agglevelsup; /* same as Aggref.agglevelsup */
  310. int location; /* token location */
  311. } GroupingFunc;
  312. /*
  313. * WindowFunc
  314. */
  315. typedef struct WindowFunc
  316. {
  317. Expr xpr;
  318. Oid winfnoid; /* pg_proc Oid of the function */
  319. Oid wintype; /* type Oid of result of the window function */
  320. Oid wincollid; /* OID of collation of result */
  321. Oid inputcollid; /* OID of collation that function should use */
  322. List *args; /* arguments to the window function */
  323. Expr *aggfilter; /* FILTER expression, if any */
  324. Index winref; /* index of associated WindowClause */
  325. bool winstar; /* TRUE if argument list was really '*' */
  326. bool winagg; /* is function a simple aggregate? */
  327. int location; /* token location, or -1 if unknown */
  328. } WindowFunc;
  329. /* ----------------
  330. * ArrayRef: describes an array subscripting operation
  331. *
  332. * An ArrayRef can describe fetching a single element from an array,
  333. * fetching a subarray (array slice), storing a single element into
  334. * an array, or storing a slice. The "store" cases work with an
  335. * initial array value and a source value that is inserted into the
  336. * appropriate part of the array; the result of the operation is an
  337. * entire new modified array value.
  338. *
  339. * If reflowerindexpr = NIL, then we are fetching or storing a single array
  340. * element at the subscripts given by refupperindexpr. Otherwise we are
  341. * fetching or storing an array slice, that is a rectangular subarray
  342. * with lower and upper bounds given by the index expressions.
  343. * reflowerindexpr must be the same length as refupperindexpr when it
  344. * is not NIL.
  345. *
  346. * In the slice case, individual expressions in the subscript lists can be
  347. * NULL, meaning "substitute the array's current lower or upper bound".
  348. *
  349. * Note: the result datatype is the element type when fetching a single
  350. * element; but it is the array type when doing subarray fetch or either
  351. * type of store.
  352. *
  353. * Note: for the cases where an array is returned, if refexpr yields a R/W
  354. * expanded array, then the implementation is allowed to modify that object
  355. * in-place and return the same object.)
  356. * ----------------
  357. */
  358. typedef struct ArrayRef
  359. {
  360. Expr xpr;
  361. Oid refarraytype; /* type of the array proper */
  362. Oid refelemtype; /* type of the array elements */
  363. int32 reftypmod; /* typmod of the array (and elements too) */
  364. Oid refcollid; /* OID of collation, or InvalidOid if none */
  365. List *refupperindexpr;/* expressions that evaluate to upper array
  366. * indexes */
  367. List *reflowerindexpr;/* expressions that evaluate to lower array
  368. * indexes, or NIL for single array element */
  369. Expr *refexpr; /* the expression that evaluates to an array
  370. * value */
  371. Expr *refassgnexpr; /* expression for the source value, or NULL if
  372. * fetch */
  373. } ArrayRef;
  374. /*
  375. * CoercionContext - distinguishes the allowed set of type casts
  376. *
  377. * NB: ordering of the alternatives is significant; later (larger) values
  378. * allow more casts than earlier ones.
  379. */
  380. typedef enum CoercionContext
  381. {
  382. COERCION_IMPLICIT, /* coercion in context of expression */
  383. COERCION_ASSIGNMENT, /* coercion in context of assignment */
  384. COERCION_EXPLICIT /* explicit cast operation */
  385. } CoercionContext;
  386. /*
  387. * CoercionForm - how to display a node that could have come from a cast
  388. *
  389. * NB: equal() ignores CoercionForm fields, therefore this *must* not carry
  390. * any semantically significant information. We need that behavior so that
  391. * the planner will consider equivalent implicit and explicit casts to be
  392. * equivalent. In cases where those actually behave differently, the coercion
  393. * function's arguments will be different.
  394. */
  395. typedef enum CoercionForm
  396. {
  397. COERCE_EXPLICIT_CALL, /* display as a function call */
  398. COERCE_EXPLICIT_CAST, /* display as an explicit cast */
  399. COERCE_IMPLICIT_CAST /* implicit cast, so hide it */
  400. } CoercionForm;
  401. /*
  402. * FuncExpr - expression node for a function call
  403. */
  404. typedef struct FuncExpr
  405. {
  406. Expr xpr;
  407. Oid funcid; /* PG_PROC OID of the function */
  408. Oid funcresulttype; /* PG_TYPE OID of result value */
  409. bool funcretset; /* true if function returns set */
  410. bool funcvariadic; /* true if variadic arguments have been
  411. * combined into an array last argument */
  412. CoercionForm funcformat; /* how to display this function call */
  413. Oid funccollid; /* OID of collation of result */
  414. Oid inputcollid; /* OID of collation that function should use */
  415. List *args; /* arguments to the function */
  416. int location; /* token location, or -1 if unknown */
  417. } FuncExpr;
  418. /*
  419. * NamedArgExpr - a named argument of a function
  420. *
  421. * This node type can only appear in the args list of a FuncCall or FuncExpr
  422. * node. We support pure positional call notation (no named arguments),
  423. * named notation (all arguments are named), and mixed notation (unnamed
  424. * arguments followed by named ones).
  425. *
  426. * Parse analysis sets argnumber to the positional index of the argument,
  427. * but doesn't rearrange the argument list.
  428. *
  429. * The planner will convert argument lists to pure positional notation
  430. * during expression preprocessing, so execution never sees a NamedArgExpr.
  431. */
  432. typedef struct NamedArgExpr
  433. {
  434. Expr xpr;
  435. Expr *arg; /* the argument expression */
  436. char *name; /* the name */
  437. int argnumber; /* argument's number in positional notation */
  438. int location; /* argument name location, or -1 if unknown */
  439. } NamedArgExpr;
  440. /*
  441. * OpExpr - expression node for an operator invocation
  442. *
  443. * Semantically, this is essentially the same as a function call.
  444. *
  445. * Note that opfuncid is not necessarily filled in immediately on creation
  446. * of the node. The planner makes sure it is valid before passing the node
  447. * tree to the executor, but during parsing/planning opfuncid can be 0.
  448. */
  449. typedef struct OpExpr
  450. {
  451. Expr xpr;
  452. Oid opno; /* PG_OPERATOR OID of the operator */
  453. Oid opfuncid; /* PG_PROC OID of underlying function */
  454. Oid opresulttype; /* PG_TYPE OID of result value */
  455. bool opretset; /* true if operator returns set */
  456. Oid opcollid; /* OID of collation of result */
  457. Oid inputcollid; /* OID of collation that operator should use */
  458. List *args; /* arguments to the operator (1 or 2) */
  459. int location; /* token location, or -1 if unknown */
  460. } OpExpr;
  461. /*
  462. * DistinctExpr - expression node for "x IS DISTINCT FROM y"
  463. *
  464. * Except for the nodetag, this is represented identically to an OpExpr
  465. * referencing the "=" operator for x and y.
  466. * We use "=", not the more obvious "<>", because more datatypes have "="
  467. * than "<>". This means the executor must invert the operator result.
  468. * Note that the operator function won't be called at all if either input
  469. * is NULL, since then the result can be determined directly.
  470. */
  471. typedef OpExpr DistinctExpr;
  472. /*
  473. * NullIfExpr - a NULLIF expression
  474. *
  475. * Like DistinctExpr, this is represented the same as an OpExpr referencing
  476. * the "=" operator for x and y.
  477. */
  478. typedef OpExpr NullIfExpr;
  479. /*
  480. * ScalarArrayOpExpr - expression node for "scalar op ANY/ALL (array)"
  481. *
  482. * The operator must yield boolean. It is applied to the left operand
  483. * and each element of the righthand array, and the results are combined
  484. * with OR or AND (for ANY or ALL respectively). The node representation
  485. * is almost the same as for the underlying operator, but we need a useOr
  486. * flag to remember whether it's ANY or ALL, and we don't have to store
  487. * the result type (or the collation) because it must be boolean.
  488. */
  489. typedef struct ScalarArrayOpExpr
  490. {
  491. Expr xpr;
  492. Oid opno; /* PG_OPERATOR OID of the operator */
  493. Oid opfuncid; /* PG_PROC OID of underlying function */
  494. bool useOr; /* true for ANY, false for ALL */
  495. Oid inputcollid; /* OID of collation that operator should use */
  496. List *args; /* the scalar and array operands */
  497. int location; /* token location, or -1 if unknown */
  498. } ScalarArrayOpExpr;
  499. /*
  500. * BoolExpr - expression node for the basic Boolean operators AND, OR, NOT
  501. *
  502. * Notice the arguments are given as a List. For NOT, of course the list
  503. * must always have exactly one element. For AND and OR, there can be two
  504. * or more arguments.
  505. */
  506. typedef enum BoolExprType
  507. {
  508. AND_EXPR, OR_EXPR, NOT_EXPR
  509. } BoolExprType;
  510. typedef struct BoolExpr
  511. {
  512. Expr xpr;
  513. BoolExprType boolop;
  514. List *args; /* arguments to this expression */
  515. int location; /* token location, or -1 if unknown */
  516. } BoolExpr;
  517. /*
  518. * SubLink
  519. *
  520. * A SubLink represents a subselect appearing in an expression, and in some
  521. * cases also the combining operator(s) just above it. The subLinkType
  522. * indicates the form of the expression represented:
  523. * EXISTS_SUBLINK EXISTS(SELECT ...)
  524. * ALL_SUBLINK (lefthand) op ALL (SELECT ...)
  525. * ANY_SUBLINK (lefthand) op ANY (SELECT ...)
  526. * ROWCOMPARE_SUBLINK (lefthand) op (SELECT ...)
  527. * EXPR_SUBLINK (SELECT with single targetlist item ...)
  528. * MULTIEXPR_SUBLINK (SELECT with multiple targetlist items ...)
  529. * ARRAY_SUBLINK ARRAY(SELECT with single targetlist item ...)
  530. * CTE_SUBLINK WITH query (never actually part of an expression)
  531. * For ALL, ANY, and ROWCOMPARE, the lefthand is a list of expressions of the
  532. * same length as the subselect's targetlist. ROWCOMPARE will *always* have
  533. * a list with more than one entry; if the subselect has just one target
  534. * then the parser will create an EXPR_SUBLINK instead (and any operator
  535. * above the subselect will be represented separately).
  536. * ROWCOMPARE, EXPR, and MULTIEXPR require the subselect to deliver at most
  537. * one row (if it returns no rows, the result is NULL).
  538. * ALL, ANY, and ROWCOMPARE require the combining operators to deliver boolean
  539. * results. ALL and ANY combine the per-row results using AND and OR
  540. * semantics respectively.
  541. * ARRAY requires just one target column, and creates an array of the target
  542. * column's type using any number of rows resulting from the subselect.
  543. *
  544. * SubLink is classed as an Expr node, but it is not actually executable;
  545. * it must be replaced in the expression tree by a SubPlan node during
  546. * planning.
  547. *
  548. * NOTE: in the raw output of gram.y, testexpr contains just the raw form
  549. * of the lefthand expression (if any), and operName is the String name of
  550. * the combining operator. Also, subselect is a raw parsetree. During parse
  551. * analysis, the parser transforms testexpr into a complete boolean expression
  552. * that compares the lefthand value(s) to PARAM_SUBLINK nodes representing the
  553. * output columns of the subselect. And subselect is transformed to a Query.
  554. * This is the representation seen in saved rules and in the rewriter.
  555. *
  556. * In EXISTS, EXPR, MULTIEXPR, and ARRAY SubLinks, testexpr and operName
  557. * are unused and are always null.
  558. *
  559. * subLinkId is currently used only for MULTIEXPR SubLinks, and is zero in
  560. * other SubLinks. This number identifies different multiple-assignment
  561. * subqueries within an UPDATE statement's SET list. It is unique only
  562. * within a particular targetlist. The output column(s) of the MULTIEXPR
  563. * are referenced by PARAM_MULTIEXPR Params appearing elsewhere in the tlist.
  564. *
  565. * The CTE_SUBLINK case never occurs in actual SubLink nodes, but it is used
  566. * in SubPlans generated for WITH subqueries.
  567. */
  568. typedef enum SubLinkType
  569. {
  570. EXISTS_SUBLINK,
  571. ALL_SUBLINK,
  572. ANY_SUBLINK,
  573. ROWCOMPARE_SUBLINK,
  574. EXPR_SUBLINK,
  575. MULTIEXPR_SUBLINK,
  576. ARRAY_SUBLINK,
  577. CTE_SUBLINK /* for SubPlans only */
  578. } SubLinkType;
  579. typedef struct SubLink
  580. {
  581. Expr xpr;
  582. SubLinkType subLinkType; /* see above */
  583. int subLinkId; /* ID (1..n); 0 if not MULTIEXPR */
  584. Node *testexpr; /* outer-query test for ALL/ANY/ROWCOMPARE */
  585. List *operName; /* originally specified operator name */
  586. Node *subselect; /* subselect as Query* or raw parsetree */
  587. int location; /* token location, or -1 if unknown */
  588. } SubLink;
  589. /*
  590. * SubPlan - executable expression node for a subplan (sub-SELECT)
  591. *
  592. * The planner replaces SubLink nodes in expression trees with SubPlan
  593. * nodes after it has finished planning the subquery. SubPlan references
  594. * a sub-plantree stored in the subplans list of the toplevel PlannedStmt.
  595. * (We avoid a direct link to make it easier to copy expression trees
  596. * without causing multiple processing of the subplan.)
  597. *
  598. * In an ordinary subplan, testexpr points to an executable expression
  599. * (OpExpr, an AND/OR tree of OpExprs, or RowCompareExpr) for the combining
  600. * operator(s); the left-hand arguments are the original lefthand expressions,
  601. * and the right-hand arguments are PARAM_EXEC Param nodes representing the
  602. * outputs of the sub-select. (NOTE: runtime coercion functions may be
  603. * inserted as well.) This is just the same expression tree as testexpr in
  604. * the original SubLink node, but the PARAM_SUBLINK nodes are replaced by
  605. * suitably numbered PARAM_EXEC nodes.
  606. *
  607. * If the sub-select becomes an initplan rather than a subplan, the executable
  608. * expression is part of the outer plan's expression tree (and the SubPlan
  609. * node itself is not, but rather is found in the outer plan's initPlan
  610. * list). In this case testexpr is NULL to avoid duplication.
  611. *
  612. * The planner also derives lists of the values that need to be passed into
  613. * and out of the subplan. Input values are represented as a list "args" of
  614. * expressions to be evaluated in the outer-query context (currently these
  615. * args are always just Vars, but in principle they could be any expression).
  616. * The values are assigned to the global PARAM_EXEC params indexed by parParam
  617. * (the parParam and args lists must have the same ordering). setParam is a
  618. * list of the PARAM_EXEC params that are computed by the sub-select, if it
  619. * is an initplan; they are listed in order by sub-select output column
  620. * position. (parParam and setParam are integer Lists, not Bitmapsets,
  621. * because their ordering is significant.)
  622. *
  623. * Also, the planner computes startup and per-call costs for use of the
  624. * SubPlan. Note that these include the cost of the subquery proper,
  625. * evaluation of the testexpr if any, and any hashtable management overhead.
  626. */
  627. typedef struct SubPlan
  628. {
  629. Expr xpr;
  630. /* Fields copied from original SubLink: */
  631. SubLinkType subLinkType; /* see above */
  632. /* The combining operators, transformed to an executable expression: */
  633. Node *testexpr; /* OpExpr or RowCompareExpr expression tree */
  634. List *paramIds; /* IDs of Params embedded in the above */
  635. /* Identification of the Plan tree to use: */
  636. int plan_id; /* Index (from 1) in PlannedStmt.subplans */
  637. /* Identification of the SubPlan for EXPLAIN and debugging purposes: */
  638. char *plan_name; /* A name assigned during planning */
  639. /* Extra data useful for determining subplan's output type: */
  640. Oid firstColType; /* Type of first column of subplan result */
  641. int32 firstColTypmod; /* Typmod of first column of subplan result */
  642. Oid firstColCollation; /* Collation of first column of
  643. * subplan result */
  644. /* Information about execution strategy: */
  645. bool useHashTable; /* TRUE to store subselect output in a hash
  646. * table (implies we are doing "IN") */
  647. bool unknownEqFalse; /* TRUE if it's okay to return FALSE when the
  648. * spec result is UNKNOWN; this allows much
  649. * simpler handling of null values */
  650. /* Information for passing params into and out of the subselect: */
  651. /* setParam and parParam are lists of integers (param IDs) */
  652. List *setParam; /* initplan subqueries have to set these
  653. * Params for parent plan */
  654. List *parParam; /* indices of input Params from parent plan */
  655. List *args; /* exprs to pass as parParam values */
  656. /* Estimated execution costs: */
  657. Cost startup_cost; /* one-time setup cost */
  658. Cost per_call_cost; /* cost for each subplan evaluation */
  659. } SubPlan;
  660. /*
  661. * AlternativeSubPlan - expression node for a choice among SubPlans
  662. *
  663. * The subplans are given as a List so that the node definition need not
  664. * change if there's ever more than two alternatives. For the moment,
  665. * though, there are always exactly two; and the first one is the fast-start
  666. * plan.
  667. */
  668. typedef struct AlternativeSubPlan
  669. {
  670. Expr xpr;
  671. List *subplans; /* SubPlan(s) with equivalent results */
  672. } AlternativeSubPlan;
  673. /* ----------------
  674. * FieldSelect
  675. *
  676. * FieldSelect represents the operation of extracting one field from a tuple
  677. * value. At runtime, the input expression is expected to yield a rowtype
  678. * Datum. The specified field number is extracted and returned as a Datum.
  679. * ----------------
  680. */
  681. typedef struct FieldSelect
  682. {
  683. Expr xpr;
  684. Expr *arg; /* input expression */
  685. AttrNumber fieldnum; /* attribute number of field to extract */
  686. Oid resulttype; /* type of the field (result type of this
  687. * node) */
  688. int32 resulttypmod; /* output typmod (usually -1) */
  689. Oid resultcollid; /* OID of collation of the field */
  690. } FieldSelect;
  691. /* ----------------
  692. * FieldStore
  693. *
  694. * FieldStore represents the operation of modifying one field in a tuple
  695. * value, yielding a new tuple value (the input is not touched!). Like
  696. * the assign case of ArrayRef, this is used to implement UPDATE of a
  697. * portion of a column.
  698. *
  699. * A single FieldStore can actually represent updates of several different
  700. * fields. The parser only generates FieldStores with single-element lists,
  701. * but the planner will collapse multiple updates of the same base column
  702. * into one FieldStore.
  703. * ----------------
  704. */
  705. typedef struct FieldStore
  706. {
  707. Expr xpr;
  708. Expr *arg; /* input tuple value */
  709. List *newvals; /* new value(s) for field(s) */
  710. List *fieldnums; /* integer list of field attnums */
  711. Oid resulttype; /* type of result (same as type of arg) */
  712. /* Like RowExpr, we deliberately omit a typmod and collation here */
  713. } FieldStore;
  714. /* ----------------
  715. * RelabelType
  716. *
  717. * RelabelType represents a "dummy" type coercion between two binary-
  718. * compatible datatypes, such as reinterpreting the result of an OID
  719. * expression as an int4. It is a no-op at runtime; we only need it
  720. * to provide a place to store the correct type to be attributed to
  721. * the expression result during type resolution. (We can't get away
  722. * with just overwriting the type field of the input expression node,
  723. * so we need a separate node to show the coercion's result type.)
  724. * ----------------
  725. */
  726. typedef struct RelabelType
  727. {
  728. Expr xpr;
  729. Expr *arg; /* input expression */
  730. Oid resulttype; /* output type of coercion expression */
  731. int32 resulttypmod; /* output typmod (usually -1) */
  732. Oid resultcollid; /* OID of collation, or InvalidOid if none */
  733. CoercionForm relabelformat; /* how to display this node */
  734. int location; /* token location, or -1 if unknown */
  735. } RelabelType;
  736. /* ----------------
  737. * CoerceViaIO
  738. *
  739. * CoerceViaIO represents a type coercion between two types whose textual
  740. * representations are compatible, implemented by invoking the source type's
  741. * typoutput function then the destination type's typinput function.
  742. * ----------------
  743. */
  744. typedef struct CoerceViaIO
  745. {
  746. Expr xpr;
  747. Expr *arg; /* input expression */
  748. Oid resulttype; /* output type of coercion */
  749. /* output typmod is not stored, but is presumed -1 */
  750. Oid resultcollid; /* OID of collation, or InvalidOid if none */
  751. CoercionForm coerceformat; /* how to display this node */
  752. int location; /* token location, or -1 if unknown */
  753. } CoerceViaIO;
  754. /* ----------------
  755. * ArrayCoerceExpr
  756. *
  757. * ArrayCoerceExpr represents a type coercion from one array type to another,
  758. * which is implemented by applying the indicated element-type coercion
  759. * function to each element of the source array. If elemfuncid is InvalidOid
  760. * then the element types are binary-compatible, but the coercion still
  761. * requires some effort (we have to fix the element type ID stored in the
  762. * array header).
  763. * ----------------
  764. */
  765. typedef struct ArrayCoerceExpr
  766. {
  767. Expr xpr;
  768. Expr *arg; /* input expression (yields an array) */
  769. Oid elemfuncid; /* OID of element coercion function, or 0 */
  770. Oid resulttype; /* output type of coercion (an array type) */
  771. int32 resulttypmod; /* output typmod (also element typmod) */
  772. Oid resultcollid; /* OID of collation, or InvalidOid if none */
  773. bool isExplicit; /* conversion semantics flag to pass to func */
  774. CoercionForm coerceformat; /* how to display this node */
  775. int location; /* token location, or -1 if unknown */
  776. } ArrayCoerceExpr;
  777. /* ----------------
  778. * ConvertRowtypeExpr
  779. *
  780. * ConvertRowtypeExpr represents a type coercion from one composite type
  781. * to another, where the source type is guaranteed to contain all the columns
  782. * needed for the destination type plus possibly others; the columns need not
  783. * be in the same positions, but are matched up by name. This is primarily
  784. * used to convert a whole-row value of an inheritance child table into a
  785. * valid whole-row value of its parent table's rowtype.
  786. * ----------------
  787. */
  788. typedef struct ConvertRowtypeExpr
  789. {
  790. Expr xpr;
  791. Expr *arg; /* input expression */
  792. Oid resulttype; /* output type (always a composite type) */
  793. /* Like RowExpr, we deliberately omit a typmod and collation here */
  794. CoercionForm convertformat; /* how to display this node */
  795. int location; /* token location, or -1 if unknown */
  796. } ConvertRowtypeExpr;
  797. /*----------
  798. * CollateExpr - COLLATE
  799. *
  800. * The planner replaces CollateExpr with RelabelType during expression
  801. * preprocessing, so execution never sees a CollateExpr.
  802. *----------
  803. */
  804. typedef struct CollateExpr
  805. {
  806. Expr xpr;
  807. Expr *arg; /* input expression */
  808. Oid collOid; /* collation's OID */
  809. int location; /* token location, or -1 if unknown */
  810. } CollateExpr;
  811. /*----------
  812. * CaseExpr - a CASE expression
  813. *
  814. * We support two distinct forms of CASE expression:
  815. * CASE WHEN boolexpr THEN expr [ WHEN boolexpr THEN expr ... ]
  816. * CASE testexpr WHEN compexpr THEN expr [ WHEN compexpr THEN expr ... ]
  817. * These are distinguishable by the "arg" field being NULL in the first case
  818. * and the testexpr in the second case.
  819. *
  820. * In the raw grammar output for the second form, the condition expressions
  821. * of the WHEN clauses are just the comparison values. Parse analysis
  822. * converts these to valid boolean expressions of the form
  823. * CaseTestExpr '=' compexpr
  824. * where the CaseTestExpr node is a placeholder that emits the correct
  825. * value at runtime. This structure is used so that the testexpr need be
  826. * evaluated only once. Note that after parse analysis, the condition
  827. * expressions always yield boolean.
  828. *
  829. * Note: we can test whether a CaseExpr has been through parse analysis
  830. * yet by checking whether casetype is InvalidOid or not.
  831. *----------
  832. */
  833. typedef struct CaseExpr
  834. {
  835. Expr xpr;
  836. Oid casetype; /* type of expression result */
  837. Oid casecollid; /* OID of collation, or InvalidOid if none */
  838. Expr *arg; /* implicit equality comparison argument */
  839. List *args; /* the arguments (list of WHEN clauses) */
  840. Expr *defresult; /* the default result (ELSE clause) */
  841. int location; /* token location, or -1 if unknown */
  842. } CaseExpr;
  843. /*
  844. * CaseWhen - one arm of a CASE expression
  845. */
  846. typedef struct CaseWhen
  847. {
  848. Expr xpr;
  849. Expr *expr; /* condition expression */
  850. Expr *result; /* substitution result */
  851. int location; /* token location, or -1 if unknown */
  852. } CaseWhen;
  853. /*
  854. * Placeholder node for the test value to be processed by a CASE expression.
  855. * This is effectively like a Param, but can be implemented more simply
  856. * since we need only one replacement value at a time.
  857. *
  858. * We also use this in nested UPDATE expressions.
  859. * See transformAssignmentIndirection().
  860. */
  861. typedef struct CaseTestExpr
  862. {
  863. Expr xpr;
  864. Oid typeId; /* type for substituted value */
  865. int32 typeMod; /* typemod for substituted value */
  866. Oid collation; /* collation for the substituted value */
  867. } CaseTestExpr;
  868. /*
  869. * ArrayExpr - an ARRAY[] expression
  870. *
  871. * Note: if multidims is false, the constituent expressions all yield the
  872. * scalar type identified by element_typeid. If multidims is true, the
  873. * constituent expressions all yield arrays of element_typeid (ie, the same
  874. * type as array_typeid); at runtime we must check for compatible subscripts.
  875. */
  876. typedef struct ArrayExpr
  877. {
  878. Expr xpr;
  879. Oid array_typeid; /* type of expression result */
  880. Oid array_collid; /* OID of collation, or InvalidOid if none */
  881. Oid element_typeid; /* common type of array elements */
  882. List *elements; /* the array elements or sub-arrays */
  883. bool multidims; /* true if elements are sub-arrays */
  884. int location; /* token location, or -1 if unknown */
  885. } ArrayExpr;
  886. /*
  887. * RowExpr - a ROW() expression
  888. *
  889. * Note: the list of fields must have a one-for-one correspondence with
  890. * physical fields of the associated rowtype, although it is okay for it
  891. * to be shorter than the rowtype. That is, the N'th list element must
  892. * match up with the N'th physical field. When the N'th physical field
  893. * is a dropped column (attisdropped) then the N'th list element can just
  894. * be a NULL constant. (This case can only occur for named composite types,
  895. * not RECORD types, since those are built from the RowExpr itself rather
  896. * than vice versa.) It is important not to assume that length(args) is
  897. * the same as the number of columns logically present in the rowtype.
  898. *
  899. * colnames provides field names in cases where the names can't easily be
  900. * obtained otherwise. Names *must* be provided if row_typeid is RECORDOID.
  901. * If row_typeid identifies a known composite type, colnames can be NIL to
  902. * indicate the type's cataloged field names apply. Note that colnames can
  903. * be non-NIL even for a composite type, and typically is when the RowExpr
  904. * was created by expanding a whole-row Var. This is so that we can retain
  905. * the column alias names of the RTE that the Var referenced (which would
  906. * otherwise be very difficult to extract from the parsetree). Like the
  907. * args list, colnames is one-for-one with physical fields of the rowtype.
  908. */
  909. typedef struct RowExpr
  910. {
  911. Expr xpr;
  912. List *args; /* the fields */
  913. Oid row_typeid; /* RECORDOID or a composite type's ID */
  914. /*
  915. * Note: we deliberately do NOT store a typmod. Although a typmod will be
  916. * associated with specific RECORD types at runtime, it will differ for
  917. * different backends, and so cannot safely be stored in stored
  918. * parsetrees. We must assume typmod -1 for a RowExpr node.
  919. *
  920. * We don't need to store a collation either. The result type is
  921. * necessarily composite, and composite types never have a collation.
  922. */
  923. CoercionForm row_format; /* how to display this node */
  924. List *colnames; /* list of String, or NIL */
  925. int location; /* token location, or -1 if unknown */
  926. } RowExpr;
  927. /*
  928. * RowCompareExpr - row-wise comparison, such as (a, b) <= (1, 2)
  929. *
  930. * We support row comparison for any operator that can be determined to
  931. * act like =, <>, <, <=, >, or >= (we determine this by looking for the
  932. * operator in btree opfamilies). Note that the same operator name might
  933. * map to a different operator for each pair of row elements, since the
  934. * element datatypes can vary.
  935. *
  936. * A RowCompareExpr node is only generated for the < <= > >= cases;
  937. * the = and <> cases are translated to simple AND or OR combinations
  938. * of the pairwise comparisons. However, we include = and <> in the
  939. * RowCompareType enum for the convenience of parser logic.
  940. */
  941. typedef enum RowCompareType
  942. {
  943. /* Values of this enum are chosen to match btree strategy numbers */
  944. ROWCOMPARE_LT = 1, /* BTLessStrategyNumber */
  945. ROWCOMPARE_LE = 2, /* BTLessEqualStrategyNumber */
  946. ROWCOMPARE_EQ = 3, /* BTEqualStrategyNumber */
  947. ROWCOMPARE_GE = 4, /* BTGreaterEqualStrategyNumber */
  948. ROWCOMPARE_GT = 5, /* BTGreaterStrategyNumber */
  949. ROWCOMPARE_NE = 6 /* no such btree strategy */
  950. } RowCompareType;
  951. typedef struct RowCompareExpr
  952. {
  953. Expr xpr;
  954. RowCompareType rctype; /* LT LE GE or GT, never EQ or NE */
  955. List *opnos; /* OID list of pairwise comparison ops */
  956. List *opfamilies; /* OID list of containing operator families */
  957. List *inputcollids; /* OID list of collations for comparisons */
  958. List *largs; /* the left-hand input arguments */
  959. List *rargs; /* the right-hand input arguments */
  960. } RowCompareExpr;
  961. /*
  962. * CoalesceExpr - a COALESCE expression
  963. */
  964. typedef struct CoalesceExpr
  965. {
  966. Expr xpr;
  967. Oid coalescetype; /* type of expression result */
  968. Oid coalescecollid; /* OID of collation, or InvalidOid if none */
  969. List *args; /* the arguments */
  970. int location; /* token location, or -1 if unknown */
  971. } CoalesceExpr;
  972. /*
  973. * MinMaxExpr - a GREATEST or LEAST function
  974. */
  975. typedef enum MinMaxOp
  976. {
  977. IS_GREATEST,
  978. IS_LEAST
  979. } MinMaxOp;
  980. typedef struct MinMaxExpr
  981. {
  982. Expr xpr;
  983. Oid minmaxtype; /* common type of arguments and result */
  984. Oid minmaxcollid; /* OID of collation of result */
  985. Oid inputcollid; /* OID of collation that function should use */
  986. MinMaxOp op; /* function to execute */
  987. List *args; /* the arguments */
  988. int location; /* token location, or -1 if unknown */
  989. } MinMaxExpr;
  990. /*
  991. * XmlExpr - various SQL/XML functions requiring special grammar productions
  992. *
  993. * 'name' carries the "NAME foo" argument (already XML-escaped).
  994. * 'named_args' and 'arg_names' represent an xml_attribute list.
  995. * 'args' carries all other arguments.
  996. *
  997. * Note: result type/typmod/collation are not stored, but can be deduced
  998. * from the XmlExprOp. The type/typmod fields are just used for display
  999. * purposes, and are NOT necessarily the true result type of the node.
  1000. */
  1001. typedef enum XmlExprOp
  1002. {
  1003. IS_XMLCONCAT, /* XMLCONCAT(args) */
  1004. IS_XMLELEMENT, /* XMLELEMENT(name, xml_attributes, args) */
  1005. IS_XMLFOREST, /* XMLFOREST(xml_attributes) */
  1006. IS_XMLPARSE, /* XMLPARSE(text, is_doc, preserve_ws) */
  1007. IS_XMLPI, /* XMLPI(name [, args]) */
  1008. IS_XMLROOT, /* XMLROOT(xml, version, standalone) */
  1009. IS_XMLSERIALIZE, /* XMLSERIALIZE(is_document, xmlval) */
  1010. IS_DOCUMENT /* xmlval IS DOCUMENT */
  1011. } XmlExprOp;
  1012. typedef enum
  1013. {
  1014. XMLOPTION_DOCUMENT,
  1015. XMLOPTION_CONTENT
  1016. } XmlOptionType;
  1017. typedef struct XmlExpr
  1018. {
  1019. Expr xpr;
  1020. XmlExprOp op; /* xml function ID */
  1021. char *name; /* name in xml(NAME foo ...) syntaxes */
  1022. List *named_args; /* non-XML expressions for xml_attributes */
  1023. List *arg_names; /* parallel list of Value strings */
  1024. List *args; /* list of expressions */
  1025. XmlOptionType xmloption; /* DOCUMENT or CONTENT */
  1026. Oid type; /* target type/typmod for XMLSERIALIZE */
  1027. int32 typmod;
  1028. int location; /* token location, or -1 if unknown */
  1029. } XmlExpr;
  1030. /* ----------------
  1031. * NullTest
  1032. *
  1033. * NullTest represents the operation of testing a value for NULLness.
  1034. * The appropriate test is performed and returned as a boolean Datum.
  1035. *
  1036. * When argisrow is false, this simply represents a test for the null value.
  1037. *
  1038. * When argisrow is true, the input expression must yield a rowtype, and
  1039. * the node implements "row IS [NOT] NULL" per the SQL standard. This
  1040. * includes checking individual fields for NULLness when the row datum
  1041. * itself isn't NULL.
  1042. *
  1043. * NOTE: the combination of a rowtype input and argisrow==false does NOT
  1044. * correspond to the SQL notation "row IS [NOT] NULL"; instead, this case
  1045. * represents the SQL notation "row IS [NOT] DISTINCT FROM NULL".
  1046. * ----------------
  1047. */
  1048. typedef enum NullTestType
  1049. {
  1050. IS_NULL, IS_NOT_NULL
  1051. } NullTestType;
  1052. typedef struct NullTest
  1053. {
  1054. Expr xpr;
  1055. Expr *arg; /* input expression */
  1056. NullTestType nulltesttype; /* IS NULL, IS NOT NULL */
  1057. bool argisrow; /* T to perform field-by-field null checks */
  1058. int location; /* token location, or -1 if unknown */
  1059. } NullTest;
  1060. /*
  1061. * BooleanTest
  1062. *
  1063. * BooleanTest represents the operation of determining whether a boolean
  1064. * is TRUE, FALSE, or UNKNOWN (ie, NULL). All six meaningful combinations
  1065. * are supported. Note that a NULL input does *not* cause a NULL result.
  1066. * The appropriate test is performed and returned as a boolean Datum.
  1067. */
  1068. typedef enum BoolTestType
  1069. {
  1070. IS_TRUE, IS_NOT_TRUE, IS_FALSE, IS_NOT_FALSE, IS_UNKNOWN, IS_NOT_UNKNOWN
  1071. } BoolTestType;
  1072. typedef struct BooleanTest
  1073. {
  1074. Expr xpr;
  1075. Expr *arg; /* input expression */
  1076. BoolTestType booltesttype; /* test type */
  1077. int location; /* token location, or -1 if unknown */
  1078. } BooleanTest;
  1079. /*
  1080. * CoerceToDomain
  1081. *
  1082. * CoerceToDomain represents the operation of coercing a value to a domain
  1083. * type. At runtime (and not before) the precise set of constraints to be
  1084. * checked will be determined. If the value passes, it is returned as the
  1085. * result; if not, an error is raised. Note that this is equivalent to
  1086. * RelabelType in the scenario where no constraints are applied.
  1087. */
  1088. typedef struct CoerceToDomain
  1089. {
  1090. Expr xpr;
  1091. Expr *arg; /* input expression */
  1092. Oid resulttype; /* domain type ID (result type) */
  1093. int32 resulttypmod; /* output typmod (currently always -1) */
  1094. Oid resultcollid; /* OID of collation, or InvalidOid if none */
  1095. CoercionForm coercionformat; /* how to display this node */
  1096. int location; /* token location, or -1 if unknown */
  1097. } CoerceToDomain;
  1098. /*
  1099. * Placeholder node for the value to be processed by a domain's check
  1100. * constraint. This is effectively like a Param, but can be implemented more
  1101. * simply since we need only one replacement value at a time.
  1102. *
  1103. * Note: the typeId/typeMod/collation will be set from the domain's base type,
  1104. * not the domain itself. This is because we shouldn't consider the value
  1105. * to be a member of the domain if we haven't yet checked its constraints.
  1106. */
  1107. typedef struct CoerceToDomainValue
  1108. {
  1109. Expr xpr;
  1110. Oid typeId; /* type for substituted value */
  1111. int32 typeMod; /* typemod for substituted value */
  1112. Oid collation; /* collation for the substituted value */
  1113. int location; /* token location, or -1 if unknown */
  1114. } CoerceToDomainValue;
  1115. /*
  1116. * Placeholder node for a DEFAULT marker in an INSERT or UPDATE command.
  1117. *
  1118. * This is not an executable expression: it must be replaced by the actual
  1119. * column default expression during rewriting. But it is convenient to
  1120. * treat it as an expression node during parsing and rewriting.
  1121. */
  1122. typedef struct SetToDefault
  1123. {
  1124. Expr xpr;
  1125. Oid typeId; /* type for substituted value */
  1126. int32 typeMod; /* typemod for substituted value */
  1127. Oid collation; /* collation for the substituted value */
  1128. int location; /* token location, or -1 if unknown */
  1129. } SetToDefault;
  1130. /*
  1131. * Node representing [WHERE] CURRENT OF cursor_name
  1132. *
  1133. * CURRENT OF is a bit like a Var, in that it carries the rangetable index
  1134. * of the target relation being constrained; this aids placing the expression
  1135. * correctly during planning. We can assume however that its "levelsup" is
  1136. * always zero, due to the syntactic constraints on where it can appear.
  1137. *
  1138. * The referenced cursor can be represented either as a hardwired string
  1139. * or as a reference to a run-time parameter of type REFCURSOR. The latter
  1140. * case is for the convenience of plpgsql.
  1141. */
  1142. typedef struct CurrentOfExpr
  1143. {
  1144. Expr xpr;
  1145. Index cvarno; /* RT index of target relation */
  1146. char *cursor_name; /* name of referenced cursor, or NULL */
  1147. int cursor_param; /* refcursor parameter number, or 0 */
  1148. } CurrentOfExpr;
  1149. /*
  1150. * InferenceElem - an element of a unique index inference specification
  1151. *
  1152. * This mostly matches the structure of IndexElems, but having a dedicated
  1153. * primnode allows for a clean separation between the use of index parameters
  1154. * by utility commands, and this node.
  1155. */
  1156. typedef struct InferenceElem
  1157. {
  1158. Expr xpr;
  1159. Node *expr; /* expression to infer from, or NULL */
  1160. Oid infercollid; /* OID of collation, or InvalidOid */
  1161. Oid inferopclass; /* OID of att opclass, or InvalidOid */
  1162. } InferenceElem;
  1163. /*--------------------
  1164. * TargetEntry -
  1165. * a target entry (used in query target lists)
  1166. *
  1167. * Strictly speaking, a TargetEntry isn't an expression node (since it can't
  1168. * be evaluated by ExecEvalExpr). But we treat it as one anyway, since in
  1169. * very many places it's convenient to process a whole query targetlist as a
  1170. * single expression tree.
  1171. *
  1172. * In a SELECT's targetlist, resno should always be equal to the item's
  1173. * ordinal position (counting from 1). However, in an INSERT or UPDATE
  1174. * targetlist, resno represents the attribute number of the destination
  1175. * column for the item; so there may be missing or out-of-order resnos.
  1176. * It is even legal to have duplicated resnos; consider
  1177. * UPDATE table SET arraycol[1] = ..., arraycol[2] = ..., ...
  1178. * The two meanings come together in the executor, because the planner
  1179. * transforms INSERT/UPDATE tlists into a normalized form with exactly
  1180. * one entry for each column of the destination table. Before that's
  1181. * happened, however, it is risky to assume that resno == position.
  1182. * Generally get_tle_by_resno() should be used rather than list_nth()
  1183. * to fetch tlist entries by resno, and only in SELECT should you assume
  1184. * that resno is a unique identifier.
  1185. *
  1186. * resname is required to represent the correct column name in non-resjunk
  1187. * entries of top-level SELECT targetlists, since it will be used as the
  1188. * column title sent to the frontend. In most other contexts it is only
  1189. * a debugging aid, and may be wrong or even NULL. (In particular, it may
  1190. * be wrong in a tlist from a stored rule, if the referenced column has been
  1191. * renamed by ALTER TABLE since the rule was made. Also, the planner tends
  1192. * to store NULL rather than look up a valid name for tlist entries in
  1193. * non-toplevel plan nodes.) In resjunk entries, resname should be either
  1194. * a specific system-generated name (such as "ctid") or NULL; anything else
  1195. * risks confusing ExecGetJunkAttribute!
  1196. *
  1197. * ressortgroupref is used in the representation of ORDER BY, GROUP BY, and
  1198. * DISTINCT items. Targetlist entries with ressortgroupref=0 are not
  1199. * sort/group items. If ressortgroupref>0, then this item is an ORDER BY,
  1200. * GROUP BY, and/or DISTINCT target value. No two entries in a targetlist
  1201. * may have the same nonzero ressortgroupref --- but there is no particular
  1202. * meaning to the nonzero values, except as tags. (For example, one must
  1203. * not assume that lower ressortgroupref means a more significant sort key.)
  1204. * The order of the associated SortGroupClause lists determine the semantics.
  1205. *
  1206. * resorigtbl/resorigcol identify the source of the column, if it is a
  1207. * simple reference to a column of a base table (or view). If it is not
  1208. * a simple reference, these fields are zeroes.
  1209. *
  1210. * If resjunk is true then the column is a working column (such as a sort key)
  1211. * that should be removed from the final output of the query. Resjunk columns
  1212. * must have resnos that cannot duplicate any regular column's resno. Also
  1213. * note that there are places that assume resjunk columns come after non-junk
  1214. * columns.
  1215. *--------------------
  1216. */
  1217. typedef struct TargetEntry
  1218. {
  1219. Expr xpr;
  1220. Expr *expr; /* expression to evaluate */
  1221. AttrNumber resno; /* attribute number (see notes above) */
  1222. char *resname; /* name of the column (could be NULL) */
  1223. Index ressortgroupref;/* nonzero if referenced by a sort/group
  1224. * clause */
  1225. Oid resorigtbl; /* OID of column's source table */
  1226. AttrNumber resorigcol; /* column's number in source table */
  1227. bool resjunk; /* set to true to eliminate the attribute from
  1228. * final target list */
  1229. } TargetEntry;
  1230. /* ----------------------------------------------------------------
  1231. * node types for join trees
  1232. *
  1233. * The leaves of a join tree structure are RangeTblRef nodes. Above
  1234. * these, JoinExpr nodes can appear to denote a specific kind of join
  1235. * or qualified join. Also, FromExpr nodes can appear to denote an
  1236. * ordinary cross-product join ("FROM foo, bar, baz WHERE ...").
  1237. * FromExpr is like a JoinExpr of jointype JOIN_INNER, except that it
  1238. * may have any number of child nodes, not just two.
  1239. *
  1240. * NOTE: the top level of a Query's jointree is always a FromExpr.
  1241. * Even if the jointree contains no rels, there will be a FromExpr.
  1242. *
  1243. * NOTE: the qualification expressions present in JoinExpr nodes are
  1244. * *in addition to* the query's main WHERE clause, which appears as the
  1245. * qual of the top-level FromExpr. The reason for associating quals with
  1246. * specific nodes in the jointree is that the position of a qual is critical
  1247. * when outer joins are present. (If we enforce a qual too soon or too late,
  1248. * that may cause the outer join to produce the wrong set of NULL-extended
  1249. * rows.) If all joins are inner joins then all the qual positions are
  1250. * semantically interchangeable.
  1251. *
  1252. * NOTE: in the raw output of gram.y, a join tree contains RangeVar,
  1253. * RangeSubselect, and RangeFunction nodes, which are all replaced by
  1254. * RangeTblRef nodes during the parse analysis phase. Also, the top-level
  1255. * FromExpr is added during parse analysis; the grammar regards FROM and
  1256. * WHERE as separate.
  1257. * ----------------------------------------------------------------
  1258. */
  1259. /*
  1260. * RangeTblRef - reference to an entry in the query's rangetable
  1261. *
  1262. * We could use direct pointers to the RT entries and skip having these
  1263. * nodes, but multiple pointers to the same node in a querytree cause
  1264. * lots of headaches, so it seems better to store an index into the RT.
  1265. */
  1266. typedef struct RangeTblRef
  1267. {
  1268. NodeTag type;
  1269. int rtindex;
  1270. } RangeTblRef;
  1271. /*----------
  1272. * JoinExpr - for SQL JOIN expressions
  1273. *
  1274. * isNatural, usingClause, and quals are interdependent. The user can write
  1275. * only one of NATURAL, USING(), or ON() (this is enforced by the grammar).
  1276. * If he writes NATURAL then parse analysis generates the equivalent USING()
  1277. * list, and from that fills in "quals" with the right equality comparisons.
  1278. * If he writes USING() then "quals" is filled with equality comparisons.
  1279. * If he writes ON() then only "quals" is set. Note that NATURAL/USING
  1280. * are not equivalent to ON() since they also affect the output column list.
  1281. *
  1282. * alias is an Alias node representing the AS alias-clause attached to the
  1283. * join expression, or NULL if no clause. NB: presence or absence of the
  1284. * alias has a critical impact on semantics, because a join with an alias
  1285. * restricts visibility of the tables/columns inside it.
  1286. *
  1287. * During parse analysis, an RTE is created for the Join, and its index
  1288. * is filled into rtindex. This RTE is present mainly so that Vars can
  1289. * be created that refer to the outputs of the join. The planner sometimes
  1290. * generates JoinExprs internally; these can have rtindex = 0 if there are
  1291. * no join alias variables referencing such joins.
  1292. *----------
  1293. */
  1294. typedef struct JoinExpr
  1295. {
  1296. NodeTag type;
  1297. JoinType jointype; /* type of join */
  1298. bool isNatural; /* Natural join? Will need to shape table */
  1299. Node *larg; /* left subtree */
  1300. Node *rarg; /* right subtree */
  1301. List *usingClause; /* USING clause, if any (list of String) */
  1302. Node *quals; /* qualifiers on join, if any */
  1303. Alias *alias; /* user-written alias clause, if any */
  1304. int rtindex; /* RT index assigned for join, or 0 */
  1305. } JoinExpr;
  1306. /*----------
  1307. * FromExpr - represents a FROM ... WHERE ... construct
  1308. *
  1309. * This is both more flexible than a JoinExpr (it can have any number of
  1310. * children, including zero) and less so --- we don't need to deal with
  1311. * aliases and so on. The output column set is implicitly just the union
  1312. * of the outputs of the children.
  1313. *----------
  1314. */
  1315. typedef struct FromExpr
  1316. {
  1317. NodeTag type;
  1318. List *fromlist; /* List of join subtrees */
  1319. Node *quals; /* qualifiers on join, if any */
  1320. } FromExpr;
  1321. /*----------
  1322. * OnConflictExpr - represents an ON CONFLICT DO ... expression
  1323. *
  1324. * The optimizer requires a list of inference elements, and optionally a WHERE
  1325. * clause to infer a unique index. The unique index (or, occasionally,
  1326. * indexes) inferred are used to arbitrate whether or not the alternative ON
  1327. * CONFLICT path is taken.
  1328. *----------
  1329. */
  1330. typedef struct OnConflictExpr
  1331. {
  1332. NodeTag type;
  1333. OnConflictAction action; /* DO NOTHING or UPDATE? */
  1334. /* Arbiter */
  1335. List *arbiterElems; /* unique index arbiter list (of
  1336. * InferenceElem's) */
  1337. Node *arbiterWhere; /* unique index arbiter WHERE clause */
  1338. Oid constraint; /* pg_constraint OID for arbiter */
  1339. /* ON CONFLICT UPDATE */
  1340. List *onConflictSet; /* List of ON CONFLICT SET TargetEntrys */
  1341. Node *onConflictWhere; /* qualifiers to restrict UPDATE to */
  1342. int exclRelIndex; /* RT index of 'excluded' relation */
  1343. List *exclRelTlist; /* tlist of the EXCLUDED pseudo relation */
  1344. } OnConflictExpr;
  1345. #endif /* PRIMNODES_H */