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