]> granicus.if.org Git - postgresql/blob - src/include/nodes/primnodes.h
Update copyright for 2006. Update scripts.
[postgresql] / src / include / nodes / primnodes.h
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-2006, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  * $PostgreSQL: pgsql/src/include/nodes/primnodes.h,v 1.112 2006/03/05 15:58:57 momjian Exp $
14  *
15  *-------------------------------------------------------------------------
16  */
17 #ifndef PRIMNODES_H
18 #define PRIMNODES_H
19
20 #include "access/attnum.h"
21 #include "nodes/pg_list.h"
22 #include "nodes/value.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  */
39 typedef 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 typedef enum InhOption
47 {
48         INH_NO,                                         /* Do NOT scan child tables */
49         INH_YES,                                        /* DO scan child tables */
50         INH_DEFAULT                                     /* Use current SQL_inheritance option */
51 } InhOption;
52
53 /*
54  * RangeVar - range variable, used in FROM clauses
55  *
56  * Also used to represent table names in utility statements; there, the alias
57  * field is not used, and inhOpt shows whether to apply the operation
58  * recursively to child tables.  In some contexts it is also useful to carry
59  * a TEMP table indication here.
60  */
61 typedef struct RangeVar
62 {
63         NodeTag         type;
64         char       *catalogname;        /* the catalog (database) name, or NULL */
65         char       *schemaname;         /* the schema name, or NULL */
66         char       *relname;            /* the relation/sequence name */
67         InhOption       inhOpt;                 /* expand rel by inheritance? recursively act
68                                                                  * on children? */
69         bool            istemp;                 /* is this a temp relation/sequence? */
70         Alias      *alias;                      /* table alias & optional column aliases */
71 } RangeVar;
72
73
74 /* ----------------------------------------------------------------
75  *                                      node types for executable expressions
76  * ----------------------------------------------------------------
77  */
78
79 /*
80  * Expr - generic superclass for executable-expression nodes
81  *
82  * All node types that are used in executable expression trees should derive
83  * from Expr (that is, have Expr as their first field).  Since Expr only
84  * contains NodeTag, this is a formality, but it is an easy form of
85  * documentation.  See also the ExprState node types in execnodes.h.
86  */
87 typedef struct Expr
88 {
89         NodeTag         type;
90 } Expr;
91
92 /*
93  * Var - expression node representing a variable (ie, a table column)
94  *
95  * Note: during parsing/planning, varnoold/varoattno are always just copies
96  * of varno/varattno.  At the tail end of planning, Var nodes appearing in
97  * upper-level plan nodes are reassigned to point to the outputs of their
98  * subplans; for example, in a join node varno becomes INNER or OUTER and
99  * varattno becomes the index of the proper element of that subplan's target
100  * list.  But varnoold/varoattno continue to hold the original values.
101  * The code doesn't really need varnoold/varoattno, but they are very useful
102  * for debugging and interpreting completed plans, so we keep them around.
103  */
104 #define    INNER                65000
105 #define    OUTER                65001
106
107 #define    PRS2_OLD_VARNO                       1
108 #define    PRS2_NEW_VARNO                       2
109
110 typedef struct Var
111 {
112         Expr            xpr;
113         Index           varno;                  /* index of this var's relation in the range
114                                                                  * table (could also be INNER or OUTER) */
115         AttrNumber      varattno;               /* attribute number of this var, or zero for
116                                                                  * all */
117         Oid                     vartype;                /* pg_type tuple OID for the type of this var */
118         int32           vartypmod;              /* pg_attribute typmod value */
119         Index           varlevelsup;
120
121         /*
122          * for subquery variables referencing outer relations; 0 in a normal var,
123          * >0 means N levels up
124          */
125         Index           varnoold;               /* original value of varno, for debugging */
126         AttrNumber      varoattno;              /* original value of varattno */
127 } Var;
128
129 /*
130  * Const
131  */
132 typedef struct Const
133 {
134         Expr            xpr;
135         Oid                     consttype;              /* PG_TYPE OID of the constant's datatype */
136         int                     constlen;               /* typlen of the constant's datatype */
137         Datum           constvalue;             /* the constant's value */
138         bool            constisnull;    /* whether the constant is null (if true,
139                                                                  * constvalue is undefined) */
140         bool            constbyval;             /* whether this datatype is passed by value.
141                                                                  * If true, then all the information is stored
142                                                                  * in the Datum. If false, then the Datum
143                                                                  * contains a pointer to the information. */
144 } Const;
145
146 /* ----------------
147  * Param
148  *              paramkind - specifies the kind of parameter. The possible values
149  *              for this field are specified in "params.h", and they are:
150  *
151  *              PARAM_NAMED: The parameter has a name, i.e. something
152  *                              like `$.salary' or `$.foobar'.
153  *                              In this case field `paramname' must be a valid name.
154  *
155  *              PARAM_NUM:       The parameter has only a numeric identifier,
156  *                              i.e. something like `$1', `$2' etc.
157  *                              The number is contained in the `paramid' field.
158  *
159  *              PARAM_EXEC:  The parameter is an internal executor parameter.
160  *                              It has a number contained in the `paramid' field.
161  *
162  *              PARAM_SUBLINK:  The parameter represents an output column of a SubLink
163  *                              node's sub-select.  The column number is contained in the
164  *                              `paramid' field.  (This type of Param is converted to
165  *                              PARAM_EXEC during planning.)
166  * ----------------
167  */
168 typedef struct Param
169 {
170         Expr            xpr;
171         int                     paramkind;              /* kind of parameter. See above */
172         AttrNumber      paramid;                /* numeric ID for parameter ("$1") */
173         char       *paramname;          /* name for parameter ("$.foo") */
174         Oid                     paramtype;              /* PG_TYPE OID of parameter's datatype */
175 } Param;
176
177 /*
178  * Aggref
179  */
180 typedef struct Aggref
181 {
182         Expr            xpr;
183         Oid                     aggfnoid;               /* pg_proc Oid of the aggregate */
184         Oid                     aggtype;                /* type Oid of result of the aggregate */
185         Expr       *target;                     /* expression we are aggregating on */
186         Index           agglevelsup;    /* > 0 if agg belongs to outer query */
187         bool            aggstar;                /* TRUE if argument was really '*' */
188         bool            aggdistinct;    /* TRUE if it's agg(DISTINCT ...) */
189 } Aggref;
190
191 /* ----------------
192  *      ArrayRef: describes an array subscripting operation
193  *
194  * An ArrayRef can describe fetching a single element from an array,
195  * fetching a subarray (array slice), storing a single element into
196  * an array, or storing a slice.  The "store" cases work with an
197  * initial array value and a source value that is inserted into the
198  * appropriate part of the array; the result of the operation is an
199  * entire new modified array value.
200  *
201  * If reflowerindexpr = NIL, then we are fetching or storing a single array
202  * element at the subscripts given by refupperindexpr.  Otherwise we are
203  * fetching or storing an array slice, that is a rectangular subarray
204  * with lower and upper bounds given by the index expressions.
205  * reflowerindexpr must be the same length as refupperindexpr when it
206  * is not NIL.
207  *
208  * Note: refrestype is NOT the element type, but the array type,
209  * when doing subarray fetch or either type of store.
210  * ----------------
211  */
212 typedef struct ArrayRef
213 {
214         Expr            xpr;
215         Oid                     refrestype;             /* type of the result of the ArrayRef
216                                                                  * operation */
217         Oid                     refarraytype;   /* type of the array proper */
218         Oid                     refelemtype;    /* type of the array elements */
219         List       *refupperindexpr;/* expressions that evaluate to upper array
220                                                                  * indexes */
221         List       *reflowerindexpr;/* expressions that evaluate to lower array
222                                                                  * indexes */
223         Expr       *refexpr;            /* the expression that evaluates to an array
224                                                                  * value */
225         Expr       *refassgnexpr;       /* expression for the source value, or NULL if
226                                                                  * fetch */
227 } ArrayRef;
228
229 /*
230  * CoercionContext - distinguishes the allowed set of type casts
231  *
232  * NB: ordering of the alternatives is significant; later (larger) values
233  * allow more casts than earlier ones.
234  */
235 typedef enum CoercionContext
236 {
237         COERCION_IMPLICIT,                      /* coercion in context of expression */
238         COERCION_ASSIGNMENT,            /* coercion in context of assignment */
239         COERCION_EXPLICIT                       /* explicit cast operation */
240 } CoercionContext;
241
242 /*
243  * CoercionForm - information showing how to display a function-call node
244  */
245 typedef enum CoercionForm
246 {
247         COERCE_EXPLICIT_CALL,           /* display as a function call */
248         COERCE_EXPLICIT_CAST,           /* display as an explicit cast */
249         COERCE_IMPLICIT_CAST,           /* implicit cast, so hide it */
250         COERCE_DONTCARE                         /* special case for planner */
251 } CoercionForm;
252
253 /*
254  * FuncExpr - expression node for a function call
255  */
256 typedef struct FuncExpr
257 {
258         Expr            xpr;
259         Oid                     funcid;                 /* PG_PROC OID of the function */
260         Oid                     funcresulttype; /* PG_TYPE OID of result value */
261         bool            funcretset;             /* true if function returns set */
262         CoercionForm funcformat;        /* how to display this function call */
263         List       *args;                       /* arguments to the function */
264 } FuncExpr;
265
266 /*
267  * OpExpr - expression node for an operator invocation
268  *
269  * Semantically, this is essentially the same as a function call.
270  *
271  * Note that opfuncid is not necessarily filled in immediately on creation
272  * of the node.  The planner makes sure it is valid before passing the node
273  * tree to the executor, but during parsing/planning opfuncid is typically 0.
274  */
275 typedef struct OpExpr
276 {
277         Expr            xpr;
278         Oid                     opno;                   /* PG_OPERATOR OID of the operator */
279         Oid                     opfuncid;               /* PG_PROC OID of underlying function */
280         Oid                     opresulttype;   /* PG_TYPE OID of result value */
281         bool            opretset;               /* true if operator returns set */
282         List       *args;                       /* arguments to the operator (1 or 2) */
283 } OpExpr;
284
285 /*
286  * DistinctExpr - expression node for "x IS DISTINCT FROM y"
287  *
288  * Except for the nodetag, this is represented identically to an OpExpr
289  * referencing the "=" operator for x and y.
290  * We use "=", not the more obvious "<>", because more datatypes have "="
291  * than "<>".  This means the executor must invert the operator result.
292  * Note that the operator function won't be called at all if either input
293  * is NULL, since then the result can be determined directly.
294  */
295 typedef OpExpr DistinctExpr;
296
297 /*
298  * ScalarArrayOpExpr - expression node for "scalar op ANY/ALL (array)"
299  *
300  * The operator must yield boolean.  It is applied to the left operand
301  * and each element of the righthand array, and the results are combined
302  * with OR or AND (for ANY or ALL respectively).  The node representation
303  * is almost the same as for the underlying operator, but we need a useOr
304  * flag to remember whether it's ANY or ALL, and we don't have to store
305  * the result type because it must be boolean.
306  */
307 typedef struct ScalarArrayOpExpr
308 {
309         Expr            xpr;
310         Oid                     opno;                   /* PG_OPERATOR OID of the operator */
311         Oid                     opfuncid;               /* PG_PROC OID of underlying function */
312         bool            useOr;                  /* true for ANY, false for ALL */
313         List       *args;                       /* the scalar and array operands */
314 } ScalarArrayOpExpr;
315
316 /*
317  * BoolExpr - expression node for the basic Boolean operators AND, OR, NOT
318  *
319  * Notice the arguments are given as a List.  For NOT, of course the list
320  * must always have exactly one element.  For AND and OR, the executor can
321  * handle any number of arguments.      The parser treats AND and OR as binary
322  * and so it only produces two-element lists, but the optimizer will flatten
323  * trees of AND and OR nodes to produce longer lists when possible.
324  */
325 typedef enum BoolExprType
326 {
327         AND_EXPR, OR_EXPR, NOT_EXPR
328 } BoolExprType;
329
330 typedef struct BoolExpr
331 {
332         Expr            xpr;
333         BoolExprType boolop;
334         List       *args;                       /* arguments to this expression */
335 } BoolExpr;
336
337 /*
338  * SubLink
339  *
340  * A SubLink represents a subselect appearing in an expression, and in some
341  * cases also the combining operator(s) just above it.  The subLinkType
342  * indicates the form of the expression represented:
343  *      EXISTS_SUBLINK          EXISTS(SELECT ...)
344  *      ALL_SUBLINK                     (lefthand) op ALL (SELECT ...)
345  *      ANY_SUBLINK                     (lefthand) op ANY (SELECT ...)
346  *      ROWCOMPARE_SUBLINK      (lefthand) op (SELECT ...)
347  *      EXPR_SUBLINK            (SELECT with single targetlist item ...)
348  *      ARRAY_SUBLINK           ARRAY(SELECT with single targetlist item ...)
349  * For ALL, ANY, and ROWCOMPARE, the lefthand is a list of expressions of the
350  * same length as the subselect's targetlist.  ROWCOMPARE will *always* have
351  * a list with more than one entry; if the subselect has just one target
352  * then the parser will create an EXPR_SUBLINK instead (and any operator
353  * above the subselect will be represented separately).  Note that both
354  * ROWCOMPARE and EXPR require the subselect to deliver only one row.
355  * ALL, ANY, and ROWCOMPARE require the combining operators to deliver boolean
356  * results.  ALL and ANY combine the per-row results using AND and OR
357  * semantics respectively.
358  * ARRAY requires just one target column, and creates an array of the target
359  * column's type using one or more rows resulting from the subselect.
360  *
361  * SubLink is classed as an Expr node, but it is not actually executable;
362  * it must be replaced in the expression tree by a SubPlan node during
363  * planning.
364  *
365  * NOTE: in the raw output of gram.y, testexpr contains just the raw form
366  * of the lefthand expression (if any), and operName is the String name of
367  * the combining operator.  Also, subselect is a raw parsetree.  During parse
368  * analysis, the parser transforms testexpr into a complete boolean expression
369  * that compares the lefthand value(s) to PARAM_SUBLINK nodes representing the
370  * output columns of the subselect.  And subselect is transformed to a Query.
371  * This is the representation seen in saved rules and in the rewriter.
372  *
373  * In EXISTS, EXPR, and ARRAY SubLinks, testexpr and operName are unused and
374  * are always null.
375  */
376 typedef enum SubLinkType
377 {
378         EXISTS_SUBLINK,
379         ALL_SUBLINK,
380         ANY_SUBLINK,
381         ROWCOMPARE_SUBLINK,
382         EXPR_SUBLINK,
383         ARRAY_SUBLINK
384 } SubLinkType;
385
386
387 typedef struct SubLink
388 {
389         Expr            xpr;
390         SubLinkType subLinkType;        /* see above */
391         Node       *testexpr;           /* outer-query test for ALL/ANY/ROWCOMPARE */
392         List       *operName;           /* originally specified operator name */
393         Node       *subselect;          /* subselect as Query* or parsetree */
394 } SubLink;
395
396 /*
397  * SubPlan - executable expression node for a subplan (sub-SELECT)
398  *
399  * The planner replaces SubLink nodes in expression trees with SubPlan
400  * nodes after it has finished planning the subquery.  SubPlan contains
401  * a sub-plantree and rtable instead of a sub-Query.
402  *
403  * In an ordinary subplan, testexpr points to an executable expression
404  * (OpExpr, an AND/OR tree of OpExprs, or RowCompareExpr) for the combining
405  * operator(s); the left-hand arguments are the original lefthand expressions,
406  * and the right-hand arguments are PARAM_EXEC Param nodes representing the
407  * outputs of the sub-select.  (NOTE: runtime coercion functions may be
408  * inserted as well.)  This is just the same expression tree as testexpr in
409  * the original SubLink node, but the PARAM_SUBLINK nodes are replaced by
410  * suitably numbered PARAM_EXEC nodes.
411  *
412  * If the sub-select becomes an initplan rather than a subplan, the executable
413  * expression is part of the outer plan's expression tree (and the SubPlan
414  * node itself is not).  In this case testexpr is NULL to avoid duplication.
415  *
416  * The planner also derives lists of the values that need to be passed into
417  * and out of the subplan.      Input values are represented as a list "args" of
418  * expressions to be evaluated in the outer-query context (currently these
419  * args are always just Vars, but in principle they could be any expression).
420  * The values are assigned to the global PARAM_EXEC params indexed by parParam
421  * (the parParam and args lists must have the same ordering).  setParam is a
422  * list of the PARAM_EXEC params that are computed by the sub-select, if it
423  * is an initplan; they are listed in order by sub-select output column
424  * position.  (parParam and setParam are integer Lists, not Bitmapsets,
425  * because their ordering is significant.)
426  */
427 typedef struct SubPlan
428 {
429         Expr            xpr;
430         /* Fields copied from original SubLink: */
431         SubLinkType subLinkType;        /* see above */
432         /* The combining operators, transformed to an executable expression: */
433         Node       *testexpr;           /* OpExpr or RowCompareExpr expression tree */
434         List       *paramIds;           /* IDs of Params embedded in the above */
435         /* The subselect, transformed to a Plan: */
436         struct Plan *plan;                      /* subselect plan itself */
437         int                     plan_id;                /* dummy thing because of we haven't equal
438                                                                  * funcs for plan nodes... actually, we could
439                                                                  * put *plan itself somewhere else (TopPlan
440                                                                  * node ?)... */
441         List       *rtable;                     /* range table for subselect */
442         /* Information about execution strategy: */
443         bool            useHashTable;   /* TRUE to store subselect output in a hash
444                                                                  * table (implies we are doing "IN") */
445         bool            unknownEqFalse; /* TRUE if it's okay to return FALSE when the
446                                                                  * spec result is UNKNOWN; this allows much
447                                                                  * simpler handling of null values */
448         /* Information for passing params into and out of the subselect: */
449         /* setParam and parParam are lists of integers (param IDs) */
450         List       *setParam;           /* initplan subqueries have to set these
451                                                                  * Params for parent plan */
452         List       *parParam;           /* indices of input Params from parent plan */
453         List       *args;                       /* exprs to pass as parParam values */
454 } SubPlan;
455
456 /* ----------------
457  * FieldSelect
458  *
459  * FieldSelect represents the operation of extracting one field from a tuple
460  * value.  At runtime, the input expression is expected to yield a rowtype
461  * Datum.  The specified field number is extracted and returned as a Datum.
462  * ----------------
463  */
464
465 typedef struct FieldSelect
466 {
467         Expr            xpr;
468         Expr       *arg;                        /* input expression */
469         AttrNumber      fieldnum;               /* attribute number of field to extract */
470         Oid                     resulttype;             /* type of the field (result type of this
471                                                                  * node) */
472         int32           resulttypmod;   /* output typmod (usually -1) */
473 } FieldSelect;
474
475 /* ----------------
476  * FieldStore
477  *
478  * FieldStore represents the operation of modifying one field in a tuple
479  * value, yielding a new tuple value (the input is not touched!).  Like
480  * the assign case of ArrayRef, this is used to implement UPDATE of a
481  * portion of a column.
482  *
483  * A single FieldStore can actually represent updates of several different
484  * fields.      The parser only generates FieldStores with single-element lists,
485  * but the planner will collapse multiple updates of the same base column
486  * into one FieldStore.
487  * ----------------
488  */
489
490 typedef struct FieldStore
491 {
492         Expr            xpr;
493         Expr       *arg;                        /* input tuple value */
494         List       *newvals;            /* new value(s) for field(s) */
495         List       *fieldnums;          /* integer list of field attnums */
496         Oid                     resulttype;             /* type of result (same as type of arg) */
497         /* Like RowExpr, we deliberately omit a typmod here */
498 } FieldStore;
499
500 /* ----------------
501  * RelabelType
502  *
503  * RelabelType represents a "dummy" type coercion between two binary-
504  * compatible datatypes, such as reinterpreting the result of an OID
505  * expression as an int4.  It is a no-op at runtime; we only need it
506  * to provide a place to store the correct type to be attributed to
507  * the expression result during type resolution.  (We can't get away
508  * with just overwriting the type field of the input expression node,
509  * so we need a separate node to show the coercion's result type.)
510  * ----------------
511  */
512
513 typedef struct RelabelType
514 {
515         Expr            xpr;
516         Expr       *arg;                        /* input expression */
517         Oid                     resulttype;             /* output type of coercion expression */
518         int32           resulttypmod;   /* output typmod (usually -1) */
519         CoercionForm relabelformat; /* how to display this node */
520 } RelabelType;
521
522 /* ----------------
523  * ConvertRowtypeExpr
524  *
525  * ConvertRowtypeExpr represents a type coercion from one composite type
526  * to another, where the source type is guaranteed to contain all the columns
527  * needed for the destination type plus possibly others; the columns need not
528  * be in the same positions, but are matched up by name.  This is primarily
529  * used to convert a whole-row value of an inheritance child table into a
530  * valid whole-row value of its parent table's rowtype.
531  * ----------------
532  */
533
534 typedef struct ConvertRowtypeExpr
535 {
536         Expr            xpr;
537         Expr       *arg;                        /* input expression */
538         Oid                     resulttype;             /* output type (always a composite type) */
539         /* result typmod is not stored, but must be -1; see RowExpr comments */
540         CoercionForm convertformat; /* how to display this node */
541 } ConvertRowtypeExpr;
542
543 /*----------
544  * CaseExpr - a CASE expression
545  *
546  * We support two distinct forms of CASE expression:
547  *              CASE WHEN boolexpr THEN expr [ WHEN boolexpr THEN expr ... ]
548  *              CASE testexpr WHEN compexpr THEN expr [ WHEN compexpr THEN expr ... ]
549  * These are distinguishable by the "arg" field being NULL in the first case
550  * and the testexpr in the second case.
551  *
552  * In the raw grammar output for the second form, the condition expressions
553  * of the WHEN clauses are just the comparison values.  Parse analysis
554  * converts these to valid boolean expressions of the form
555  *              CaseTestExpr '=' compexpr
556  * where the CaseTestExpr node is a placeholder that emits the correct
557  * value at runtime.  This structure is used so that the testexpr need be
558  * evaluated only once.  Note that after parse analysis, the condition
559  * expressions always yield boolean.
560  *
561  * Note: we can test whether a CaseExpr has been through parse analysis
562  * yet by checking whether casetype is InvalidOid or not.
563  *----------
564  */
565 typedef struct CaseExpr
566 {
567         Expr            xpr;
568         Oid                     casetype;               /* type of expression result */
569         Expr       *arg;                        /* implicit equality comparison argument */
570         List       *args;                       /* the arguments (list of WHEN clauses) */
571         Expr       *defresult;          /* the default result (ELSE clause) */
572 } CaseExpr;
573
574 /*
575  * CaseWhen - one arm of a CASE expression
576  */
577 typedef struct CaseWhen
578 {
579         Expr            xpr;
580         Expr       *expr;                       /* condition expression */
581         Expr       *result;                     /* substitution result */
582 } CaseWhen;
583
584 /*
585  * Placeholder node for the test value to be processed by a CASE expression.
586  * This is effectively like a Param, but can be implemented more simply
587  * since we need only one replacement value at a time.
588  *
589  * We also use this in nested UPDATE expressions.
590  * See transformAssignmentIndirection().
591  */
592 typedef struct CaseTestExpr
593 {
594         Expr            xpr;
595         Oid                     typeId;                 /* type for substituted value */
596         int32           typeMod;                /* typemod for substituted value */
597 } CaseTestExpr;
598
599 /*
600  * ArrayExpr - an ARRAY[] expression
601  *
602  * Note: if multidims is false, the constituent expressions all yield the
603  * scalar type identified by element_typeid.  If multidims is true, the
604  * constituent expressions all yield arrays of element_typeid (ie, the same
605  * type as array_typeid); at runtime we must check for compatible subscripts.
606  */
607 typedef struct ArrayExpr
608 {
609         Expr            xpr;
610         Oid                     array_typeid;   /* type of expression result */
611         Oid                     element_typeid; /* common type of array elements */
612         List       *elements;           /* the array elements or sub-arrays */
613         bool            multidims;              /* true if elements are sub-arrays */
614 } ArrayExpr;
615
616 /*
617  * RowExpr - a ROW() expression
618  *
619  * Note: the list of fields must have a one-for-one correspondence with
620  * physical fields of the associated rowtype, although it is okay for it
621  * to be shorter than the rowtype.      That is, the N'th list element must
622  * match up with the N'th physical field.  When the N'th physical field
623  * is a dropped column (attisdropped) then the N'th list element can just
624  * be a NULL constant.  (This case can only occur for named composite types,
625  * not RECORD types, since those are built from the RowExpr itself rather
626  * than vice versa.)  It is important not to assume that length(args) is
627  * the same as the number of columns logically present in the rowtype.
628  */
629 typedef struct RowExpr
630 {
631         Expr            xpr;
632         List       *args;                       /* the fields */
633         Oid                     row_typeid;             /* RECORDOID or a composite type's ID */
634
635         /*
636          * Note: we deliberately do NOT store a typmod.  Although a typmod will be
637          * associated with specific RECORD types at runtime, it will differ for
638          * different backends, and so cannot safely be stored in stored
639          * parsetrees.  We must assume typmod -1 for a RowExpr node.
640          */
641         CoercionForm row_format;        /* how to display this node */
642 } RowExpr;
643
644 /*
645  * RowCompareExpr - row-wise comparison, such as (a, b) <= (1, 2)
646  *
647  * We support row comparison for any operator that can be determined to
648  * act like =, <>, <, <=, >, or >= (we determine this by looking for the
649  * operator in btree opclasses).  Note that the same operator name might
650  * map to a different operator for each pair of row elements, since the
651  * element datatypes can vary.
652  *
653  * A RowCompareExpr node is only generated for the < <= > >= cases;
654  * the = and <> cases are translated to simple AND or OR combinations
655  * of the pairwise comparisons.  However, we include = and <> in the
656  * RowCompareType enum for the convenience of parser logic.
657  */
658 typedef enum RowCompareType
659 {
660         /* Values of this enum are chosen to match btree strategy numbers */
661         ROWCOMPARE_LT = 1,                      /* BTLessStrategyNumber */
662         ROWCOMPARE_LE = 2,                      /* BTLessEqualStrategyNumber */
663         ROWCOMPARE_EQ = 3,                      /* BTEqualStrategyNumber */
664         ROWCOMPARE_GE = 4,                      /* BTGreaterEqualStrategyNumber */
665         ROWCOMPARE_GT = 5,                      /* BTGreaterStrategyNumber */
666         ROWCOMPARE_NE = 6                       /* no such btree strategy */
667 } RowCompareType;
668
669 typedef struct RowCompareExpr
670 {
671         Expr            xpr;
672         RowCompareType rctype;          /* LT LE GE or GT, never EQ or NE */
673         List       *opnos;                      /* OID list of pairwise comparison ops */
674         List       *opclasses;          /* OID list of containing operator classes */
675         List       *largs;                      /* the left-hand input arguments */
676         List       *rargs;                      /* the right-hand input arguments */
677 } RowCompareExpr;
678
679 /*
680  * CoalesceExpr - a COALESCE expression
681  */
682 typedef struct CoalesceExpr
683 {
684         Expr            xpr;
685         Oid                     coalescetype;   /* type of expression result */
686         List       *args;                       /* the arguments */
687 } CoalesceExpr;
688
689 /*
690  * MinMaxExpr - a GREATEST or LEAST function
691  */
692 typedef enum MinMaxOp
693 {
694         IS_GREATEST,
695         IS_LEAST
696 } MinMaxOp;
697
698 typedef struct MinMaxExpr
699 {
700         Expr            xpr;
701         Oid                     minmaxtype;             /* common type of arguments and result */
702         MinMaxOp        op;                             /* function to execute */
703         List       *args;                       /* the arguments */
704 } MinMaxExpr;
705
706 /*
707  * NullIfExpr - a NULLIF expression
708  *
709  * Like DistinctExpr, this is represented the same as an OpExpr referencing
710  * the "=" operator for x and y.
711  */
712 typedef OpExpr NullIfExpr;
713
714 /* ----------------
715  * NullTest
716  *
717  * NullTest represents the operation of testing a value for NULLness.
718  * Currently, we only support scalar input values, but eventually a
719  * row-constructor input should be supported.
720  * The appropriate test is performed and returned as a boolean Datum.
721  * ----------------
722  */
723
724 typedef enum NullTestType
725 {
726         IS_NULL, IS_NOT_NULL
727 } NullTestType;
728
729 typedef struct NullTest
730 {
731         Expr            xpr;
732         Expr       *arg;                        /* input expression */
733         NullTestType nulltesttype;      /* IS NULL, IS NOT NULL */
734 } NullTest;
735
736 /*
737  * BooleanTest
738  *
739  * BooleanTest represents the operation of determining whether a boolean
740  * is TRUE, FALSE, or UNKNOWN (ie, NULL).  All six meaningful combinations
741  * are supported.  Note that a NULL input does *not* cause a NULL result.
742  * The appropriate test is performed and returned as a boolean Datum.
743  */
744
745 typedef enum BoolTestType
746 {
747         IS_TRUE, IS_NOT_TRUE, IS_FALSE, IS_NOT_FALSE, IS_UNKNOWN, IS_NOT_UNKNOWN
748 } BoolTestType;
749
750 typedef struct BooleanTest
751 {
752         Expr            xpr;
753         Expr       *arg;                        /* input expression */
754         BoolTestType booltesttype;      /* test type */
755 } BooleanTest;
756
757 /*
758  * CoerceToDomain
759  *
760  * CoerceToDomain represents the operation of coercing a value to a domain
761  * type.  At runtime (and not before) the precise set of constraints to be
762  * checked will be determined.  If the value passes, it is returned as the
763  * result; if not, an error is raised.  Note that this is equivalent to
764  * RelabelType in the scenario where no constraints are applied.
765  */
766 typedef struct CoerceToDomain
767 {
768         Expr            xpr;
769         Expr       *arg;                        /* input expression */
770         Oid                     resulttype;             /* domain type ID (result type) */
771         int32           resulttypmod;   /* output typmod (currently always -1) */
772         CoercionForm coercionformat;    /* how to display this node */
773 } CoerceToDomain;
774
775 /*
776  * Placeholder node for the value to be processed by a domain's check
777  * constraint.  This is effectively like a Param, but can be implemented more
778  * simply since we need only one replacement value at a time.
779  *
780  * Note: the typeId/typeMod will be set from the domain's base type, not
781  * the domain itself.  This is because we shouldn't consider the value to
782  * be a member of the domain if we haven't yet checked its constraints.
783  */
784 typedef struct CoerceToDomainValue
785 {
786         Expr            xpr;
787         Oid                     typeId;                 /* type for substituted value */
788         int32           typeMod;                /* typemod for substituted value */
789 } CoerceToDomainValue;
790
791 /*
792  * Placeholder node for a DEFAULT marker in an INSERT or UPDATE command.
793  *
794  * This is not an executable expression: it must be replaced by the actual
795  * column default expression during rewriting.  But it is convenient to
796  * treat it as an expression node during parsing and rewriting.
797  */
798 typedef struct SetToDefault
799 {
800         Expr            xpr;
801         Oid                     typeId;                 /* type for substituted value */
802         int32           typeMod;                /* typemod for substituted value */
803 } SetToDefault;
804
805 /*--------------------
806  * TargetEntry -
807  *         a target entry (used in query target lists)
808  *
809  * Strictly speaking, a TargetEntry isn't an expression node (since it can't
810  * be evaluated by ExecEvalExpr).  But we treat it as one anyway, since in
811  * very many places it's convenient to process a whole query targetlist as a
812  * single expression tree.
813  *
814  * In a SELECT's targetlist, resno should always be equal to the item's
815  * ordinal position (counting from 1).  However, in an INSERT or UPDATE
816  * targetlist, resno represents the attribute number of the destination
817  * column for the item; so there may be missing or out-of-order resnos.
818  * It is even legal to have duplicated resnos; consider
819  *              UPDATE table SET arraycol[1] = ..., arraycol[2] = ..., ...
820  * The two meanings come together in the executor, because the planner
821  * transforms INSERT/UPDATE tlists into a normalized form with exactly
822  * one entry for each column of the destination table.  Before that's
823  * happened, however, it is risky to assume that resno == position.
824  * Generally get_tle_by_resno() should be used rather than list_nth()
825  * to fetch tlist entries by resno, and only in SELECT should you assume
826  * that resno is a unique identifier.
827  *
828  * resname is required to represent the correct column name in non-resjunk
829  * entries of top-level SELECT targetlists, since it will be used as the
830  * column title sent to the frontend.  In most other contexts it is only
831  * a debugging aid, and may be wrong or even NULL.      (In particular, it may
832  * be wrong in a tlist from a stored rule, if the referenced column has been
833  * renamed by ALTER TABLE since the rule was made.      Also, the planner tends
834  * to store NULL rather than look up a valid name for tlist entries in
835  * non-toplevel plan nodes.)  In resjunk entries, resname should be either
836  * a specific system-generated name (such as "ctid") or NULL; anything else
837  * risks confusing ExecGetJunkAttribute!
838  *
839  * ressortgroupref is used in the representation of ORDER BY and
840  * GROUP BY items.      Targetlist entries with ressortgroupref=0 are not
841  * sort/group items.  If ressortgroupref>0, then this item is an ORDER BY or
842  * GROUP BY value.      No two entries in a targetlist may have the same nonzero
843  * ressortgroupref --- but there is no particular meaning to the nonzero
844  * values, except as tags.      (For example, one must not assume that lower
845  * ressortgroupref means a more significant sort key.)  The order of the
846  * associated SortClause or GroupClause lists determine the semantics.
847  *
848  * resorigtbl/resorigcol identify the source of the column, if it is a
849  * simple reference to a column of a base table (or view).      If it is not
850  * a simple reference, these fields are zeroes.
851  *
852  * If resjunk is true then the column is a working column (such as a sort key)
853  * that should be removed from the final output of the query.  Resjunk columns
854  * must have resnos that cannot duplicate any regular column's resno.  Also
855  * note that there are places that assume resjunk columns come after non-junk
856  * columns.
857  *--------------------
858  */
859 typedef struct TargetEntry
860 {
861         Expr            xpr;
862         Expr       *expr;                       /* expression to evaluate */
863         AttrNumber      resno;                  /* attribute number (see notes above) */
864         char       *resname;            /* name of the column (could be NULL) */
865         Index           ressortgroupref;/* nonzero if referenced by a sort/group
866                                                                  * clause */
867         Oid                     resorigtbl;             /* OID of column's source table */
868         AttrNumber      resorigcol;             /* column's number in source table */
869         bool            resjunk;                /* set to true to eliminate the attribute from
870                                                                  * final target list */
871 } TargetEntry;
872
873
874 /* ----------------------------------------------------------------
875  *                                      node types for join trees
876  *
877  * The leaves of a join tree structure are RangeTblRef nodes.  Above
878  * these, JoinExpr nodes can appear to denote a specific kind of join
879  * or qualified join.  Also, FromExpr nodes can appear to denote an
880  * ordinary cross-product join ("FROM foo, bar, baz WHERE ...").
881  * FromExpr is like a JoinExpr of jointype JOIN_INNER, except that it
882  * may have any number of child nodes, not just two.
883  *
884  * NOTE: the top level of a Query's jointree is always a FromExpr.
885  * Even if the jointree contains no rels, there will be a FromExpr.
886  *
887  * NOTE: the qualification expressions present in JoinExpr nodes are
888  * *in addition to* the query's main WHERE clause, which appears as the
889  * qual of the top-level FromExpr.      The reason for associating quals with
890  * specific nodes in the jointree is that the position of a qual is critical
891  * when outer joins are present.  (If we enforce a qual too soon or too late,
892  * that may cause the outer join to produce the wrong set of NULL-extended
893  * rows.)  If all joins are inner joins then all the qual positions are
894  * semantically interchangeable.
895  *
896  * NOTE: in the raw output of gram.y, a join tree contains RangeVar,
897  * RangeSubselect, and RangeFunction nodes, which are all replaced by
898  * RangeTblRef nodes during the parse analysis phase.  Also, the top-level
899  * FromExpr is added during parse analysis; the grammar regards FROM and
900  * WHERE as separate.
901  * ----------------------------------------------------------------
902  */
903
904 /*
905  * RangeTblRef - reference to an entry in the query's rangetable
906  *
907  * We could use direct pointers to the RT entries and skip having these
908  * nodes, but multiple pointers to the same node in a querytree cause
909  * lots of headaches, so it seems better to store an index into the RT.
910  */
911 typedef struct RangeTblRef
912 {
913         NodeTag         type;
914         int                     rtindex;
915 } RangeTblRef;
916
917 /*----------
918  * JoinExpr - for SQL JOIN expressions
919  *
920  * isNatural, using, and quals are interdependent.      The user can write only
921  * one of NATURAL, USING(), or ON() (this is enforced by the grammar).
922  * If he writes NATURAL then parse analysis generates the equivalent USING()
923  * list, and from that fills in "quals" with the right equality comparisons.
924  * If he writes USING() then "quals" is filled with equality comparisons.
925  * If he writes ON() then only "quals" is set.  Note that NATURAL/USING
926  * are not equivalent to ON() since they also affect the output column list.
927  *
928  * alias is an Alias node representing the AS alias-clause attached to the
929  * join expression, or NULL if no clause.  NB: presence or absence of the
930  * alias has a critical impact on semantics, because a join with an alias
931  * restricts visibility of the tables/columns inside it.
932  *
933  * During parse analysis, an RTE is created for the Join, and its index
934  * is filled into rtindex.      This RTE is present mainly so that Vars can
935  * be created that refer to the outputs of the join.
936  *----------
937  */
938 typedef struct JoinExpr
939 {
940         NodeTag         type;
941         JoinType        jointype;               /* type of join */
942         bool            isNatural;              /* Natural join? Will need to shape table */
943         Node       *larg;                       /* left subtree */
944         Node       *rarg;                       /* right subtree */
945         List       *using;                      /* USING clause, if any (list of String) */
946         Node       *quals;                      /* qualifiers on join, if any */
947         Alias      *alias;                      /* user-written alias clause, if any */
948         int                     rtindex;                /* RT index assigned for join */
949 } JoinExpr;
950
951 /*----------
952  * FromExpr - represents a FROM ... WHERE ... construct
953  *
954  * This is both more flexible than a JoinExpr (it can have any number of
955  * children, including zero) and less so --- we don't need to deal with
956  * aliases and so on.  The output column set is implicitly just the union
957  * of the outputs of the children.
958  *----------
959  */
960 typedef struct FromExpr
961 {
962         NodeTag         type;
963         List       *fromlist;           /* List of join subtrees */
964         Node       *quals;                      /* qualifiers on join, if any */
965 } FromExpr;
966
967 #endif   /* PRIMNODES_H */