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-2002, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
13 * $Id: primnodes.h,v 1.67 2002/09/04 20:31:44 momjian Exp $
15 *-------------------------------------------------------------------------
20 #include "access/attnum.h"
21 #include "nodes/pg_list.h"
23 /* FunctionCache is declared in utils/fcache.h */
24 typedef struct FunctionCache *FunctionCachePtr;
27 /* ----------------------------------------------------------------
29 * ----------------------------------------------------------------
32 /*--------------------
33 * Resdom (Result Domain)
36 * ressortgroupref is the parse/plan-time representation of ORDER BY and
37 * GROUP BY items. Targetlist entries with ressortgroupref=0 are not
38 * sort/group items. If ressortgroupref>0, then this item is an ORDER BY or
39 * GROUP BY value. No two entries in a targetlist may have the same nonzero
40 * ressortgroupref --- but there is no particular meaning to the nonzero
41 * values, except as tags. (For example, one must not assume that lower
42 * ressortgroupref means a more significant sort key.) The order of the
43 * associated SortClause or GroupClause lists determine the semantics.
45 * reskey and reskeyop are the execution-time representation of sorting.
46 * reskey must be zero in any non-sort-key item. The reskey of sort key
47 * targetlist items for a sort plan node is 1,2,...,n for the n sort keys.
48 * The reskeyop of each such targetlist item is the sort operator's OID.
49 * reskeyop will be zero in non-sort-key items.
51 * Both reskey and reskeyop are typically zero during parse/plan stages.
52 * The executor does not pay any attention to ressortgroupref.
58 AttrNumber resno; /* attribute number */
59 Oid restype; /* type of the value */
60 int32 restypmod; /* type-specific modifier of the value */
61 char *resname; /* name of the resdom (could be NULL) */
62 Index ressortgroupref;
63 /* nonzero if referenced by a sort/group clause */
64 Index reskey; /* order of key in a sort (for those > 0) */
65 Oid reskeyop; /* sort operator's Oid */
66 bool resjunk; /* set to true to eliminate the attribute
67 * from final target list */
76 bool fj_initialized; /* true if the Fjoin has already been
77 * initialized for the current target list
79 int fj_nNodes; /* The number of Iter nodes returning sets
80 * that the node will flatten */
81 List *fj_innerNode; /* exactly one Iter node. We eval every
82 * node in the outerList once then eval
83 * the inner node to completion pair the
84 * outerList result vector with each inner
85 * result to form the full result. When
86 * the inner has been exhausted, we get
87 * the next outer result vector and reset
89 DatumPtr fj_results; /* The complete (flattened) result vector */
90 BoolPtr fj_alwaysDone; /* a null vector to indicate sets with a
91 * cardinality of 0, we treat them as the
98 * specifies an alias for a range variable; the alias might also
99 * specify renaming of columns within the table.
104 char *aliasname; /* aliased rel name (never qualified) */
105 List *colnames; /* optional list of column aliases */
106 /* Note: colnames is a list of Value nodes (always strings) */
109 typedef enum InhOption
111 INH_NO, /* Do NOT scan child tables */
112 INH_YES, /* DO scan child tables */
113 INH_DEFAULT /* Use current SQL_inheritance option */
117 * RangeVar - range variable, used in FROM clauses
119 * Also used to represent table names in utility statements; there, the alias
120 * field is not used, and inhOpt shows whether to apply the operation
121 * recursively to child tables. In some contexts it is also useful to carry
122 * a TEMP table indication here.
124 typedef struct RangeVar
127 char *catalogname; /* the catalog (database) name, or NULL */
128 char *schemaname; /* the schema name, or NULL */
129 char *relname; /* the relation/sequence name */
130 InhOption inhOpt; /* expand rel by inheritance? recursively
131 * act on children? */
132 bool istemp; /* is this a temp relation/sequence? */
133 Alias *alias; /* table alias & optional column aliases */
137 /* ----------------------------------------------------------------
138 * node types for executable expressions
139 * ----------------------------------------------------------------
147 OP_EXPR, DISTINCT_EXPR, FUNC_EXPR,
148 OR_EXPR, AND_EXPR, NOT_EXPR, SUBPLAN_EXPR
154 Oid typeOid; /* oid of the type of this expression */
155 OpType opType; /* kind of expression */
156 Node *oper; /* operator node if needed (Oper, Func, or
158 List *args; /* arguments to this expression */
162 * Oper - Expr subnode for an OP_EXPR
164 * NOTE: in the good old days 'opno' used to be both (or either, or
165 * neither) the pg_operator oid, and/or the pg_proc oid depending
166 * on the postgres module in question (parser->pg_operator,
167 * executor->pg_proc, planner->both), the mood of the programmer,
168 * and the phase of the moon (rumors that it was also depending on the day
169 * of the week are probably false). To make things even more postgres-like
170 * (i.e. a mess) some comments were referring to 'opno' using the name
171 * 'opid'. Anyway, now we have two separate fields, and of course that
172 * immediately removes all bugs from the code... [ sp :-) ].
174 * Note also that opid is not necessarily filled in immediately on creation
175 * of the node. The planner makes sure it is valid before passing the node
176 * tree to the executor, but during parsing/planning opid is typically 0.
181 Oid opno; /* PG_OPERATOR OID of the operator */
182 Oid opid; /* PG_PROC OID of underlying function */
183 Oid opresulttype; /* PG_TYPE OID of result value */
184 bool opretset; /* true if operator returns set */
185 FunctionCachePtr op_fcache; /* runtime state, else NULL */
189 * Func - Expr subnode for a FUNC_EXPR
194 Oid funcid; /* PG_PROC OID of the function */
195 Oid funcresulttype; /* PG_TYPE OID of result value */
196 bool funcretset; /* true if function returns set */
197 FunctionCachePtr func_fcache; /* runtime state, or NULL */
203 * Note: during parsing/planning, varnoold/varoattno are always just copies
204 * of varno/varattno. At the tail end of planning, Var nodes appearing in
205 * upper-level plan nodes are reassigned to point to the outputs of their
206 * subplans; for example, in a join node varno becomes INNER or OUTER and
207 * varattno becomes the index of the proper element of that subplan's target
208 * list. But varnoold/varoattno continue to hold the original values.
209 * The code doesn't really need varnoold/varoattno, but they are very useful
210 * for debugging and interpreting completed plans, so we keep them around.
215 #define PRS2_OLD_VARNO 1
216 #define PRS2_NEW_VARNO 2
221 Index varno; /* index of this var's relation in the
222 * range table (could also be INNER or
224 AttrNumber varattno; /* attribute number of this var, or zero
226 Oid vartype; /* pg_type tuple OID for the type of this
228 int32 vartypmod; /* pg_attribute typmod value */
232 * for subquery variables referencing outer relations; 0 in a normal
233 * var, >0 means N levels up
235 Index varnoold; /* original value of varno, for debugging */
236 AttrNumber varoattno; /* original value of varattno */
245 Oid consttype; /* PG_TYPE OID of the constant's value */
246 int constlen; /* length in bytes of the constant's value */
247 Datum constvalue; /* the constant's value */
248 bool constisnull; /* whether the constant is null (if true,
249 * the other fields are undefined) */
250 bool constbyval; /* whether the information in constvalue
251 * if passed by value. If true, then all
252 * the information is stored in the datum.
253 * If false, then the datum contains a
254 * pointer to the information. */
255 bool constisset; /* whether the const represents a set. The
256 * const value corresponding will be the
257 * query that defines the set. */
263 * paramkind - specifies the kind of parameter. The possible values
264 * for this field are specified in "params.h", and they are:
266 * PARAM_NAMED: The parameter has a name, i.e. something
267 * like `$.salary' or `$.foobar'.
268 * In this case field `paramname' must be a valid Name.
270 * PARAM_NUM: The parameter has only a numeric identifier,
271 * i.e. something like `$1', `$2' etc.
272 * The number is contained in the `paramid' field.
274 * PARAM_NEW: Used in PRS2 rule, similar to PARAM_NAMED.
275 * The `paramname' and `paramid' refer to the "NEW" tuple
276 * The `pramname' is the attribute name and `paramid'
277 * is the attribute number.
279 * PARAM_OLD: Same as PARAM_NEW, but in this case we refer to
286 int paramkind; /* specifies the kind of parameter. See
288 AttrNumber paramid; /* numeric identifier for literal-constant
289 * parameters ("$1") */
290 char *paramname; /* attribute name for tuple-substitution
291 * parameters ("$.foo") */
292 Oid paramtype; /* PG_TYPE OID of the parameter's value */
298 typedef struct Aggref
301 Oid aggfnoid; /* pg_proc Oid of the aggregate */
302 Oid aggtype; /* type Oid of result of the aggregate */
303 Node *target; /* expression we are aggregating on */
304 bool aggstar; /* TRUE if argument was really '*' */
305 bool aggdistinct; /* TRUE if it's agg(DISTINCT ...) */
306 int aggno; /* workspace for executor (see nodeAgg.c) */
312 * A SubLink represents a subselect appearing in an expression, and in some
313 * cases also the combining operator(s) just above it. The subLinkType
314 * indicates the form of the expression represented:
315 * EXISTS_SUBLINK EXISTS(SELECT ...)
316 * ALL_SUBLINK (lefthand) op ALL (SELECT ...)
317 * ANY_SUBLINK (lefthand) op ANY (SELECT ...)
318 * MULTIEXPR_SUBLINK (lefthand) op (SELECT ...)
319 * EXPR_SUBLINK (SELECT with single targetlist item ...)
320 * For ALL, ANY, and MULTIEXPR, the lefthand is a list of expressions of the
321 * same length as the subselect's targetlist. MULTIEXPR will *always* have
322 * a list with more than one entry; if the subselect has just one target
323 * then the parser will create an EXPR_SUBLINK instead (and any operator
324 * above the subselect will be represented separately). Note that both
325 * MULTIEXPR and EXPR require the subselect to deliver only one row.
326 * ALL, ANY, and MULTIEXPR require the combining operators to deliver boolean
327 * results. These are reduced to one result per row using OR or AND semantics
328 * depending on the "useor" flag. ALL and ANY combine the per-row results
329 * using AND and OR semantics respectively.
331 * NOTE: lefthand and oper have varying meanings depending on where you look
332 * in the parse/plan pipeline:
333 * 1. gram.y delivers a list of the (untransformed) lefthand expressions in
334 * lefthand, and sets oper to a single A_Expr (not a list!) containing
335 * the string name of the operator, but no arguments.
336 * 2. The parser's expression transformation transforms lefthand normally,
337 * and replaces oper with a list of Oper nodes, one per lefthand
338 * expression. These nodes represent the parser's resolution of exactly
339 * which operator to apply to each pair of lefthand and targetlist
340 * expressions. However, we have not constructed actual Expr trees for
341 * these operators yet. This is the representation seen in saved rules
342 * and in the rewriter.
343 * 3. Finally, the planner converts the oper list to a list of normal Expr
344 * nodes representing the application of the operator(s) to the lefthand
345 * expressions and values from the inner targetlist. The inner
346 * targetlist items are represented by placeholder Param or Const nodes.
347 * The lefthand field is set to NIL, since its expressions are now in
348 * the Expr list. This representation is passed to the executor.
350 * Planner routines that might see either representation 2 or 3 can tell
351 * the difference by checking whether lefthand is NIL or not. Also,
352 * representation 2 appears in a "bare" SubLink, while representation 3 is
353 * found in SubLinks that are children of SubPlan nodes.
355 * In EXISTS and EXPR SubLinks, both lefthand and oper are unused and are
356 * always NIL. useor is not significant either for these sublink types.
359 typedef enum SubLinkType
361 EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, MULTIEXPR_SUBLINK, EXPR_SUBLINK
365 typedef struct SubLink
368 SubLinkType subLinkType; /* EXISTS, ALL, ANY, MULTIEXPR, EXPR */
369 bool useor; /* TRUE to combine column results with
371 List *lefthand; /* list of outer-query expressions on the
373 List *oper; /* list of Oper nodes for combining
375 Node *subselect; /* subselect as Query* or parsetree */
379 * ArrayRef: describes an array subscripting operation
381 * An ArrayRef can describe fetching a single element from an array,
382 * fetching a subarray (array slice), storing a single element into
383 * an array, or storing a slice. The "store" cases work with an
384 * initial array value and a source value that is inserted into the
385 * appropriate part of the array; the result of the operation is an
386 * entire new modified array value.
388 * If reflowerindexpr = NIL, then we are fetching or storing a single array
389 * element at the subscripts given by refupperindexpr. Otherwise we are
390 * fetching or storing an array slice, that is a rectangular subarray
391 * with lower and upper bounds given by the index expressions.
392 * reflowerindexpr must be the same length as refupperindexpr when it
395 * Note: array types can be fixed-length (refattrlength > 0), but only
396 * when the element type is itself fixed-length. Otherwise they are
397 * varlena structures and have refattrlength = -1. In any case,
398 * an array type is never pass-by-value.
400 * Note: refrestype is NOT the element type, but the array type,
401 * when doing subarray fetch or either type of store. It might be a good
402 * idea to include a refelemtype field as well.
405 typedef struct ArrayRef
408 Oid refrestype; /* type of the result of the ArrayRef
410 int refattrlength; /* typlen of array type */
411 int refelemlength; /* typlen of the array element type */
412 bool refelembyval; /* is the element type pass-by-value? */
413 char refelemalign; /* typalign of the element type */
414 List *refupperindexpr;/* expressions that evaluate to upper
416 List *reflowerindexpr;/* expressions that evaluate to lower
418 Node *refexpr; /* the expression that evaluates to an
420 Node *refassgnexpr; /* expression for the source value, or
427 * FieldSelect represents the operation of extracting one field from a tuple
428 * value. At runtime, the input expression is expected to yield a Datum
429 * that contains a pointer-to-TupleTableSlot. The specified field number
430 * is extracted and returned as a Datum.
434 typedef struct FieldSelect
437 Node *arg; /* input expression */
438 AttrNumber fieldnum; /* attribute number of field to extract */
439 Oid resulttype; /* type of the field (result type of this
441 int32 resulttypmod; /* output typmod (usually -1) */
447 * RelabelType represents a "dummy" type coercion between two binary-
448 * compatible datatypes, such as reinterpreting the result of an OID
449 * expression as an int4. It is a no-op at runtime; we only need it
450 * to provide a place to store the correct type to be attributed to
451 * the expression result during type resolution. (We can't get away
452 * with just overwriting the type field of the input expression node,
453 * so we need a separate node to show the coercion's result type.)
457 typedef struct RelabelType
460 Node *arg; /* input expression */
461 Oid resulttype; /* output type of coercion expression */
462 int32 resulttypmod; /* output typmod (usually -1) */
466 /* ----------------------------------------------------------------
467 * node types for join trees
469 * The leaves of a join tree structure are RangeTblRef nodes. Above
470 * these, JoinExpr nodes can appear to denote a specific kind of join
471 * or qualified join. Also, FromExpr nodes can appear to denote an
472 * ordinary cross-product join ("FROM foo, bar, baz WHERE ...").
473 * FromExpr is like a JoinExpr of jointype JOIN_INNER, except that it
474 * may have any number of child nodes, not just two. Also, there is an
475 * implementation-defined difference: the planner is allowed to join the
476 * children of a FromExpr using whatever join order seems good to it.
477 * At present, JoinExpr nodes are always joined in exactly the order
478 * implied by the jointree structure (except the planner may choose to
479 * swap inner and outer members of a join pair).
481 * NOTE: the top level of a Query's jointree is always a FromExpr.
482 * Even if the jointree contains no rels, there will be a FromExpr.
484 * NOTE: the qualification expressions present in JoinExpr nodes are
485 * *in addition to* the query's main WHERE clause, which appears as the
486 * qual of the top-level FromExpr. The reason for associating quals with
487 * specific nodes in the jointree is that the position of a qual is critical
488 * when outer joins are present. (If we enforce a qual too soon or too late,
489 * that may cause the outer join to produce the wrong set of NULL-extended
490 * rows.) If all joins are inner joins then all the qual positions are
491 * semantically interchangeable.
493 * NOTE: in the raw output of gram.y, a join tree contains RangeVar,
494 * RangeSubselect, and RangeFunction nodes, which are all replaced by
495 * RangeTblRef nodes during the parse analysis phase. Also, the top-level
496 * FromExpr is added during parse analysis; the grammar regards FROM and
498 * ----------------------------------------------------------------
502 * RangeTblRef - reference to an entry in the query's rangetable
504 * We could use direct pointers to the RT entries and skip having these
505 * nodes, but multiple pointers to the same node in a querytree cause
506 * lots of headaches, so it seems better to store an index into the RT.
508 typedef struct RangeTblRef
515 * JoinExpr - for SQL JOIN expressions
517 * isNatural, using, and quals are interdependent. The user can write only
518 * one of NATURAL, USING(), or ON() (this is enforced by the grammar).
519 * If he writes NATURAL then parse analysis generates the equivalent USING()
520 * list, and from that fills in "quals" with the right equality comparisons.
521 * If he writes USING() then "quals" is filled with equality comparisons.
522 * If he writes ON() then only "quals" is set. Note that NATURAL/USING
523 * are not equivalent to ON() since they also affect the output column list.
525 * alias is an Alias node representing the AS alias-clause attached to the
526 * join expression, or NULL if no clause. NB: presence or absence of the
527 * alias has a critical impact on semantics, because a join with an alias
528 * restricts visibility of the tables/columns inside it.
530 * During parse analysis, an RTE is created for the Join, and its index
531 * is filled into rtindex. This RTE is present mainly so that Vars can
532 * be created that refer to the outputs of the join.
535 typedef struct JoinExpr
538 JoinType jointype; /* type of join */
539 bool isNatural; /* Natural join? Will need to shape table */
540 Node *larg; /* left subtree */
541 Node *rarg; /* right subtree */
542 List *using; /* USING clause, if any (list of String) */
543 Node *quals; /* qualifiers on join, if any */
544 Alias *alias; /* user-written alias clause, if any */
545 int rtindex; /* RT index assigned for join */
549 * FromExpr - represents a FROM ... WHERE ... construct
551 * This is both more flexible than a JoinExpr (it can have any number of
552 * children, including zero) and less so --- we don't need to deal with
553 * aliases and so on. The output column set is implicitly just the union
554 * of the outputs of the children.
557 typedef struct FromExpr
560 List *fromlist; /* List of join subtrees */
561 Node *quals; /* qualifiers on join, if any */
564 #endif /* PRIMNODES_H */