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