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.71 2002/11/30 21:25:06 tgl 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 * ----------------------------------------------------------------
143 * CoercionContext - distinguishes the allowed set of type casts
145 * NB: ordering of the alternatives is significant; later (larger) values
146 * allow more casts than earlier ones.
148 typedef enum CoercionContext
150 COERCION_IMPLICIT, /* coercion in context of expression */
151 COERCION_ASSIGNMENT, /* coercion in context of assignment */
152 COERCION_EXPLICIT /* explicit cast operation */
156 * CoercionForm - information showing how to display a function-call node
158 typedef enum CoercionForm
160 COERCE_EXPLICIT_CALL, /* display as a function call */
161 COERCE_EXPLICIT_CAST, /* display as an explicit cast */
162 COERCE_IMPLICIT_CAST, /* implicit cast, so hide it */
163 COERCE_DONTCARE /* special case for pathkeys */
169 * Note: DISTINCT_EXPR implements the "x IS DISTINCT FROM y" construct.
170 * This is similar to an OP_EXPR, except for its handling of NULL inputs.
171 * The oper field is always an Oper node for the "=" operator for x and y.
172 * (We use "=", not the more obvious "<>", because more datatypes have "="
173 * than "<>". This means the executor must invert the operator result.)
177 OP_EXPR, DISTINCT_EXPR, FUNC_EXPR,
178 OR_EXPR, AND_EXPR, NOT_EXPR, SUBPLAN_EXPR
184 Oid typeOid; /* oid of the type of this expression */
185 OpType opType; /* kind of expression */
186 Node *oper; /* operator node if needed (Oper, Func, or
188 List *args; /* arguments to this expression */
192 * Oper - Expr subnode for an OP_EXPR (or DISTINCT_EXPR)
194 * NOTE: in the good old days 'opno' used to be both (or either, or
195 * neither) the pg_operator oid, and/or the pg_proc oid depending
196 * on the postgres module in question (parser->pg_operator,
197 * executor->pg_proc, planner->both), the mood of the programmer,
198 * and the phase of the moon (rumors that it was also depending on the day
199 * of the week are probably false). To make things even more postgres-like
200 * (i.e. a mess) some comments were referring to 'opno' using the name
201 * 'opid'. Anyway, now we have two separate fields, and of course that
202 * immediately removes all bugs from the code... [ sp :-) ].
204 * Note also that opid is not necessarily filled in immediately on creation
205 * of the node. The planner makes sure it is valid before passing the node
206 * tree to the executor, but during parsing/planning opid is typically 0.
211 Oid opno; /* PG_OPERATOR OID of the operator */
212 Oid opid; /* PG_PROC OID of underlying function */
213 Oid opresulttype; /* PG_TYPE OID of result value */
214 bool opretset; /* true if operator returns set */
215 FunctionCachePtr op_fcache; /* runtime state, else NULL */
219 * Func - Expr subnode for a FUNC_EXPR
224 Oid funcid; /* PG_PROC OID of the function */
225 Oid funcresulttype; /* PG_TYPE OID of result value */
226 bool funcretset; /* true if function returns set */
227 CoercionForm funcformat; /* how to display this function call */
228 FunctionCachePtr func_fcache; /* runtime state, or NULL */
234 * Note: during parsing/planning, varnoold/varoattno are always just copies
235 * of varno/varattno. At the tail end of planning, Var nodes appearing in
236 * upper-level plan nodes are reassigned to point to the outputs of their
237 * subplans; for example, in a join node varno becomes INNER or OUTER and
238 * varattno becomes the index of the proper element of that subplan's target
239 * list. But varnoold/varoattno continue to hold the original values.
240 * The code doesn't really need varnoold/varoattno, but they are very useful
241 * for debugging and interpreting completed plans, so we keep them around.
246 #define PRS2_OLD_VARNO 1
247 #define PRS2_NEW_VARNO 2
252 Index varno; /* index of this var's relation in the
253 * range table (could also be INNER or
255 AttrNumber varattno; /* attribute number of this var, or zero
257 Oid vartype; /* pg_type tuple OID for the type of this
259 int32 vartypmod; /* pg_attribute typmod value */
263 * for subquery variables referencing outer relations; 0 in a normal
264 * var, >0 means N levels up
266 Index varnoold; /* original value of varno, for debugging */
267 AttrNumber varoattno; /* original value of varattno */
276 Oid consttype; /* PG_TYPE OID of the constant's datatype */
277 int constlen; /* typlen of the constant's datatype */
278 Datum constvalue; /* the constant's value */
279 bool constisnull; /* whether the constant is null (if true,
280 * constvalue is undefined) */
281 bool constbyval; /* whether this datatype is passed by value.
282 * If true, then all the information is
283 * stored in the Datum.
284 * If false, then the Datum contains a
285 * pointer to the information. */
290 * paramkind - specifies the kind of parameter. The possible values
291 * for this field are specified in "params.h", and they are:
293 * PARAM_NAMED: The parameter has a name, i.e. something
294 * like `$.salary' or `$.foobar'.
295 * In this case field `paramname' must be a valid name.
297 * PARAM_NUM: The parameter has only a numeric identifier,
298 * i.e. something like `$1', `$2' etc.
299 * The number is contained in the `paramid' field.
301 * PARAM_EXEC: The parameter is an internal executor parameter.
302 * It has a number contained in the `paramid' field.
309 int paramkind; /* kind of parameter. See above */
310 AttrNumber paramid; /* numeric ID for parameter ("$1") */
311 char *paramname; /* name for parameter ("$.foo") */
312 Oid paramtype; /* PG_TYPE OID of parameter's datatype */
318 typedef struct Aggref
321 Oid aggfnoid; /* pg_proc Oid of the aggregate */
322 Oid aggtype; /* type Oid of result of the aggregate */
323 Node *target; /* expression we are aggregating on */
324 bool aggstar; /* TRUE if argument was really '*' */
325 bool aggdistinct; /* TRUE if it's agg(DISTINCT ...) */
326 int aggno; /* workspace for executor (see nodeAgg.c) */
332 * A SubLink represents a subselect appearing in an expression, and in some
333 * cases also the combining operator(s) just above it. The subLinkType
334 * indicates the form of the expression represented:
335 * EXISTS_SUBLINK EXISTS(SELECT ...)
336 * ALL_SUBLINK (lefthand) op ALL (SELECT ...)
337 * ANY_SUBLINK (lefthand) op ANY (SELECT ...)
338 * MULTIEXPR_SUBLINK (lefthand) op (SELECT ...)
339 * EXPR_SUBLINK (SELECT with single targetlist item ...)
340 * For ALL, ANY, and MULTIEXPR, the lefthand is a list of expressions of the
341 * same length as the subselect's targetlist. MULTIEXPR will *always* have
342 * a list with more than one entry; if the subselect has just one target
343 * then the parser will create an EXPR_SUBLINK instead (and any operator
344 * above the subselect will be represented separately). Note that both
345 * MULTIEXPR and EXPR require the subselect to deliver only one row.
346 * ALL, ANY, and MULTIEXPR require the combining operators to deliver boolean
347 * results. These are reduced to one result per row using OR or AND semantics
348 * depending on the "useor" flag. ALL and ANY combine the per-row results
349 * using AND and OR semantics respectively.
351 * NOTE: lefthand and oper have varying meanings depending on where you look
352 * in the parse/plan pipeline:
353 * 1. gram.y delivers a list of the (untransformed) lefthand expressions in
354 * lefthand, and sets oper to a single A_Expr (not a list!) containing
355 * the string name of the operator, but no arguments.
356 * 2. The parser's expression transformation transforms lefthand normally,
357 * and replaces oper with a list of Oper nodes, one per lefthand
358 * expression. These nodes represent the parser's resolution of exactly
359 * which operator to apply to each pair of lefthand and targetlist
360 * expressions. However, we have not constructed actual Expr trees for
361 * these operators yet. This is the representation seen in saved rules
362 * and in the rewriter.
363 * 3. Finally, the planner converts the oper list to a list of normal Expr
364 * nodes representing the application of the operator(s) to the lefthand
365 * expressions and values from the inner targetlist. The inner
366 * targetlist items are represented by placeholder Param nodes.
367 * The lefthand field is set to NIL, since its expressions are now in
368 * the Expr list. This representation is passed to the executor.
370 * Planner routines that might see either representation 2 or 3 can tell
371 * the difference by checking whether lefthand is NIL or not. Also,
372 * representation 2 appears in a "bare" SubLink, while representation 3 is
373 * found in SubLinks that are children of SubPlan nodes.
375 * In EXISTS and EXPR SubLinks, both lefthand and oper are unused and are
376 * always NIL. useor is not significant either for these sublink types.
379 typedef enum SubLinkType
381 EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, MULTIEXPR_SUBLINK, EXPR_SUBLINK
385 typedef struct SubLink
388 SubLinkType subLinkType; /* EXISTS, ALL, ANY, MULTIEXPR, EXPR */
389 bool useor; /* TRUE to combine column results with
391 List *lefthand; /* list of outer-query expressions on the
393 List *oper; /* list of Oper nodes for combining
395 Node *subselect; /* subselect as Query* or parsetree */
399 * ArrayRef: describes an array subscripting operation
401 * An ArrayRef can describe fetching a single element from an array,
402 * fetching a subarray (array slice), storing a single element into
403 * an array, or storing a slice. The "store" cases work with an
404 * initial array value and a source value that is inserted into the
405 * appropriate part of the array; the result of the operation is an
406 * entire new modified array value.
408 * If reflowerindexpr = NIL, then we are fetching or storing a single array
409 * element at the subscripts given by refupperindexpr. Otherwise we are
410 * fetching or storing an array slice, that is a rectangular subarray
411 * with lower and upper bounds given by the index expressions.
412 * reflowerindexpr must be the same length as refupperindexpr when it
415 * Note: array types can be fixed-length (refattrlength > 0), but only
416 * when the element type is itself fixed-length. Otherwise they are
417 * varlena structures and have refattrlength = -1. In any case,
418 * an array type is never pass-by-value.
420 * Note: refrestype is NOT the element type, but the array type,
421 * when doing subarray fetch or either type of store. It might be a good
422 * idea to include a refelemtype field as well.
425 typedef struct ArrayRef
428 Oid refrestype; /* type of the result of the ArrayRef
430 int refattrlength; /* typlen of array type */
431 int refelemlength; /* typlen of the array element type */
432 bool refelembyval; /* is the element type pass-by-value? */
433 char refelemalign; /* typalign of the element type */
434 List *refupperindexpr;/* expressions that evaluate to upper
436 List *reflowerindexpr;/* expressions that evaluate to lower
438 Node *refexpr; /* the expression that evaluates to an
440 Node *refassgnexpr; /* expression for the source value, or
447 * FieldSelect represents the operation of extracting one field from a tuple
448 * value. At runtime, the input expression is expected to yield a Datum
449 * that contains a pointer-to-TupleTableSlot. The specified field number
450 * is extracted and returned as a Datum.
454 typedef struct FieldSelect
457 Node *arg; /* input expression */
458 AttrNumber fieldnum; /* attribute number of field to extract */
459 Oid resulttype; /* type of the field (result type of this
461 int32 resulttypmod; /* output typmod (usually -1) */
467 * RelabelType represents a "dummy" type coercion between two binary-
468 * compatible datatypes, such as reinterpreting the result of an OID
469 * expression as an int4. It is a no-op at runtime; we only need it
470 * to provide a place to store the correct type to be attributed to
471 * the expression result during type resolution. (We can't get away
472 * with just overwriting the type field of the input expression node,
473 * so we need a separate node to show the coercion's result type.)
477 typedef struct RelabelType
480 Node *arg; /* input expression */
481 Oid resulttype; /* output type of coercion expression */
482 int32 resulttypmod; /* output typmod (usually -1) */
483 CoercionForm relabelformat; /* how to display this node */
487 /* ----------------------------------------------------------------
488 * node types for join trees
490 * The leaves of a join tree structure are RangeTblRef nodes. Above
491 * these, JoinExpr nodes can appear to denote a specific kind of join
492 * or qualified join. Also, FromExpr nodes can appear to denote an
493 * ordinary cross-product join ("FROM foo, bar, baz WHERE ...").
494 * FromExpr is like a JoinExpr of jointype JOIN_INNER, except that it
495 * may have any number of child nodes, not just two. Also, there is an
496 * implementation-defined difference: the planner is allowed to join the
497 * children of a FromExpr using whatever join order seems good to it.
498 * At present, JoinExpr nodes are always joined in exactly the order
499 * implied by the jointree structure (except the planner may choose to
500 * swap inner and outer members of a join pair).
502 * NOTE: the top level of a Query's jointree is always a FromExpr.
503 * Even if the jointree contains no rels, there will be a FromExpr.
505 * NOTE: the qualification expressions present in JoinExpr nodes are
506 * *in addition to* the query's main WHERE clause, which appears as the
507 * qual of the top-level FromExpr. The reason for associating quals with
508 * specific nodes in the jointree is that the position of a qual is critical
509 * when outer joins are present. (If we enforce a qual too soon or too late,
510 * that may cause the outer join to produce the wrong set of NULL-extended
511 * rows.) If all joins are inner joins then all the qual positions are
512 * semantically interchangeable.
514 * NOTE: in the raw output of gram.y, a join tree contains RangeVar,
515 * RangeSubselect, and RangeFunction nodes, which are all replaced by
516 * RangeTblRef nodes during the parse analysis phase. Also, the top-level
517 * FromExpr is added during parse analysis; the grammar regards FROM and
519 * ----------------------------------------------------------------
523 * RangeTblRef - reference to an entry in the query's rangetable
525 * We could use direct pointers to the RT entries and skip having these
526 * nodes, but multiple pointers to the same node in a querytree cause
527 * lots of headaches, so it seems better to store an index into the RT.
529 typedef struct RangeTblRef
536 * JoinExpr - for SQL JOIN expressions
538 * isNatural, using, and quals are interdependent. The user can write only
539 * one of NATURAL, USING(), or ON() (this is enforced by the grammar).
540 * If he writes NATURAL then parse analysis generates the equivalent USING()
541 * list, and from that fills in "quals" with the right equality comparisons.
542 * If he writes USING() then "quals" is filled with equality comparisons.
543 * If he writes ON() then only "quals" is set. Note that NATURAL/USING
544 * are not equivalent to ON() since they also affect the output column list.
546 * alias is an Alias node representing the AS alias-clause attached to the
547 * join expression, or NULL if no clause. NB: presence or absence of the
548 * alias has a critical impact on semantics, because a join with an alias
549 * restricts visibility of the tables/columns inside it.
551 * During parse analysis, an RTE is created for the Join, and its index
552 * is filled into rtindex. This RTE is present mainly so that Vars can
553 * be created that refer to the outputs of the join.
556 typedef struct JoinExpr
559 JoinType jointype; /* type of join */
560 bool isNatural; /* Natural join? Will need to shape table */
561 Node *larg; /* left subtree */
562 Node *rarg; /* right subtree */
563 List *using; /* USING clause, if any (list of String) */
564 Node *quals; /* qualifiers on join, if any */
565 Alias *alias; /* user-written alias clause, if any */
566 int rtindex; /* RT index assigned for join */
570 * FromExpr - represents a FROM ... WHERE ... construct
572 * This is both more flexible than a JoinExpr (it can have any number of
573 * children, including zero) and less so --- we don't need to deal with
574 * aliases and so on. The output column set is implicitly just the union
575 * of the outputs of the children.
578 typedef struct FromExpr
581 List *fromlist; /* List of join subtrees */
582 Node *quals; /* qualifiers on join, if any */
585 #endif /* PRIMNODES_H */