]> granicus.if.org Git - postgresql/blob - src/include/nodes/primnodes.h
Phase 3 of read-only-plans project: ExecInitExpr now builds expression
[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-2002, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  * $Id: primnodes.h,v 1.74 2002/12/13 19:46:00 tgl 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
23
24 /* ----------------------------------------------------------------
25  *                                              node definitions
26  * ----------------------------------------------------------------
27  */
28
29 /*--------------------
30  * Resdom (Result Domain)
31  *
32  * Notes:
33  * ressortgroupref is the parse/plan-time representation of ORDER BY and
34  * GROUP BY items.      Targetlist entries with ressortgroupref=0 are not
35  * sort/group items.  If ressortgroupref>0, then this item is an ORDER BY or
36  * GROUP BY value.      No two entries in a targetlist may have the same nonzero
37  * ressortgroupref --- but there is no particular meaning to the nonzero
38  * values, except as tags.      (For example, one must not assume that lower
39  * ressortgroupref means a more significant sort key.)  The order of the
40  * associated SortClause or GroupClause lists determine the semantics.
41  *
42  * reskey and reskeyop are the execution-time representation of sorting.
43  * reskey must be zero in any non-sort-key item.  The reskey of sort key
44  * targetlist items for a sort plan node is 1,2,...,n for the n sort keys.
45  * The reskeyop of each such targetlist item is the sort operator's OID.
46  * reskeyop will be zero in non-sort-key items.
47  *
48  * Both reskey and reskeyop are typically zero during parse/plan stages.
49  * The executor does not pay any attention to ressortgroupref.
50  *--------------------
51  */
52 typedef struct Resdom
53 {
54         NodeTag         type;
55         AttrNumber      resno;                  /* attribute number */
56         Oid                     restype;                /* type of the value */
57         int32           restypmod;              /* type-specific modifier of the value */
58         char       *resname;            /* name of the resdom (could be NULL) */
59         Index           ressortgroupref;
60         /* nonzero if referenced by a sort/group clause */
61         Index           reskey;                 /* order of key in a sort (for those > 0) */
62         Oid                     reskeyop;               /* sort operator's Oid */
63         bool            resjunk;                /* set to true to eliminate the attribute
64                                                                  * from final target list */
65 } Resdom;
66
67
68 /*
69  * Alias -
70  *        specifies an alias for a range variable; the alias might also
71  *        specify renaming of columns within the table.
72  */
73 typedef struct Alias
74 {
75         NodeTag         type;
76         char       *aliasname;          /* aliased rel name (never qualified) */
77         List       *colnames;           /* optional list of column aliases */
78         /* Note: colnames is a list of Value nodes (always strings) */
79 } Alias;
80
81 typedef enum InhOption
82 {
83         INH_NO,                                         /* Do NOT scan child tables */
84         INH_YES,                                        /* DO scan child tables */
85         INH_DEFAULT                                     /* Use current SQL_inheritance option */
86 } InhOption;
87
88 /*
89  * RangeVar - range variable, used in FROM clauses
90  *
91  * Also used to represent table names in utility statements; there, the alias
92  * field is not used, and inhOpt shows whether to apply the operation
93  * recursively to child tables.  In some contexts it is also useful to carry
94  * a TEMP table indication here.
95  */
96 typedef struct RangeVar
97 {
98         NodeTag         type;
99         char       *catalogname;        /* the catalog (database) name, or NULL */
100         char       *schemaname;         /* the schema name, or NULL */
101         char       *relname;            /* the relation/sequence name */
102         InhOption       inhOpt;                 /* expand rel by inheritance? recursively
103                                                                  * act on children? */
104         bool            istemp;                 /* is this a temp relation/sequence? */
105         Alias      *alias;                      /* table alias & optional column aliases */
106 } RangeVar;
107
108
109 /* ----------------------------------------------------------------
110  *                                      node types for executable expressions
111  * ----------------------------------------------------------------
112  */
113
114 /*
115  * Expr - generic superclass for executable-expression nodes
116  *
117  * All node types that are used in executable expression trees should derive
118  * from Expr (that is, have Expr as their first field).  Since Expr only
119  * contains NodeTag, this is a formality, but it is an easy form of
120  * documentation.  See also the ExprState node types in execnodes.h.
121  */
122 typedef struct Expr
123 {
124         NodeTag         type;
125 } Expr;
126
127 /*
128  * Var - expression node representing a variable (ie, a table column)
129  *
130  * Note: during parsing/planning, varnoold/varoattno are always just copies
131  * of varno/varattno.  At the tail end of planning, Var nodes appearing in
132  * upper-level plan nodes are reassigned to point to the outputs of their
133  * subplans; for example, in a join node varno becomes INNER or OUTER and
134  * varattno becomes the index of the proper element of that subplan's target
135  * list.  But varnoold/varoattno continue to hold the original values.
136  * The code doesn't really need varnoold/varoattno, but they are very useful
137  * for debugging and interpreting completed plans, so we keep them around.
138  */
139 #define    INNER                65000
140 #define    OUTER                65001
141
142 #define    PRS2_OLD_VARNO                       1
143 #define    PRS2_NEW_VARNO                       2
144
145 typedef struct Var
146 {
147         Expr            xpr;
148         Index           varno;                  /* index of this var's relation in the
149                                                                  * range table (could also be INNER or
150                                                                  * OUTER) */
151         AttrNumber      varattno;               /* attribute number of this var, or zero
152                                                                  * for all */
153         Oid                     vartype;                /* pg_type tuple OID for the type of this
154                                                                  * var */
155         int32           vartypmod;              /* pg_attribute typmod value */
156         Index           varlevelsup;
157
158         /*
159          * for subquery variables referencing outer relations; 0 in a normal
160          * var, >0 means N levels up
161          */
162         Index           varnoold;               /* original value of varno, for debugging */
163         AttrNumber      varoattno;              /* original value of varattno */
164 } Var;
165
166 /*
167  * Const
168  */
169 typedef struct Const
170 {
171         Expr            xpr;
172         Oid                     consttype;              /* PG_TYPE OID of the constant's datatype */
173         int                     constlen;               /* typlen of the constant's datatype */
174         Datum           constvalue;             /* the constant's value */
175         bool            constisnull;    /* whether the constant is null (if true,
176                                                                  * constvalue is undefined) */
177         bool            constbyval;             /* whether this datatype is passed by value.
178                                                                  * If true, then all the information is
179                                                                  * stored in the Datum.
180                                                                  * If false, then the Datum contains a
181                                                                  * pointer to the information. */
182 } Const;
183
184 /* ----------------
185  * Param
186  *              paramkind - specifies the kind of parameter. The possible values
187  *              for this field are specified in "params.h", and they are:
188  *
189  *              PARAM_NAMED: The parameter has a name, i.e. something
190  *                              like `$.salary' or `$.foobar'.
191  *                              In this case field `paramname' must be a valid name.
192  *
193  *              PARAM_NUM:       The parameter has only a numeric identifier,
194  *                              i.e. something like `$1', `$2' etc.
195  *                              The number is contained in the `paramid' field.
196  *
197  *              PARAM_EXEC:      The parameter is an internal executor parameter.
198  *                              It has a number contained in the `paramid' field.
199  * ----------------
200  */
201 typedef struct Param
202 {
203         Expr            xpr;
204         int                     paramkind;              /* kind of parameter. See above */
205         AttrNumber      paramid;                /* numeric ID for parameter ("$1") */
206         char       *paramname;          /* name for parameter ("$.foo") */
207         Oid                     paramtype;              /* PG_TYPE OID of parameter's datatype */
208 } Param;
209
210 /*
211  * Aggref
212  */
213 typedef struct Aggref
214 {
215         Expr            xpr;
216         Oid                     aggfnoid;               /* pg_proc Oid of the aggregate */
217         Oid                     aggtype;                /* type Oid of result of the aggregate */
218         Expr       *target;                     /* expression we are aggregating on */
219         bool            aggstar;                /* TRUE if argument was really '*' */
220         bool            aggdistinct;    /* TRUE if it's agg(DISTINCT ...) */
221 } Aggref;
222
223 /* ----------------
224  *      ArrayRef: describes an array subscripting operation
225  *
226  * An ArrayRef can describe fetching a single element from an array,
227  * fetching a subarray (array slice), storing a single element into
228  * an array, or storing a slice.  The "store" cases work with an
229  * initial array value and a source value that is inserted into the
230  * appropriate part of the array; the result of the operation is an
231  * entire new modified array value.
232  *
233  * If reflowerindexpr = NIL, then we are fetching or storing a single array
234  * element at the subscripts given by refupperindexpr.  Otherwise we are
235  * fetching or storing an array slice, that is a rectangular subarray
236  * with lower and upper bounds given by the index expressions.
237  * reflowerindexpr must be the same length as refupperindexpr when it
238  * is not NIL.
239  *
240  * Note: array types can be fixed-length (refattrlength > 0), but only
241  * when the element type is itself fixed-length.  Otherwise they are
242  * varlena structures and have refattrlength = -1.      In any case,
243  * an array type is never pass-by-value.
244  *
245  * Note: refrestype is NOT the element type, but the array type,
246  * when doing subarray fetch or either type of store.  It might be a good
247  * idea to include a refelemtype field as well.
248  * ----------------
249  */
250 typedef struct ArrayRef
251 {
252         Expr            xpr;
253         Oid                     refrestype;             /* type of the result of the ArrayRef
254                                                                  * operation */
255         int                     refattrlength;  /* typlen of array type */
256         int                     refelemlength;  /* typlen of the array element type */
257         bool            refelembyval;   /* is the element type pass-by-value? */
258         char            refelemalign;   /* typalign of the element type */
259         List       *refupperindexpr;/* expressions that evaluate to upper
260                                                                  * array indexes */
261         List       *reflowerindexpr;/* expressions that evaluate to lower
262                                                                  * array indexes */
263         Expr       *refexpr;            /* the expression that evaluates to an
264                                                                  * array value */
265         Expr       *refassgnexpr;       /* expression for the source value, or
266                                                                  * NULL if fetch */
267 } ArrayRef;
268
269 /*
270  * CoercionContext - distinguishes the allowed set of type casts
271  *
272  * NB: ordering of the alternatives is significant; later (larger) values
273  * allow more casts than earlier ones.
274  */
275 typedef enum CoercionContext
276 {
277         COERCION_IMPLICIT,                      /* coercion in context of expression */
278         COERCION_ASSIGNMENT,            /* coercion in context of assignment */
279         COERCION_EXPLICIT                       /* explicit cast operation */
280 } CoercionContext;
281
282 /*
283  * CoercionForm - information showing how to display a function-call node
284  */
285 typedef enum CoercionForm
286 {
287         COERCE_EXPLICIT_CALL,           /* display as a function call */
288         COERCE_EXPLICIT_CAST,           /* display as an explicit cast */
289         COERCE_IMPLICIT_CAST,           /* implicit cast, so hide it */
290         COERCE_DONTCARE                         /* special case for pathkeys */
291 } CoercionForm;
292
293 /*
294  * FuncExpr - expression node for a function call
295  */
296 typedef struct FuncExpr
297 {
298         Expr            xpr;
299         Oid                     funcid;                 /* PG_PROC OID of the function */
300         Oid                     funcresulttype; /* PG_TYPE OID of result value */
301         bool            funcretset;             /* true if function returns set */
302         CoercionForm funcformat;        /* how to display this function call */
303         List       *args;                       /* arguments to the function */
304 } FuncExpr;
305
306 /*
307  * OpExpr - expression node for an operator invocation
308  *
309  * Semantically, this is essentially the same as a function call.
310  *
311  * Note that opfuncid is not necessarily filled in immediately on creation
312  * of the node.  The planner makes sure it is valid before passing the node
313  * tree to the executor, but during parsing/planning opfuncid is typically 0.
314  */
315 typedef struct OpExpr
316 {
317         Expr            xpr;
318         Oid                     opno;                   /* PG_OPERATOR OID of the operator */
319         Oid                     opfuncid;               /* PG_PROC OID of underlying function */
320         Oid                     opresulttype;   /* PG_TYPE OID of result value */
321         bool            opretset;               /* true if operator returns set */
322         List       *args;                       /* arguments to the operator (1 or 2) */
323 } OpExpr;
324
325 /*
326  * DistinctExpr - expression node for "x IS DISTINCT FROM y"
327  *
328  * Except for the nodetag, this is represented identically to an OpExpr
329  * referencing the "=" operator for x and y.
330  * We use "=", not the more obvious "<>", because more datatypes have "="
331  * than "<>".  This means the executor must invert the operator result.
332  * Note that the operator function won't be called at all if either input
333  * is NULL, since then the result can be determined directly.
334  */
335 typedef OpExpr DistinctExpr;
336
337 /*
338  * BoolExpr - expression node for the basic Boolean operators AND, OR, NOT
339  *
340  * Notice the arguments are given as a List.  For NOT, of course the list
341  * must always have exactly one element.  For AND and OR, the executor can
342  * handle any number of arguments.  The parser treats AND and OR as binary
343  * and so it only produces two-element lists, but the optimizer will flatten
344  * trees of AND and OR nodes to produce longer lists when possible.
345  */
346 typedef enum BoolExprType
347 {
348         AND_EXPR, OR_EXPR, NOT_EXPR
349 } BoolExprType;
350
351 typedef struct BoolExpr
352 {
353         Expr            xpr;
354         BoolExprType boolop;
355         List       *args;                       /* arguments to this expression */
356 } BoolExpr;
357
358 /* ----------------
359  * SubLink
360  *
361  * A SubLink represents a subselect appearing in an expression, and in some
362  * cases also the combining operator(s) just above it.  The subLinkType
363  * indicates the form of the expression represented:
364  *      EXISTS_SUBLINK          EXISTS(SELECT ...)
365  *      ALL_SUBLINK                     (lefthand) op ALL (SELECT ...)
366  *      ANY_SUBLINK                     (lefthand) op ANY (SELECT ...)
367  *      MULTIEXPR_SUBLINK       (lefthand) op (SELECT ...)
368  *      EXPR_SUBLINK            (SELECT with single targetlist item ...)
369  * For ALL, ANY, and MULTIEXPR, the lefthand is a list of expressions of the
370  * same length as the subselect's targetlist.  MULTIEXPR will *always* have
371  * a list with more than one entry; if the subselect has just one target
372  * then the parser will create an EXPR_SUBLINK instead (and any operator
373  * above the subselect will be represented separately).  Note that both
374  * MULTIEXPR and EXPR require the subselect to deliver only one row.
375  * ALL, ANY, and MULTIEXPR require the combining operators to deliver boolean
376  * results.  These are reduced to one result per row using OR or AND semantics
377  * depending on the "useor" flag.  ALL and ANY combine the per-row results
378  * using AND and OR semantics respectively.
379  *
380  * SubLink is classed as an Expr node, but it is not actually executable;
381  * it must be replaced in the expression tree by a SubPlanExpr node during
382  * planning.
383  *
384  * NOTE: lefthand and oper have varying meanings depending on where you look
385  * in the parse/plan pipeline:
386  * 1. gram.y delivers a list of the (untransformed) lefthand expressions in
387  *        lefthand, and sets oper to a single A_Expr (not a list!) containing
388  *        the string name of the operator, but no arguments.
389  * 2. The parser's expression transformation transforms lefthand normally,
390  *        and replaces oper with a list of OpExpr nodes, one per lefthand
391  *        expression.  These nodes represent the parser's resolution of exactly
392  *        which operator to apply to each pair of lefthand and targetlist
393  *        expressions.  However, we have not constructed complete Expr trees for
394  *        these operations yet: the args fields of the OpExpr nodes are NIL.
395  *        This is the representation seen in saved rules and in the rewriter.
396  * 3. Finally, the planner converts the oper list to a list of normal OpExpr
397  *        nodes representing the application of the operator(s) to the lefthand
398  *        expressions and values from the inner targetlist.  The inner
399  *        targetlist items are represented by placeholder Param nodes.
400  *        The lefthand field is set to NIL, since its expressions are now in
401  *        the Expr list.  This representation is passed to the executor.
402  *
403  * Planner routines that might see either representation 2 or 3 can tell
404  * the difference by checking whether lefthand is NIL or not.  Also,
405  * representation 2 appears in a "bare" SubLink, while representation 3 is
406  * found in SubLinks that are children of SubPlanExpr nodes.
407  *
408  * In EXISTS and EXPR SubLinks, both lefthand and oper are unused and are
409  * always NIL.  useor is not significant either for these sublink types.
410  * ----------------
411  */
412 typedef enum SubLinkType
413 {
414         EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, MULTIEXPR_SUBLINK, EXPR_SUBLINK
415 } SubLinkType;
416
417
418 typedef struct SubLink
419 {
420         Expr            xpr;
421         SubLinkType subLinkType;        /* EXISTS, ALL, ANY, MULTIEXPR, EXPR */
422         bool            useor;                  /* TRUE to combine column results with
423                                                                  * "OR" not "AND" */
424         List       *lefthand;           /* list of outer-query expressions on the
425                                                                  * left */
426         List       *oper;                       /* list of OpExpr nodes for combining
427                                                                  * operators, or final list of executable
428                                                                  * expressions */
429         Node       *subselect;          /* subselect as Query* or parsetree */
430 } SubLink;
431
432 /*
433  * SubPlanExpr - executable expression node for a subplan (sub-SELECT)
434  *
435  * The planner replaces SubLink nodes in expression trees with SubPlanExpr
436  * nodes after it has finished planning the subquery.  See notes above.
437  */
438 typedef struct SubPlanExpr
439 {
440         Expr            xpr;
441         Oid                     typeOid;                /* PG_TYPE OID of the expression result */
442         struct Plan *plan;                      /* subselect plan itself */
443         int                     plan_id;                /* dummy thing because of we haven't equal
444                                                                  * funcs for plan nodes... actually, we
445                                                                  * could put *plan itself somewhere else
446                                                                  * (TopPlan node ?)... */
447         List       *rtable;                     /* range table for subselect */
448         /* setParam and parParam are lists of integers (param IDs) */
449         List       *setParam;           /* non-correlated EXPR & EXISTS subqueries
450                                                                  * have to set some Params for paren Plan */
451         List       *parParam;           /* indices of input Params from parent plan */
452         List       *args;                       /* exprs to pass as parParam values */
453         SubLink    *sublink;            /* SubLink node from parser; holds info
454                                                                  * about what to do with subselect's
455                                                                  * results */
456 } SubPlanExpr;
457
458 /* ----------------
459  * FieldSelect
460  *
461  * FieldSelect represents the operation of extracting one field from a tuple
462  * value.  At runtime, the input expression is expected to yield a Datum
463  * that contains a pointer-to-TupleTableSlot.  The specified field number
464  * is extracted and returned as a Datum.
465  * ----------------
466  */
467
468 typedef struct FieldSelect
469 {
470         Expr            xpr;
471         Expr       *arg;                        /* input expression */
472         AttrNumber      fieldnum;               /* attribute number of field to extract */
473         Oid                     resulttype;             /* type of the field (result type of this
474                                                                  * node) */
475         int32           resulttypmod;   /* output typmod (usually -1) */
476 } FieldSelect;
477
478 /* ----------------
479  * RelabelType
480  *
481  * RelabelType represents a "dummy" type coercion between two binary-
482  * compatible datatypes, such as reinterpreting the result of an OID
483  * expression as an int4.  It is a no-op at runtime; we only need it
484  * to provide a place to store the correct type to be attributed to
485  * the expression result during type resolution.  (We can't get away
486  * with just overwriting the type field of the input expression node,
487  * so we need a separate node to show the coercion's result type.)
488  * ----------------
489  */
490
491 typedef struct RelabelType
492 {
493         Expr            xpr;
494         Expr       *arg;                        /* input expression */
495         Oid                     resulttype;             /* output type of coercion expression */
496         int32           resulttypmod;   /* output typmod (usually -1) */
497         CoercionForm relabelformat;     /* how to display this node */
498 } RelabelType;
499
500 /*
501  * CaseExpr - a CASE expression
502  */
503 typedef struct CaseExpr
504 {
505         Expr            xpr;
506         Oid                     casetype;               /* type of expression result */
507         Expr       *arg;                        /* implicit equality comparison argument */
508         List       *args;                       /* the arguments (list of WHEN clauses) */
509         Expr       *defresult;          /* the default result (ELSE clause) */
510 } CaseExpr;
511
512 /*
513  * CaseWhen - an argument to a CASE expression
514  */
515 typedef struct CaseWhen
516 {
517         Expr            xpr;
518         Expr       *expr;                       /* condition expression */
519         Expr       *result;                     /* substitution result */
520 } CaseWhen;
521
522 /* ----------------
523  * NullTest
524  *
525  * NullTest represents the operation of testing a value for NULLness.
526  * Currently, we only support scalar input values, but eventually a
527  * row-constructor input should be supported.
528  * The appropriate test is performed and returned as a boolean Datum.
529  * ----------------
530  */
531
532 typedef enum NullTestType
533 {
534         IS_NULL, IS_NOT_NULL
535 } NullTestType;
536
537 typedef struct NullTest
538 {
539         Expr            xpr;
540         Expr       *arg;                        /* input expression */
541         NullTestType nulltesttype;      /* IS NULL, IS NOT NULL */
542 } NullTest;
543
544 /*
545  * BooleanTest
546  *
547  * BooleanTest represents the operation of determining whether a boolean
548  * is TRUE, FALSE, or UNKNOWN (ie, NULL).  All six meaningful combinations
549  * are supported.  Note that a NULL input does *not* cause a NULL result.
550  * The appropriate test is performed and returned as a boolean Datum.
551  */
552
553 typedef enum BoolTestType
554 {
555         IS_TRUE, IS_NOT_TRUE, IS_FALSE, IS_NOT_FALSE, IS_UNKNOWN, IS_NOT_UNKNOWN
556 } BoolTestType;
557
558 typedef struct BooleanTest
559 {
560         Expr            xpr;
561         Expr       *arg;                        /* input expression */
562         BoolTestType booltesttype;      /* test type */
563 } BooleanTest;
564
565 /*
566  * ConstraintTest
567  *
568  * ConstraintTest represents the operation of testing a value to see whether
569  * it meets a constraint.  If so, the input value is returned as the result;
570  * if not, an error is raised.
571  */
572
573 typedef enum ConstraintTestType
574 {
575         CONSTR_TEST_NOTNULL,
576         CONSTR_TEST_CHECK
577 } ConstraintTestType;
578
579 typedef struct ConstraintTest
580 {
581         Expr            xpr;
582         Expr       *arg;                        /* input expression */
583         ConstraintTestType testtype;    /* test type */
584         char       *name;                       /* name of constraint (for error msgs) */
585         char       *domname;            /* name of domain (for error messages) */
586         Expr       *check_expr;         /* for CHECK test, a boolean expression */
587 } ConstraintTest;
588
589 /*
590  * Placeholder node for the value to be processed by a domain's check
591  * constraint.  This is effectively like a Param, but can be implemented more
592  * simply since we need only one replacement value at a time.
593  *
594  * Note: the typeId/typeMod will be set from the domain's base type, not
595  * the domain itself.  This is because we shouldn't consider the value to
596  * be a member of the domain if we haven't yet checked its constraints.
597  */
598 typedef struct ConstraintTestValue
599 {
600         Expr            xpr;
601         Oid                     typeId;                 /* type for substituted value */
602         int32           typeMod;                /* typemod for substituted value */
603 } ConstraintTestValue;
604
605
606 /*
607  * TargetEntry -
608  *         a target entry (used in query target lists)
609  *
610  * Strictly speaking, a TargetEntry isn't an expression node (since it can't
611  * be evaluated by ExecEvalExpr).  But we treat it as one anyway, since in
612  * very many places it's convenient to process a whole query targetlist as a
613  * single expression tree.
614  *
615  * The separation between TargetEntry and Resdom is historical.  One of these
616  * days, Resdom should probably get folded into TargetEntry.
617  */
618 typedef struct TargetEntry
619 {
620         Expr            xpr;
621         Resdom     *resdom;                     /* descriptor for targetlist item */
622         Expr       *expr;                       /* expression to evaluate */
623 } TargetEntry;
624
625
626 /* ----------------------------------------------------------------
627  *                                      node types for join trees
628  *
629  * The leaves of a join tree structure are RangeTblRef nodes.  Above
630  * these, JoinExpr nodes can appear to denote a specific kind of join
631  * or qualified join.  Also, FromExpr nodes can appear to denote an
632  * ordinary cross-product join ("FROM foo, bar, baz WHERE ...").
633  * FromExpr is like a JoinExpr of jointype JOIN_INNER, except that it
634  * may have any number of child nodes, not just two.  Also, there is an
635  * implementation-defined difference: the planner is allowed to join the
636  * children of a FromExpr using whatever join order seems good to it.
637  * At present, JoinExpr nodes are always joined in exactly the order
638  * implied by the jointree structure (except the planner may choose to
639  * swap inner and outer members of a join pair).
640  *
641  * NOTE: the top level of a Query's jointree is always a FromExpr.
642  * Even if the jointree contains no rels, there will be a FromExpr.
643  *
644  * NOTE: the qualification expressions present in JoinExpr nodes are
645  * *in addition to* the query's main WHERE clause, which appears as the
646  * qual of the top-level FromExpr.      The reason for associating quals with
647  * specific nodes in the jointree is that the position of a qual is critical
648  * when outer joins are present.  (If we enforce a qual too soon or too late,
649  * that may cause the outer join to produce the wrong set of NULL-extended
650  * rows.)  If all joins are inner joins then all the qual positions are
651  * semantically interchangeable.
652  *
653  * NOTE: in the raw output of gram.y, a join tree contains RangeVar,
654  * RangeSubselect, and RangeFunction nodes, which are all replaced by
655  * RangeTblRef nodes during the parse analysis phase.  Also, the top-level
656  * FromExpr is added during parse analysis; the grammar regards FROM and
657  * WHERE as separate.
658  * ----------------------------------------------------------------
659  */
660
661 /*
662  * RangeTblRef - reference to an entry in the query's rangetable
663  *
664  * We could use direct pointers to the RT entries and skip having these
665  * nodes, but multiple pointers to the same node in a querytree cause
666  * lots of headaches, so it seems better to store an index into the RT.
667  */
668 typedef struct RangeTblRef
669 {
670         NodeTag         type;
671         int                     rtindex;
672 } RangeTblRef;
673
674 /*----------
675  * JoinExpr - for SQL JOIN expressions
676  *
677  * isNatural, using, and quals are interdependent.      The user can write only
678  * one of NATURAL, USING(), or ON() (this is enforced by the grammar).
679  * If he writes NATURAL then parse analysis generates the equivalent USING()
680  * list, and from that fills in "quals" with the right equality comparisons.
681  * If he writes USING() then "quals" is filled with equality comparisons.
682  * If he writes ON() then only "quals" is set.  Note that NATURAL/USING
683  * are not equivalent to ON() since they also affect the output column list.
684  *
685  * alias is an Alias node representing the AS alias-clause attached to the
686  * join expression, or NULL if no clause.  NB: presence or absence of the
687  * alias has a critical impact on semantics, because a join with an alias
688  * restricts visibility of the tables/columns inside it.
689  *
690  * During parse analysis, an RTE is created for the Join, and its index
691  * is filled into rtindex.      This RTE is present mainly so that Vars can
692  * be created that refer to the outputs of the join.
693  *----------
694  */
695 typedef struct JoinExpr
696 {
697         NodeTag         type;
698         JoinType        jointype;               /* type of join */
699         bool            isNatural;              /* Natural join? Will need to shape table */
700         Node       *larg;                       /* left subtree */
701         Node       *rarg;                       /* right subtree */
702         List       *using;                      /* USING clause, if any (list of String) */
703         Node       *quals;                      /* qualifiers on join, if any */
704         Alias      *alias;                      /* user-written alias clause, if any */
705         int                     rtindex;                /* RT index assigned for join */
706 } JoinExpr;
707
708 /*----------
709  * FromExpr - represents a FROM ... WHERE ... construct
710  *
711  * This is both more flexible than a JoinExpr (it can have any number of
712  * children, including zero) and less so --- we don't need to deal with
713  * aliases and so on.  The output column set is implicitly just the union
714  * of the outputs of the children.
715  *----------
716  */
717 typedef struct FromExpr
718 {
719         NodeTag         type;
720         List       *fromlist;           /* List of join subtrees */
721         Node       *quals;                      /* qualifiers on join, if any */
722 } FromExpr;
723
724 #endif   /* PRIMNODES_H */