1 /*-------------------------------------------------------------------------
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
10 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/include/nodes/primnodes.h
15 *-------------------------------------------------------------------------
20 #include "access/attnum.h"
21 #include "nodes/pg_list.h"
24 /* ----------------------------------------------------------------
26 * ----------------------------------------------------------------
31 * specifies an alias for a range variable; the alias might also
32 * specify renaming of columns within the table.
34 * Note: colnames is a list of Value nodes (always strings). In Alias structs
35 * associated with RTEs, there may be entries corresponding to dropped
36 * columns; these are normally empty strings (""). See parsenodes.h for info.
41 char *aliasname; /* aliased rel name (never qualified) */
42 List *colnames; /* optional list of column aliases */
45 typedef enum InhOption
47 INH_NO, /* Do NOT scan child tables */
48 INH_YES, /* DO scan child tables */
49 INH_DEFAULT /* Use current SQL_inheritance option */
52 /* What to do at commit time for temporary relations */
53 typedef enum OnCommitAction
55 ONCOMMIT_NOOP, /* No ON COMMIT clause (do nothing) */
56 ONCOMMIT_PRESERVE_ROWS, /* ON COMMIT PRESERVE ROWS (do nothing) */
57 ONCOMMIT_DELETE_ROWS, /* ON COMMIT DELETE ROWS */
58 ONCOMMIT_DROP /* ON COMMIT DROP */
62 * RangeVar - range variable, used in FROM clauses
64 * Also used to represent table names in utility statements; there, the alias
65 * field is not used, and inhOpt shows whether to apply the operation
66 * recursively to child tables. In some contexts it is also useful to carry
67 * a TEMP table indication here.
69 typedef struct RangeVar
72 char *catalogname; /* the catalog (database) name, or NULL */
73 char *schemaname; /* the schema name, or NULL */
74 char *relname; /* the relation/sequence name */
75 InhOption inhOpt; /* expand rel by inheritance? recursively act
77 char relpersistence; /* see RELPERSISTENCE_* in pg_class.h */
78 Alias *alias; /* table alias & optional column aliases */
79 int location; /* token location, or -1 if unknown */
83 * IntoClause - target information for SELECT INTO and CREATE TABLE AS
85 typedef struct IntoClause
89 RangeVar *rel; /* target relation name */
90 List *colNames; /* column names to assign, or NIL */
91 List *options; /* options from WITH clause */
92 OnCommitAction onCommit; /* what do we do at COMMIT? */
93 char *tableSpaceName; /* table space to use, or NULL */
94 bool skipData; /* true for WITH NO DATA */
98 /* ----------------------------------------------------------------
99 * node types for executable expressions
100 * ----------------------------------------------------------------
104 * Expr - generic superclass for executable-expression nodes
106 * All node types that are used in executable expression trees should derive
107 * from Expr (that is, have Expr as their first field). Since Expr only
108 * contains NodeTag, this is a formality, but it is an easy form of
109 * documentation. See also the ExprState node types in execnodes.h.
117 * Var - expression node representing a variable (ie, a table column)
119 * Note: during parsing/planning, varnoold/varoattno are always just copies
120 * of varno/varattno. At the tail end of planning, Var nodes appearing in
121 * upper-level plan nodes are reassigned to point to the outputs of their
122 * subplans; for example, in a join node varno becomes INNER_VAR or OUTER_VAR
123 * and varattno becomes the index of the proper element of that subplan's
124 * target list. But varnoold/varoattno continue to hold the original values.
125 * The code doesn't really need varnoold/varoattno, but they are very useful
126 * for debugging and interpreting completed plans, so we keep them around.
128 #define INNER_VAR 65000 /* reference to inner subplan */
129 #define OUTER_VAR 65001 /* reference to outer subplan */
130 #define INDEX_VAR 65002 /* reference to index column */
132 #define IS_SPECIAL_VARNO(varno) ((varno) >= INNER_VAR)
134 /* Symbols for the indexes of the special RTE entries in rules */
135 #define PRS2_OLD_VARNO 1
136 #define PRS2_NEW_VARNO 2
141 Index varno; /* index of this var's relation in the range
142 * table, or INNER_VAR/OUTER_VAR/INDEX_VAR */
143 AttrNumber varattno; /* attribute number of this var, or zero for
145 Oid vartype; /* pg_type OID for the type of this var */
146 int32 vartypmod; /* pg_attribute typmod value */
147 Oid varcollid; /* OID of collation, or InvalidOid if none */
148 Index varlevelsup; /* for subquery variables referencing outer
149 * relations; 0 in a normal var, >0 means N
151 Index varnoold; /* original value of varno, for debugging */
152 AttrNumber varoattno; /* original value of varattno */
153 int location; /* token location, or -1 if unknown */
162 Oid consttype; /* pg_type OID of the constant's datatype */
163 int32 consttypmod; /* typmod value, if any */
164 Oid constcollid; /* OID of collation, or InvalidOid if none */
165 int constlen; /* typlen of the constant's datatype */
166 Datum constvalue; /* the constant's value */
167 bool constisnull; /* whether the constant is null (if true,
168 * constvalue is undefined) */
169 bool constbyval; /* whether this datatype is passed by value.
170 * If true, then all the information is stored
171 * in the Datum. If false, then the Datum
172 * contains a pointer to the information. */
173 int location; /* token location, or -1 if unknown */
178 * paramkind - specifies the kind of parameter. The possible values
179 * for this field are:
181 * PARAM_EXTERN: The parameter value is supplied from outside the plan.
182 * Such parameters are numbered from 1 to n.
184 * PARAM_EXEC: The parameter is an internal executor parameter, used
185 * for passing values into and out of sub-queries or from
186 * nestloop joins to their inner scans.
187 * For historical reasons, such parameters are numbered from 0.
188 * These numbers are independent of PARAM_EXTERN numbers.
190 * PARAM_SUBLINK: The parameter represents an output column of a SubLink
191 * node's sub-select. The column number is contained in the
192 * `paramid' field. (This type of Param is converted to
193 * PARAM_EXEC during planning.)
195 * Note: currently, paramtypmod is valid for PARAM_SUBLINK Params, and for
196 * PARAM_EXEC Params generated from them; it is always -1 for PARAM_EXTERN
197 * params, since the APIs that supply values for such parameters don't carry
201 typedef enum ParamKind
211 ParamKind paramkind; /* kind of parameter. See above */
212 int paramid; /* numeric ID for parameter */
213 Oid paramtype; /* pg_type OID of parameter's datatype */
214 int32 paramtypmod; /* typmod value, if known */
215 Oid paramcollid; /* OID of collation, or InvalidOid if none */
216 int location; /* token location, or -1 if unknown */
222 * The aggregate's args list is a targetlist, ie, a list of TargetEntry nodes
223 * (before Postgres 9.0 it was just bare expressions). The non-resjunk TLEs
224 * represent the aggregate's regular arguments (if any) and resjunk TLEs can
225 * be added at the end to represent ORDER BY expressions that are not also
226 * arguments. As in a top-level Query, the TLEs can be marked with
227 * ressortgroupref indexes to let them be referenced by SortGroupClause
228 * entries in the aggorder and/or aggdistinct lists. This represents ORDER BY
229 * and DISTINCT operations to be applied to the aggregate input rows before
230 * they are passed to the transition function. The grammar only allows a
231 * simple "DISTINCT" specifier for the arguments, but we use the full
232 * query-level representation to allow more code sharing.
234 typedef struct Aggref
237 Oid aggfnoid; /* pg_proc Oid of the aggregate */
238 Oid aggtype; /* type Oid of result of the aggregate */
239 Oid aggcollid; /* OID of collation of result */
240 Oid inputcollid; /* OID of collation that function should use */
241 List *args; /* arguments and sort expressions */
242 List *aggorder; /* ORDER BY (list of SortGroupClause) */
243 List *aggdistinct; /* DISTINCT (list of SortGroupClause) */
244 bool aggstar; /* TRUE if argument list was really '*' */
245 Index agglevelsup; /* > 0 if agg belongs to outer query */
246 int location; /* token location, or -1 if unknown */
252 typedef struct WindowFunc
255 Oid winfnoid; /* pg_proc Oid of the function */
256 Oid wintype; /* type Oid of result of the window function */
257 Oid wincollid; /* OID of collation of result */
258 Oid inputcollid; /* OID of collation that function should use */
259 List *args; /* arguments to the window function */
260 Index winref; /* index of associated WindowClause */
261 bool winstar; /* TRUE if argument list was really '*' */
262 bool winagg; /* is function a simple aggregate? */
263 int location; /* token location, or -1 if unknown */
267 * ArrayRef: describes an array subscripting operation
269 * An ArrayRef can describe fetching a single element from an array,
270 * fetching a subarray (array slice), storing a single element into
271 * an array, or storing a slice. The "store" cases work with an
272 * initial array value and a source value that is inserted into the
273 * appropriate part of the array; the result of the operation is an
274 * entire new modified array value.
276 * If reflowerindexpr = NIL, then we are fetching or storing a single array
277 * element at the subscripts given by refupperindexpr. Otherwise we are
278 * fetching or storing an array slice, that is a rectangular subarray
279 * with lower and upper bounds given by the index expressions.
280 * reflowerindexpr must be the same length as refupperindexpr when it
283 * Note: the result datatype is the element type when fetching a single
284 * element; but it is the array type when doing subarray fetch or either
288 typedef struct ArrayRef
291 Oid refarraytype; /* type of the array proper */
292 Oid refelemtype; /* type of the array elements */
293 int32 reftypmod; /* typmod of the array (and elements too) */
294 Oid refcollid; /* OID of collation, or InvalidOid if none */
295 List *refupperindexpr;/* expressions that evaluate to upper array
297 List *reflowerindexpr;/* expressions that evaluate to lower array
299 Expr *refexpr; /* the expression that evaluates to an array
301 Expr *refassgnexpr; /* expression for the source value, or NULL if
306 * CoercionContext - distinguishes the allowed set of type casts
308 * NB: ordering of the alternatives is significant; later (larger) values
309 * allow more casts than earlier ones.
311 typedef enum CoercionContext
313 COERCION_IMPLICIT, /* coercion in context of expression */
314 COERCION_ASSIGNMENT, /* coercion in context of assignment */
315 COERCION_EXPLICIT /* explicit cast operation */
319 * CoercionForm - information showing how to display a function-call node
321 typedef enum CoercionForm
323 COERCE_EXPLICIT_CALL, /* display as a function call */
324 COERCE_EXPLICIT_CAST, /* display as an explicit cast */
325 COERCE_IMPLICIT_CAST, /* implicit cast, so hide it */
326 COERCE_DONTCARE /* special case for planner */
330 * FuncExpr - expression node for a function call
332 typedef struct FuncExpr
335 Oid funcid; /* PG_PROC OID of the function */
336 Oid funcresulttype; /* PG_TYPE OID of result value */
337 bool funcretset; /* true if function returns set */
338 CoercionForm funcformat; /* how to display this function call */
339 Oid funccollid; /* OID of collation of result */
340 Oid inputcollid; /* OID of collation that function should use */
341 List *args; /* arguments to the function */
342 int location; /* token location, or -1 if unknown */
346 * NamedArgExpr - a named argument of a function
348 * This node type can only appear in the args list of a FuncCall or FuncExpr
349 * node. We support pure positional call notation (no named arguments),
350 * named notation (all arguments are named), and mixed notation (unnamed
351 * arguments followed by named ones).
353 * Parse analysis sets argnumber to the positional index of the argument,
354 * but doesn't rearrange the argument list.
356 * The planner will convert argument lists to pure positional notation
357 * during expression preprocessing, so execution never sees a NamedArgExpr.
359 typedef struct NamedArgExpr
362 Expr *arg; /* the argument expression */
363 char *name; /* the name */
364 int argnumber; /* argument's number in positional notation */
365 int location; /* argument name location, or -1 if unknown */
369 * OpExpr - expression node for an operator invocation
371 * Semantically, this is essentially the same as a function call.
373 * Note that opfuncid is not necessarily filled in immediately on creation
374 * of the node. The planner makes sure it is valid before passing the node
375 * tree to the executor, but during parsing/planning opfuncid can be 0.
377 typedef struct OpExpr
380 Oid opno; /* PG_OPERATOR OID of the operator */
381 Oid opfuncid; /* PG_PROC OID of underlying function */
382 Oid opresulttype; /* PG_TYPE OID of result value */
383 bool opretset; /* true if operator returns set */
384 Oid opcollid; /* OID of collation of result */
385 Oid inputcollid; /* OID of collation that operator should use */
386 List *args; /* arguments to the operator (1 or 2) */
387 int location; /* token location, or -1 if unknown */
391 * DistinctExpr - expression node for "x IS DISTINCT FROM y"
393 * Except for the nodetag, this is represented identically to an OpExpr
394 * referencing the "=" operator for x and y.
395 * We use "=", not the more obvious "<>", because more datatypes have "="
396 * than "<>". This means the executor must invert the operator result.
397 * Note that the operator function won't be called at all if either input
398 * is NULL, since then the result can be determined directly.
400 typedef OpExpr DistinctExpr;
403 * NullIfExpr - a NULLIF expression
405 * Like DistinctExpr, this is represented the same as an OpExpr referencing
406 * the "=" operator for x and y.
408 typedef OpExpr NullIfExpr;
411 * ScalarArrayOpExpr - expression node for "scalar op ANY/ALL (array)"
413 * The operator must yield boolean. It is applied to the left operand
414 * and each element of the righthand array, and the results are combined
415 * with OR or AND (for ANY or ALL respectively). The node representation
416 * is almost the same as for the underlying operator, but we need a useOr
417 * flag to remember whether it's ANY or ALL, and we don't have to store
418 * the result type (or the collation) because it must be boolean.
420 typedef struct ScalarArrayOpExpr
423 Oid opno; /* PG_OPERATOR OID of the operator */
424 Oid opfuncid; /* PG_PROC OID of underlying function */
425 bool useOr; /* true for ANY, false for ALL */
426 Oid inputcollid; /* OID of collation that operator should use */
427 List *args; /* the scalar and array operands */
428 int location; /* token location, or -1 if unknown */
432 * BoolExpr - expression node for the basic Boolean operators AND, OR, NOT
434 * Notice the arguments are given as a List. For NOT, of course the list
435 * must always have exactly one element. For AND and OR, the executor can
436 * handle any number of arguments. The parser generally treats AND and OR
437 * as binary and so it typically only produces two-element lists, but the
438 * optimizer will flatten trees of AND and OR nodes to produce longer lists
439 * when possible. There are also a few special cases where more arguments
440 * can appear before optimization.
442 typedef enum BoolExprType
444 AND_EXPR, OR_EXPR, NOT_EXPR
447 typedef struct BoolExpr
451 List *args; /* arguments to this expression */
452 int location; /* token location, or -1 if unknown */
458 * A SubLink represents a subselect appearing in an expression, and in some
459 * cases also the combining operator(s) just above it. The subLinkType
460 * indicates the form of the expression represented:
461 * EXISTS_SUBLINK EXISTS(SELECT ...)
462 * ALL_SUBLINK (lefthand) op ALL (SELECT ...)
463 * ANY_SUBLINK (lefthand) op ANY (SELECT ...)
464 * ROWCOMPARE_SUBLINK (lefthand) op (SELECT ...)
465 * EXPR_SUBLINK (SELECT with single targetlist item ...)
466 * ARRAY_SUBLINK ARRAY(SELECT with single targetlist item ...)
467 * CTE_SUBLINK WITH query (never actually part of an expression)
468 * For ALL, ANY, and ROWCOMPARE, the lefthand is a list of expressions of the
469 * same length as the subselect's targetlist. ROWCOMPARE will *always* have
470 * a list with more than one entry; if the subselect has just one target
471 * then the parser will create an EXPR_SUBLINK instead (and any operator
472 * above the subselect will be represented separately). Note that both
473 * ROWCOMPARE and EXPR require the subselect to deliver only one row.
474 * ALL, ANY, and ROWCOMPARE require the combining operators to deliver boolean
475 * results. ALL and ANY combine the per-row results using AND and OR
476 * semantics respectively.
477 * ARRAY requires just one target column, and creates an array of the target
478 * column's type using any number of rows resulting from the subselect.
480 * SubLink is classed as an Expr node, but it is not actually executable;
481 * it must be replaced in the expression tree by a SubPlan node during
484 * NOTE: in the raw output of gram.y, testexpr contains just the raw form
485 * of the lefthand expression (if any), and operName is the String name of
486 * the combining operator. Also, subselect is a raw parsetree. During parse
487 * analysis, the parser transforms testexpr into a complete boolean expression
488 * that compares the lefthand value(s) to PARAM_SUBLINK nodes representing the
489 * output columns of the subselect. And subselect is transformed to a Query.
490 * This is the representation seen in saved rules and in the rewriter.
492 * In EXISTS, EXPR, and ARRAY SubLinks, testexpr and operName are unused and
495 * The CTE_SUBLINK case never occurs in actual SubLink nodes, but it is used
496 * in SubPlans generated for WITH subqueries.
498 typedef enum SubLinkType
506 CTE_SUBLINK /* for SubPlans only */
510 typedef struct SubLink
513 SubLinkType subLinkType; /* see above */
514 Node *testexpr; /* outer-query test for ALL/ANY/ROWCOMPARE */
515 List *operName; /* originally specified operator name */
516 Node *subselect; /* subselect as Query* or parsetree */
517 int location; /* token location, or -1 if unknown */
521 * SubPlan - executable expression node for a subplan (sub-SELECT)
523 * The planner replaces SubLink nodes in expression trees with SubPlan
524 * nodes after it has finished planning the subquery. SubPlan references
525 * a sub-plantree stored in the subplans list of the toplevel PlannedStmt.
526 * (We avoid a direct link to make it easier to copy expression trees
527 * without causing multiple processing of the subplan.)
529 * In an ordinary subplan, testexpr points to an executable expression
530 * (OpExpr, an AND/OR tree of OpExprs, or RowCompareExpr) for the combining
531 * operator(s); the left-hand arguments are the original lefthand expressions,
532 * and the right-hand arguments are PARAM_EXEC Param nodes representing the
533 * outputs of the sub-select. (NOTE: runtime coercion functions may be
534 * inserted as well.) This is just the same expression tree as testexpr in
535 * the original SubLink node, but the PARAM_SUBLINK nodes are replaced by
536 * suitably numbered PARAM_EXEC nodes.
538 * If the sub-select becomes an initplan rather than a subplan, the executable
539 * expression is part of the outer plan's expression tree (and the SubPlan
540 * node itself is not, but rather is found in the outer plan's initPlan
541 * list). In this case testexpr is NULL to avoid duplication.
543 * The planner also derives lists of the values that need to be passed into
544 * and out of the subplan. Input values are represented as a list "args" of
545 * expressions to be evaluated in the outer-query context (currently these
546 * args are always just Vars, but in principle they could be any expression).
547 * The values are assigned to the global PARAM_EXEC params indexed by parParam
548 * (the parParam and args lists must have the same ordering). setParam is a
549 * list of the PARAM_EXEC params that are computed by the sub-select, if it
550 * is an initplan; they are listed in order by sub-select output column
551 * position. (parParam and setParam are integer Lists, not Bitmapsets,
552 * because their ordering is significant.)
554 * Also, the planner computes startup and per-call costs for use of the
555 * SubPlan. Note that these include the cost of the subquery proper,
556 * evaluation of the testexpr if any, and any hashtable management overhead.
558 typedef struct SubPlan
561 /* Fields copied from original SubLink: */
562 SubLinkType subLinkType; /* see above */
563 /* The combining operators, transformed to an executable expression: */
564 Node *testexpr; /* OpExpr or RowCompareExpr expression tree */
565 List *paramIds; /* IDs of Params embedded in the above */
566 /* Identification of the Plan tree to use: */
567 int plan_id; /* Index (from 1) in PlannedStmt.subplans */
568 /* Identification of the SubPlan for EXPLAIN and debugging purposes: */
569 char *plan_name; /* A name assigned during planning */
570 /* Extra data useful for determining subplan's output type: */
571 Oid firstColType; /* Type of first column of subplan result */
572 int32 firstColTypmod; /* Typmod of first column of subplan result */
573 Oid firstColCollation; /* Collation of first column of
575 /* Information about execution strategy: */
576 bool useHashTable; /* TRUE to store subselect output in a hash
577 * table (implies we are doing "IN") */
578 bool unknownEqFalse; /* TRUE if it's okay to return FALSE when the
579 * spec result is UNKNOWN; this allows much
580 * simpler handling of null values */
581 /* Information for passing params into and out of the subselect: */
582 /* setParam and parParam are lists of integers (param IDs) */
583 List *setParam; /* initplan subqueries have to set these
584 * Params for parent plan */
585 List *parParam; /* indices of input Params from parent plan */
586 List *args; /* exprs to pass as parParam values */
587 /* Estimated execution costs: */
588 Cost startup_cost; /* one-time setup cost */
589 Cost per_call_cost; /* cost for each subplan evaluation */
593 * AlternativeSubPlan - expression node for a choice among SubPlans
595 * The subplans are given as a List so that the node definition need not
596 * change if there's ever more than two alternatives. For the moment,
597 * though, there are always exactly two; and the first one is the fast-start
600 typedef struct AlternativeSubPlan
603 List *subplans; /* SubPlan(s) with equivalent results */
604 } AlternativeSubPlan;
609 * FieldSelect represents the operation of extracting one field from a tuple
610 * value. At runtime, the input expression is expected to yield a rowtype
611 * Datum. The specified field number is extracted and returned as a Datum.
615 typedef struct FieldSelect
618 Expr *arg; /* input expression */
619 AttrNumber fieldnum; /* attribute number of field to extract */
620 Oid resulttype; /* type of the field (result type of this
622 int32 resulttypmod; /* output typmod (usually -1) */
623 Oid resultcollid; /* OID of collation of the field */
629 * FieldStore represents the operation of modifying one field in a tuple
630 * value, yielding a new tuple value (the input is not touched!). Like
631 * the assign case of ArrayRef, this is used to implement UPDATE of a
632 * portion of a column.
634 * A single FieldStore can actually represent updates of several different
635 * fields. The parser only generates FieldStores with single-element lists,
636 * but the planner will collapse multiple updates of the same base column
637 * into one FieldStore.
641 typedef struct FieldStore
644 Expr *arg; /* input tuple value */
645 List *newvals; /* new value(s) for field(s) */
646 List *fieldnums; /* integer list of field attnums */
647 Oid resulttype; /* type of result (same as type of arg) */
648 /* Like RowExpr, we deliberately omit a typmod and collation here */
654 * RelabelType represents a "dummy" type coercion between two binary-
655 * compatible datatypes, such as reinterpreting the result of an OID
656 * expression as an int4. It is a no-op at runtime; we only need it
657 * to provide a place to store the correct type to be attributed to
658 * the expression result during type resolution. (We can't get away
659 * with just overwriting the type field of the input expression node,
660 * so we need a separate node to show the coercion's result type.)
664 typedef struct RelabelType
667 Expr *arg; /* input expression */
668 Oid resulttype; /* output type of coercion expression */
669 int32 resulttypmod; /* output typmod (usually -1) */
670 Oid resultcollid; /* OID of collation, or InvalidOid if none */
671 CoercionForm relabelformat; /* how to display this node */
672 int location; /* token location, or -1 if unknown */
678 * CoerceViaIO represents a type coercion between two types whose textual
679 * representations are compatible, implemented by invoking the source type's
680 * typoutput function then the destination type's typinput function.
684 typedef struct CoerceViaIO
687 Expr *arg; /* input expression */
688 Oid resulttype; /* output type of coercion */
689 /* output typmod is not stored, but is presumed -1 */
690 Oid resultcollid; /* OID of collation, or InvalidOid if none */
691 CoercionForm coerceformat; /* how to display this node */
692 int location; /* token location, or -1 if unknown */
698 * ArrayCoerceExpr represents a type coercion from one array type to another,
699 * which is implemented by applying the indicated element-type coercion
700 * function to each element of the source array. If elemfuncid is InvalidOid
701 * then the element types are binary-compatible, but the coercion still
702 * requires some effort (we have to fix the element type ID stored in the
707 typedef struct ArrayCoerceExpr
710 Expr *arg; /* input expression (yields an array) */
711 Oid elemfuncid; /* OID of element coercion function, or 0 */
712 Oid resulttype; /* output type of coercion (an array type) */
713 int32 resulttypmod; /* output typmod (also element typmod) */
714 Oid resultcollid; /* OID of collation, or InvalidOid if none */
715 bool isExplicit; /* conversion semantics flag to pass to func */
716 CoercionForm coerceformat; /* how to display this node */
717 int location; /* token location, or -1 if unknown */
723 * ConvertRowtypeExpr represents a type coercion from one composite type
724 * to another, where the source type is guaranteed to contain all the columns
725 * needed for the destination type plus possibly others; the columns need not
726 * be in the same positions, but are matched up by name. This is primarily
727 * used to convert a whole-row value of an inheritance child table into a
728 * valid whole-row value of its parent table's rowtype.
732 typedef struct ConvertRowtypeExpr
735 Expr *arg; /* input expression */
736 Oid resulttype; /* output type (always a composite type) */
737 /* Like RowExpr, we deliberately omit a typmod and collation here */
738 CoercionForm convertformat; /* how to display this node */
739 int location; /* token location, or -1 if unknown */
740 } ConvertRowtypeExpr;
743 * CollateExpr - COLLATE
745 * The planner replaces CollateExpr with RelabelType during expression
746 * preprocessing, so execution never sees a CollateExpr.
749 typedef struct CollateExpr
752 Expr *arg; /* input expression */
753 Oid collOid; /* collation's OID */
754 int location; /* token location, or -1 if unknown */
758 * CaseExpr - a CASE expression
760 * We support two distinct forms of CASE expression:
761 * CASE WHEN boolexpr THEN expr [ WHEN boolexpr THEN expr ... ]
762 * CASE testexpr WHEN compexpr THEN expr [ WHEN compexpr THEN expr ... ]
763 * These are distinguishable by the "arg" field being NULL in the first case
764 * and the testexpr in the second case.
766 * In the raw grammar output for the second form, the condition expressions
767 * of the WHEN clauses are just the comparison values. Parse analysis
768 * converts these to valid boolean expressions of the form
769 * CaseTestExpr '=' compexpr
770 * where the CaseTestExpr node is a placeholder that emits the correct
771 * value at runtime. This structure is used so that the testexpr need be
772 * evaluated only once. Note that after parse analysis, the condition
773 * expressions always yield boolean.
775 * Note: we can test whether a CaseExpr has been through parse analysis
776 * yet by checking whether casetype is InvalidOid or not.
779 typedef struct CaseExpr
782 Oid casetype; /* type of expression result */
783 Oid casecollid; /* OID of collation, or InvalidOid if none */
784 Expr *arg; /* implicit equality comparison argument */
785 List *args; /* the arguments (list of WHEN clauses) */
786 Expr *defresult; /* the default result (ELSE clause) */
787 int location; /* token location, or -1 if unknown */
791 * CaseWhen - one arm of a CASE expression
793 typedef struct CaseWhen
796 Expr *expr; /* condition expression */
797 Expr *result; /* substitution result */
798 int location; /* token location, or -1 if unknown */
802 * Placeholder node for the test value to be processed by a CASE expression.
803 * This is effectively like a Param, but can be implemented more simply
804 * since we need only one replacement value at a time.
806 * We also use this in nested UPDATE expressions.
807 * See transformAssignmentIndirection().
809 typedef struct CaseTestExpr
812 Oid typeId; /* type for substituted value */
813 int32 typeMod; /* typemod for substituted value */
814 Oid collation; /* collation for the substituted value */
818 * ArrayExpr - an ARRAY[] expression
820 * Note: if multidims is false, the constituent expressions all yield the
821 * scalar type identified by element_typeid. If multidims is true, the
822 * constituent expressions all yield arrays of element_typeid (ie, the same
823 * type as array_typeid); at runtime we must check for compatible subscripts.
825 typedef struct ArrayExpr
828 Oid array_typeid; /* type of expression result */
829 Oid array_collid; /* OID of collation, or InvalidOid if none */
830 Oid element_typeid; /* common type of array elements */
831 List *elements; /* the array elements or sub-arrays */
832 bool multidims; /* true if elements are sub-arrays */
833 int location; /* token location, or -1 if unknown */
837 * RowExpr - a ROW() expression
839 * Note: the list of fields must have a one-for-one correspondence with
840 * physical fields of the associated rowtype, although it is okay for it
841 * to be shorter than the rowtype. That is, the N'th list element must
842 * match up with the N'th physical field. When the N'th physical field
843 * is a dropped column (attisdropped) then the N'th list element can just
844 * be a NULL constant. (This case can only occur for named composite types,
845 * not RECORD types, since those are built from the RowExpr itself rather
846 * than vice versa.) It is important not to assume that length(args) is
847 * the same as the number of columns logically present in the rowtype.
849 * colnames is NIL in a RowExpr built from an ordinary ROW() expression.
850 * It is provided in cases where we expand a whole-row Var into a RowExpr,
851 * to retain the column alias names of the RTE that the Var referenced
852 * (which would otherwise be very difficult to extract from the parsetree).
853 * Like the args list, it is one-for-one with physical fields of the rowtype.
855 typedef struct RowExpr
858 List *args; /* the fields */
859 Oid row_typeid; /* RECORDOID or a composite type's ID */
862 * Note: we deliberately do NOT store a typmod. Although a typmod will be
863 * associated with specific RECORD types at runtime, it will differ for
864 * different backends, and so cannot safely be stored in stored
865 * parsetrees. We must assume typmod -1 for a RowExpr node.
867 * We don't need to store a collation either. The result type is
868 * necessarily composite, and composite types never have a collation.
870 CoercionForm row_format; /* how to display this node */
871 List *colnames; /* list of String, or NIL */
872 int location; /* token location, or -1 if unknown */
876 * RowCompareExpr - row-wise comparison, such as (a, b) <= (1, 2)
878 * We support row comparison for any operator that can be determined to
879 * act like =, <>, <, <=, >, or >= (we determine this by looking for the
880 * operator in btree opfamilies). Note that the same operator name might
881 * map to a different operator for each pair of row elements, since the
882 * element datatypes can vary.
884 * A RowCompareExpr node is only generated for the < <= > >= cases;
885 * the = and <> cases are translated to simple AND or OR combinations
886 * of the pairwise comparisons. However, we include = and <> in the
887 * RowCompareType enum for the convenience of parser logic.
889 typedef enum RowCompareType
891 /* Values of this enum are chosen to match btree strategy numbers */
892 ROWCOMPARE_LT = 1, /* BTLessStrategyNumber */
893 ROWCOMPARE_LE = 2, /* BTLessEqualStrategyNumber */
894 ROWCOMPARE_EQ = 3, /* BTEqualStrategyNumber */
895 ROWCOMPARE_GE = 4, /* BTGreaterEqualStrategyNumber */
896 ROWCOMPARE_GT = 5, /* BTGreaterStrategyNumber */
897 ROWCOMPARE_NE = 6 /* no such btree strategy */
900 typedef struct RowCompareExpr
903 RowCompareType rctype; /* LT LE GE or GT, never EQ or NE */
904 List *opnos; /* OID list of pairwise comparison ops */
905 List *opfamilies; /* OID list of containing operator families */
906 List *inputcollids; /* OID list of collations for comparisons */
907 List *largs; /* the left-hand input arguments */
908 List *rargs; /* the right-hand input arguments */
912 * CoalesceExpr - a COALESCE expression
914 typedef struct CoalesceExpr
917 Oid coalescetype; /* type of expression result */
918 Oid coalescecollid; /* OID of collation, or InvalidOid if none */
919 List *args; /* the arguments */
920 int location; /* token location, or -1 if unknown */
924 * MinMaxExpr - a GREATEST or LEAST function
926 typedef enum MinMaxOp
932 typedef struct MinMaxExpr
935 Oid minmaxtype; /* common type of arguments and result */
936 Oid minmaxcollid; /* OID of collation of result */
937 Oid inputcollid; /* OID of collation that function should use */
938 MinMaxOp op; /* function to execute */
939 List *args; /* the arguments */
940 int location; /* token location, or -1 if unknown */
944 * XmlExpr - various SQL/XML functions requiring special grammar productions
946 * 'name' carries the "NAME foo" argument (already XML-escaped).
947 * 'named_args' and 'arg_names' represent an xml_attribute list.
948 * 'args' carries all other arguments.
950 * Note: result type/typmod/collation are not stored, but can be deduced
951 * from the XmlExprOp. The type/typmod fields are just used for display
952 * purposes, and are NOT the true result type of the node.
954 typedef enum XmlExprOp
956 IS_XMLCONCAT, /* XMLCONCAT(args) */
957 IS_XMLELEMENT, /* XMLELEMENT(name, xml_attributes, args) */
958 IS_XMLFOREST, /* XMLFOREST(xml_attributes) */
959 IS_XMLPARSE, /* XMLPARSE(text, is_doc, preserve_ws) */
960 IS_XMLPI, /* XMLPI(name [, args]) */
961 IS_XMLROOT, /* XMLROOT(xml, version, standalone) */
962 IS_XMLSERIALIZE, /* XMLSERIALIZE(is_document, xmlval) */
963 IS_DOCUMENT /* xmlval IS DOCUMENT */
972 typedef struct XmlExpr
975 XmlExprOp op; /* xml function ID */
976 char *name; /* name in xml(NAME foo ...) syntaxes */
977 List *named_args; /* non-XML expressions for xml_attributes */
978 List *arg_names; /* parallel list of Value strings */
979 List *args; /* list of expressions */
980 XmlOptionType xmloption; /* DOCUMENT or CONTENT */
981 Oid type; /* target type/typmod for XMLSERIALIZE */
983 int location; /* token location, or -1 if unknown */
989 * NullTest represents the operation of testing a value for NULLness.
990 * The appropriate test is performed and returned as a boolean Datum.
992 * NOTE: the semantics of this for rowtype inputs are noticeably different
993 * from the scalar case. We provide an "argisrow" flag to reflect that.
997 typedef enum NullTestType
1002 typedef struct NullTest
1005 Expr *arg; /* input expression */
1006 NullTestType nulltesttype; /* IS NULL, IS NOT NULL */
1007 bool argisrow; /* T if input is of a composite type */
1013 * BooleanTest represents the operation of determining whether a boolean
1014 * is TRUE, FALSE, or UNKNOWN (ie, NULL). All six meaningful combinations
1015 * are supported. Note that a NULL input does *not* cause a NULL result.
1016 * The appropriate test is performed and returned as a boolean Datum.
1019 typedef enum BoolTestType
1021 IS_TRUE, IS_NOT_TRUE, IS_FALSE, IS_NOT_FALSE, IS_UNKNOWN, IS_NOT_UNKNOWN
1024 typedef struct BooleanTest
1027 Expr *arg; /* input expression */
1028 BoolTestType booltesttype; /* test type */
1034 * CoerceToDomain represents the operation of coercing a value to a domain
1035 * type. At runtime (and not before) the precise set of constraints to be
1036 * checked will be determined. If the value passes, it is returned as the
1037 * result; if not, an error is raised. Note that this is equivalent to
1038 * RelabelType in the scenario where no constraints are applied.
1040 typedef struct CoerceToDomain
1043 Expr *arg; /* input expression */
1044 Oid resulttype; /* domain type ID (result type) */
1045 int32 resulttypmod; /* output typmod (currently always -1) */
1046 Oid resultcollid; /* OID of collation, or InvalidOid if none */
1047 CoercionForm coercionformat; /* how to display this node */
1048 int location; /* token location, or -1 if unknown */
1052 * Placeholder node for the value to be processed by a domain's check
1053 * constraint. This is effectively like a Param, but can be implemented more
1054 * simply since we need only one replacement value at a time.
1056 * Note: the typeId/typeMod/collation will be set from the domain's base type,
1057 * not the domain itself. This is because we shouldn't consider the value
1058 * to be a member of the domain if we haven't yet checked its constraints.
1060 typedef struct CoerceToDomainValue
1063 Oid typeId; /* type for substituted value */
1064 int32 typeMod; /* typemod for substituted value */
1065 Oid collation; /* collation for the substituted value */
1066 int location; /* token location, or -1 if unknown */
1067 } CoerceToDomainValue;
1070 * Placeholder node for a DEFAULT marker in an INSERT or UPDATE command.
1072 * This is not an executable expression: it must be replaced by the actual
1073 * column default expression during rewriting. But it is convenient to
1074 * treat it as an expression node during parsing and rewriting.
1076 typedef struct SetToDefault
1079 Oid typeId; /* type for substituted value */
1080 int32 typeMod; /* typemod for substituted value */
1081 Oid collation; /* collation for the substituted value */
1082 int location; /* token location, or -1 if unknown */
1086 * Node representing [WHERE] CURRENT OF cursor_name
1088 * CURRENT OF is a bit like a Var, in that it carries the rangetable index
1089 * of the target relation being constrained; this aids placing the expression
1090 * correctly during planning. We can assume however that its "levelsup" is
1091 * always zero, due to the syntactic constraints on where it can appear.
1093 * The referenced cursor can be represented either as a hardwired string
1094 * or as a reference to a run-time parameter of type REFCURSOR. The latter
1095 * case is for the convenience of plpgsql.
1097 typedef struct CurrentOfExpr
1100 Index cvarno; /* RT index of target relation */
1101 char *cursor_name; /* name of referenced cursor, or NULL */
1102 int cursor_param; /* refcursor parameter number, or 0 */
1105 /*--------------------
1107 * a target entry (used in query target lists)
1109 * Strictly speaking, a TargetEntry isn't an expression node (since it can't
1110 * be evaluated by ExecEvalExpr). But we treat it as one anyway, since in
1111 * very many places it's convenient to process a whole query targetlist as a
1112 * single expression tree.
1114 * In a SELECT's targetlist, resno should always be equal to the item's
1115 * ordinal position (counting from 1). However, in an INSERT or UPDATE
1116 * targetlist, resno represents the attribute number of the destination
1117 * column for the item; so there may be missing or out-of-order resnos.
1118 * It is even legal to have duplicated resnos; consider
1119 * UPDATE table SET arraycol[1] = ..., arraycol[2] = ..., ...
1120 * The two meanings come together in the executor, because the planner
1121 * transforms INSERT/UPDATE tlists into a normalized form with exactly
1122 * one entry for each column of the destination table. Before that's
1123 * happened, however, it is risky to assume that resno == position.
1124 * Generally get_tle_by_resno() should be used rather than list_nth()
1125 * to fetch tlist entries by resno, and only in SELECT should you assume
1126 * that resno is a unique identifier.
1128 * resname is required to represent the correct column name in non-resjunk
1129 * entries of top-level SELECT targetlists, since it will be used as the
1130 * column title sent to the frontend. In most other contexts it is only
1131 * a debugging aid, and may be wrong or even NULL. (In particular, it may
1132 * be wrong in a tlist from a stored rule, if the referenced column has been
1133 * renamed by ALTER TABLE since the rule was made. Also, the planner tends
1134 * to store NULL rather than look up a valid name for tlist entries in
1135 * non-toplevel plan nodes.) In resjunk entries, resname should be either
1136 * a specific system-generated name (such as "ctid") or NULL; anything else
1137 * risks confusing ExecGetJunkAttribute!
1139 * ressortgroupref is used in the representation of ORDER BY, GROUP BY, and
1140 * DISTINCT items. Targetlist entries with ressortgroupref=0 are not
1141 * sort/group items. If ressortgroupref>0, then this item is an ORDER BY,
1142 * GROUP BY, and/or DISTINCT target value. No two entries in a targetlist
1143 * may have the same nonzero ressortgroupref --- but there is no particular
1144 * meaning to the nonzero values, except as tags. (For example, one must
1145 * not assume that lower ressortgroupref means a more significant sort key.)
1146 * The order of the associated SortGroupClause lists determine the semantics.
1148 * resorigtbl/resorigcol identify the source of the column, if it is a
1149 * simple reference to a column of a base table (or view). If it is not
1150 * a simple reference, these fields are zeroes.
1152 * If resjunk is true then the column is a working column (such as a sort key)
1153 * that should be removed from the final output of the query. Resjunk columns
1154 * must have resnos that cannot duplicate any regular column's resno. Also
1155 * note that there are places that assume resjunk columns come after non-junk
1157 *--------------------
1159 typedef struct TargetEntry
1162 Expr *expr; /* expression to evaluate */
1163 AttrNumber resno; /* attribute number (see notes above) */
1164 char *resname; /* name of the column (could be NULL) */
1165 Index ressortgroupref;/* nonzero if referenced by a sort/group
1167 Oid resorigtbl; /* OID of column's source table */
1168 AttrNumber resorigcol; /* column's number in source table */
1169 bool resjunk; /* set to true to eliminate the attribute from
1170 * final target list */
1174 /* ----------------------------------------------------------------
1175 * node types for join trees
1177 * The leaves of a join tree structure are RangeTblRef nodes. Above
1178 * these, JoinExpr nodes can appear to denote a specific kind of join
1179 * or qualified join. Also, FromExpr nodes can appear to denote an
1180 * ordinary cross-product join ("FROM foo, bar, baz WHERE ...").
1181 * FromExpr is like a JoinExpr of jointype JOIN_INNER, except that it
1182 * may have any number of child nodes, not just two.
1184 * NOTE: the top level of a Query's jointree is always a FromExpr.
1185 * Even if the jointree contains no rels, there will be a FromExpr.
1187 * NOTE: the qualification expressions present in JoinExpr nodes are
1188 * *in addition to* the query's main WHERE clause, which appears as the
1189 * qual of the top-level FromExpr. The reason for associating quals with
1190 * specific nodes in the jointree is that the position of a qual is critical
1191 * when outer joins are present. (If we enforce a qual too soon or too late,
1192 * that may cause the outer join to produce the wrong set of NULL-extended
1193 * rows.) If all joins are inner joins then all the qual positions are
1194 * semantically interchangeable.
1196 * NOTE: in the raw output of gram.y, a join tree contains RangeVar,
1197 * RangeSubselect, and RangeFunction nodes, which are all replaced by
1198 * RangeTblRef nodes during the parse analysis phase. Also, the top-level
1199 * FromExpr is added during parse analysis; the grammar regards FROM and
1200 * WHERE as separate.
1201 * ----------------------------------------------------------------
1205 * RangeTblRef - reference to an entry in the query's rangetable
1207 * We could use direct pointers to the RT entries and skip having these
1208 * nodes, but multiple pointers to the same node in a querytree cause
1209 * lots of headaches, so it seems better to store an index into the RT.
1211 typedef struct RangeTblRef
1218 * JoinExpr - for SQL JOIN expressions
1220 * isNatural, usingClause, and quals are interdependent. The user can write
1221 * only one of NATURAL, USING(), or ON() (this is enforced by the grammar).
1222 * If he writes NATURAL then parse analysis generates the equivalent USING()
1223 * list, and from that fills in "quals" with the right equality comparisons.
1224 * If he writes USING() then "quals" is filled with equality comparisons.
1225 * If he writes ON() then only "quals" is set. Note that NATURAL/USING
1226 * are not equivalent to ON() since they also affect the output column list.
1228 * alias is an Alias node representing the AS alias-clause attached to the
1229 * join expression, or NULL if no clause. NB: presence or absence of the
1230 * alias has a critical impact on semantics, because a join with an alias
1231 * restricts visibility of the tables/columns inside it.
1233 * During parse analysis, an RTE is created for the Join, and its index
1234 * is filled into rtindex. This RTE is present mainly so that Vars can
1235 * be created that refer to the outputs of the join. The planner sometimes
1236 * generates JoinExprs internally; these can have rtindex = 0 if there are
1237 * no join alias variables referencing such joins.
1240 typedef struct JoinExpr
1243 JoinType jointype; /* type of join */
1244 bool isNatural; /* Natural join? Will need to shape table */
1245 Node *larg; /* left subtree */
1246 Node *rarg; /* right subtree */
1247 List *usingClause; /* USING clause, if any (list of String) */
1248 Node *quals; /* qualifiers on join, if any */
1249 Alias *alias; /* user-written alias clause, if any */
1250 int rtindex; /* RT index assigned for join, or 0 */
1254 * FromExpr - represents a FROM ... WHERE ... construct
1256 * This is both more flexible than a JoinExpr (it can have any number of
1257 * children, including zero) and less so --- we don't need to deal with
1258 * aliases and so on. The output column set is implicitly just the union
1259 * of the outputs of the children.
1262 typedef struct FromExpr
1265 List *fromlist; /* List of join subtrees */
1266 Node *quals; /* qualifiers on join, if any */
1269 #endif /* PRIMNODES_H */