primnodes.h 20.2 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * primnodes.h
4 5 6 7
 *	  Definitions for "primitive" node types, those that are used in more
 *	  than one of the parse/plan/execute stages of the query pipeline.
 *	  Currently, these are mostly nodes for executable expressions
 *	  and join trees.
8 9
 *
 *
B
Add:  
Bruce Momjian 已提交
10 11
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
12
 *
13
 * $Id: primnodes.h,v 1.48 2000/09/12 21:07:10 tgl Exp $
14 15 16 17
 *
 *-------------------------------------------------------------------------
 */
#ifndef PRIMNODES_H
18
#define PRIMNODES_H
19

20 21
#include "access/attnum.h"
#include "nodes/pg_list.h"
22 23 24 25

/* FunctionCache is declared in utils/fcache.h */
typedef struct FunctionCache *FunctionCachePtr;

26 27

/* ----------------------------------------------------------------
28
 *						node definitions
29 30 31 32 33
 * ----------------------------------------------------------------
 */

/* ----------------
 * Resdom (Result Domain)
34
 *		resno			- attribute number
35 36
 *		restype			- type of the value
 *		restypmod		- type-specific modifier of the value
37
 *		resname			- name of the resdom (could be NULL)
38
 *		ressortgroupref - nonzero if referenced by a sort/group clause
39
 *		reskey			- order of key in a sort (for those > 0)
40
 *		reskeyop		- sort operator's regproc Oid
B
Bruce Momjian 已提交
41 42
 *		resjunk			- set to true to eliminate the attribute
 *						  from final target list
43
 *
44 45
 * Notes:
 * ressortgroupref is the parse/plan-time representation of ORDER BY and
46
 * GROUP BY items.	Targetlist entries with ressortgroupref=0 are not
47
 * sort/group items.  If ressortgroupref>0, then this item is an ORDER BY or
48
 * GROUP BY value.	No two entries in a targetlist may have the same nonzero
49
 * ressortgroupref --- but there is no particular meaning to the nonzero
50 51
 * values, except as tags.	(For example, one must not assume that lower
 * ressortgroupref means a more significant sort key.)	The order of the
52 53 54 55 56 57 58 59 60 61
 * associated SortClause or GroupClause lists determine the semantics.
 *
 * reskey and reskeyop are the execution-time representation of sorting.
 * reskey must be zero in any non-sort-key item.  The reskey of sort key
 * targetlist items for a sort plan node is 1,2,...,n for the n sort keys.
 * The reskeyop of each such targetlist item is the sort operator's
 * regproc OID.  reskeyop will be zero in non-sort-key items.
 *
 * Both reskey and reskeyop are typically zero during parse/plan stages.
 * The executor does not pay any attention to ressortgroupref.
62 63
 * ----------------
 */
64 65
typedef struct Resdom
{
66 67 68
	NodeTag		type;
	AttrNumber	resno;
	Oid			restype;
69
	int32		restypmod;
70
	char	   *resname;
71
	Index		ressortgroupref;
72 73
	Index		reskey;
	Oid			reskeyop;
B
Bruce Momjian 已提交
74
	bool		resjunk;
75
} Resdom;
76 77 78

/* -------------
 * Fjoin
79 80 81 82 83 84 85 86 87 88 89 90 91 92
 *		initialized		- true if the Fjoin has already been initialized for
 *						  the current target list evaluation
 *		nNodes			- The number of Iter nodes returning sets that the
 *						  node will flatten
 *		outerList		- 1 or more Iter nodes
 *		inner			- exactly one Iter node.  We eval every node in the
 *						  outerList once then eval the inner node to completion
 *						  pair the outerList result vector with each inner
 *						  result to form the full result.  When the inner has
 *						  been exhausted, we get the next outer result vector
 *						  and reset the inner.
 *		results			- The complete (flattened) result vector
 *		alwaysNull		- a null vector to indicate sets with a cardinality of
 *						  0, we treat them as the set {NULL}.
93
 */
94 95
typedef struct Fjoin
{
96 97 98 99 100 101
	NodeTag		type;
	bool		fj_initialized;
	int			fj_nNodes;
	List	   *fj_innerNode;
	DatumPtr	fj_results;
	BoolPtr		fj_alwaysDone;
102
} Fjoin;
103

104 105 106 107 108 109

/* ----------------------------------------------------------------
 *					node types for executable expressions
 * ----------------------------------------------------------------
 */

110 111
/* ----------------
 * Expr
112 113
 *		typeOid			- oid of the type of this expression
 *		opType			- type of this expression
114
 *		oper			- operator node if needed (Oper, Func, or SubPlan)
115
 *		args			- arguments to this expression
116 117
 * ----------------
 */
118 119
typedef enum OpType
{
120
	OP_EXPR, FUNC_EXPR, OR_EXPR, AND_EXPR, NOT_EXPR, SUBPLAN_EXPR
121
} OpType;
122

123 124
typedef struct Expr
{
125 126 127
	NodeTag		type;
	Oid			typeOid;		/* oid of the type of this expr */
	OpType		opType;			/* type of the op */
128
	Node	   *oper;			/* could be Oper or Func or SubPlan */
129
	List	   *args;			/* list of argument nodes */
130
} Expr;
131 132 133

/* ----------------
 * Var
134
 *		varno			- index of this var's relation in the range table
135
 *						  (could also be INNER or OUTER)
136
 *		varattno		- attribute number of this var, or zero for all
137
 *		vartype			- pg_type tuple OID for the type of this var
138
 *		vartypmod		- pg_attribute typmod value
139 140 141 142 143 144 145 146 147 148 149 150 151
 *		varlevelsup		- for subquery variables referencing outer relations;
 *						  0 in a normal var, >0 means N levels up
 *		varnoold		- original value of varno
 *		varoattno		- original value of varattno
 *
 * Note: during parsing/planning, varnoold/varoattno are always just copies
 * of varno/varattno.  At the tail end of planning, Var nodes appearing in
 * upper-level plan nodes are reassigned to point to the outputs of their
 * subplans; for example, in a join node varno becomes INNER or OUTER and
 * varattno becomes the index of the proper element of that subplan's target
 * list.  But varnoold/varoattno continue to hold the original values.
 * The code doesn't really need varnoold/varoattno, but they are very useful
 * for debugging and interpreting completed plans, so we keep them around.
152 153
 * ----------------
 */
154 155
#define    INNER		65000
#define    OUTER		65001
156

157 158
#define    PRS2_OLD_VARNO			1
#define    PRS2_NEW_VARNO			2
159

160 161
typedef struct Var
{
162 163 164 165
	NodeTag		type;
	Index		varno;
	AttrNumber	varattno;
	Oid			vartype;
166
	int32		vartypmod;
167
	Index		varlevelsup;
168 169
	Index		varnoold;		/* mainly for debugging --- see above */
	AttrNumber	varoattno;
170
} Var;
171 172 173

/* ----------------
 * Oper
174
 *		opno			- PG_OPERATOR OID of the operator
175
 *		opid			- PG_PROC OID for the operator's underlying function
176
 *		opresulttype	- PG_TYPE OID of the operator's return value
177
 *		op_fcache		- runtime state while running the function
178 179 180
 *
 * ----
 * NOTE: in the good old days 'opno' used to be both (or either, or
181
 * neither) the pg_operator oid, and/or the pg_proc oid depending
182 183 184 185 186 187
 * on the postgres module in question (parser->pg_operator,
 * executor->pg_proc, planner->both), the mood of the programmer,
 * and the phase of the moon (rumors that it was also depending on the day
 * of the week are probably false). To make things even more postgres-like
 * (i.e. a mess) some comments were referring to 'opno' using the name
 * 'opid'. Anyway, now we have two separate fields, and of course that
188
 * immediately removes all bugs from the code...		[ sp :-) ].
189 190 191 192
 *
 * Note also that opid is not necessarily filled in immediately on creation
 * of the node.  The planner makes sure it is valid before passing the node
 * tree to the executor, but during parsing/planning opid is typically 0.
193 194
 * ----------------
 */
195 196
typedef struct Oper
{
197 198 199 200
	NodeTag		type;
	Oid			opno;
	Oid			opid;
	Oid			opresulttype;
201
	FunctionCachePtr op_fcache;
202
} Oper;
203 204 205 206


/* ----------------
 * Const
207 208 209 210 211 212 213 214 215 216 217 218
 *		consttype - PG_TYPE OID of the constant's value
 *		constlen - length in bytes of the constant's value
 *		constvalue - the constant's value
 *		constisnull - whether the constant is null
 *				(if true, the other fields are undefined)
 *		constbyval - whether the information in constvalue
 *				if passed by value.  If true, then all the information
 *				is stored in the datum. If false, then the datum
 *				contains a pointer to the information.
 *		constisset - whether the const represents a set.  The const
 *				value corresponding will be the query that defines
 *				the set.
219 220
 * ----------------
 */
221 222
typedef struct Const
{
223 224
	NodeTag		type;
	Oid			consttype;
225
	int			constlen;
226 227 228 229 230
	Datum		constvalue;
	bool		constisnull;
	bool		constbyval;
	bool		constisset;
	bool		constiscast;
231
} Const;
232 233 234

/* ----------------
 * Param
235 236
 *		paramkind - specifies the kind of parameter. The possible values
 *		for this field are specified in "params.h", and they are:
237
 *
238 239 240
 *		PARAM_NAMED: The parameter has a name, i.e. something
 *				like `$.salary' or `$.foobar'.
 *				In this case field `paramname' must be a valid Name.
241
 *
242 243 244
 *		PARAM_NUM:	 The parameter has only a numeric identifier,
 *				i.e. something like `$1', `$2' etc.
 *				The number is contained in the `paramid' field.
245
 *
246 247 248 249
 *		PARAM_NEW:	 Used in PRS2 rule, similar to PARAM_NAMED.
 *					 The `paramname' and `paramid' refer to the "NEW" tuple
 *					 The `pramname' is the attribute name and `paramid'
 *					 is the attribute number.
250
 *
251 252
 *		PARAM_OLD:	 Same as PARAM_NEW, but in this case we refer to
 *				the "OLD" tuple.
253
 *
254 255 256
 *		paramid - numeric identifier for literal-constant parameters ("$1")
 *		paramname - attribute name for tuple-substitution parameters ("$.foo")
 *		paramtype - PG_TYPE OID of the parameter's value
257 258
 * ----------------
 */
259 260
typedef struct Param
{
261 262 263 264 265
	NodeTag		type;
	int			paramkind;
	AttrNumber	paramid;
	char	   *paramname;
	Oid			paramtype;
266
} Param;
267 268 269 270


/* ----------------
 * Func
271
 *		funcid			- PG_PROC OID of the function
272 273 274 275 276
 *		functype		- PG_TYPE OID of the function's return value
 *		func_fcache		- runtime state while running this function.  Where
 *						  we are in the execution of the function if it
 *						  returns more than one value, etc.
 *						  See utils/fcache.h
277 278
 * ----------------
 */
279 280
typedef struct Func
{
281 282 283
	NodeTag		type;
	Oid			funcid;
	Oid			functype;
284
	FunctionCachePtr func_fcache;
285
} Func;
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300
/* ----------------
 * Iter
 *		can anyone explain what this is for?  Seems to have something to do
 *		with evaluation of functions that return sets...
 * ----------------
 */
typedef struct Iter
{
	NodeTag		type;
	Node	   *iterexpr;
	Oid			itertype;		/* type of the iter expr (use for type
								 * checking) */
} Iter;

301
/* ----------------
B
Bruce Momjian 已提交
302
 * Aggref
303
 *		aggname			- name of the aggregate
304
 *		basetype		- base type Oid of the aggregate (ie, input type)
305
 *		aggtype			- type Oid of final result of the aggregate
306
 *		target			- attribute or expression we are aggregating on
307
 *		aggstar			- TRUE if argument was really '*'
308 309
 *		aggdistinct		- TRUE if it's agg(DISTINCT ...)
 *		aggno			- workspace for executor (see nodeAgg.c)
310 311
 * ----------------
 */
B
Bruce Momjian 已提交
312
typedef struct Aggref
313
{
314 315
	NodeTag		type;
	char	   *aggname;
316 317
	Oid			basetype;
	Oid			aggtype;
318
	Node	   *target;
319 320
	bool		aggstar;
	bool		aggdistinct;
321
	int			aggno;
322
} Aggref;
323

324 325
/* ----------------
 * SubLink
326 327
 *		subLinkType		- EXISTS, ALL, ANY, MULTIEXPR, EXPR
 *		useor			- TRUE to combine column results with "OR" not "AND"
328
 *		lefthand		- list of outer-query expressions on the left
329
 *		oper			- list of Oper nodes for combining operators
330
 *		subselect		- subselect as Query* or parsetree
331
 *
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
 * indicates the form of the expression represented:
 *	EXISTS_SUBLINK		EXISTS(SELECT ...)
 *	ALL_SUBLINK			(lefthand) op ALL (SELECT ...)
 *	ANY_SUBLINK			(lefthand) op ANY (SELECT ...)
 *	MULTIEXPR_SUBLINK	(lefthand) op (SELECT ...)
 *	EXPR_SUBLINK		(SELECT with single targetlist item ...)
 * For ALL, ANY, and MULTIEXPR, the lefthand is a list of expressions of the
 * same length as the subselect's targetlist.  MULTIEXPR will *always* have
 * a list with more than one entry; if the subselect has just one target
 * then the parser will create an EXPR_SUBLINK instead (and any operator
 * above the subselect will be represented separately).  Note that both
 * MULTIEXPR and EXPR require the subselect to deliver only one row.
 * ALL, ANY, and MULTIEXPR require the combining operators to deliver boolean
 * results.  These are reduced to one result per row using OR or AND semantics
 * depending on the "useor" flag.  ALL and ANY combine the per-row results
 * using AND and OR semantics respectively.
 *
351 352 353
 * NOTE: lefthand and oper have varying meanings depending on where you look
 * in the parse/plan pipeline:
 * 1. gram.y delivers a list of the (untransformed) lefthand expressions in
354 355
 *	  lefthand, and sets oper to a single A_Expr (not a list!) containing
 *	  the string name of the operator, but no arguments.
356
 * 2. The parser's expression transformation transforms lefthand normally,
357 358 359 360 361 362
 *	  and replaces oper with a list of Oper nodes, one per lefthand
 *	  expression.  These nodes represent the parser's resolution of exactly
 *	  which operator to apply to each pair of lefthand and targetlist
 *	  expressions.	However, we have not constructed actual Expr trees for
 *	  these operators yet.	This is the representation seen in saved rules
 *	  and in the rewriter.
363
 * 3. Finally, the planner converts the oper list to a list of normal Expr
364 365 366 367 368
 *	  nodes representing the application of the operator(s) to the lefthand
 *	  expressions and values from the inner targetlist.  The inner
 *	  targetlist items are represented by placeholder Param or Const nodes.
 *	  The lefthand field is set to NIL, since its expressions are now in
 *	  the Expr list.  This representation is passed to the executor.
369 370 371 372 373 374
 *
 * Planner routines that might see either representation 2 or 3 can tell
 * the difference by checking whether lefthand is NIL or not.  Also,
 * representation 2 appears in a "bare" SubLink, while representation 3 is
 * 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.
377 378 379 380
 * ----------------
 */
typedef enum SubLinkType
{
381
	EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, MULTIEXPR_SUBLINK, EXPR_SUBLINK
382 383 384 385 386 387
} SubLinkType;


typedef struct SubLink
{
	NodeTag		type;
388
	SubLinkType subLinkType;
389
	bool		useor;
390 391 392
	List	   *lefthand;
	List	   *oper;
	Node	   *subselect;
393 394
} SubLink;

395
/* ----------------
396 397 398 399 400 401
 *	ArrayRef: describes an array subscripting operation
 *
 * An ArrayRef can describe fetching a single element from an array,
 * fetching a subarray (array slice), storing a single element into
 * an array, or storing a slice.  The "store" cases work with an
 * initial array value and a source value that is inserted into the
402 403
 * appropriate part of the array; the result of the operation is an
 * entire new modified array value.
404
 *
405 406 407
 *		refattrlength	- typlen of array type
 *		refelemtype		- type of the result of the ArrayRef operation
 *		refelemlength	- typlen of the array element type
408 409
 *		refelembyval	- is the element type pass-by-value?
 *		refupperindexpr - expressions that evaluate to upper array indexes
410
 *		reflowerindexpr - expressions that evaluate to lower array indexes
411 412 413 414
 *		refexpr			- the expression that evaluates to an array value
 *		refassgnexpr	- expression for the source value, or NULL if fetch
 *
 * If reflowerindexpr = NIL, then we are fetching or storing a single array
415
 * element at the subscripts given by refupperindexpr.	Otherwise we are
416 417 418 419 420 421 422
 * fetching or storing an array slice, that is a rectangular subarray
 * with lower and upper bounds given by the index expressions.
 * reflowerindexpr must be the same length as refupperindexpr when it
 * is not NIL.
 *
 * Note: array types can be fixed-length (refattrlength > 0), but only
 * when the element type is itself fixed-length.  Otherwise they are
423
 * varlena structures and have refattrlength = -1.	In any case,
424 425 426 427 428
 * an array type is never pass-by-value.
 *
 * Note: currently, refelemtype is NOT the element type, but the array type,
 * when doing subarray fetch or either type of store.  It would be cleaner
 * to add more fields so we can distinguish the array element type from the
429
 * result type of the ArrayRef operator...
430 431
 * ----------------
 */
432 433
typedef struct ArrayRef
{
434 435 436 437 438 439 440 441 442
	NodeTag		type;
	int			refattrlength;
	int			refelemlength;
	Oid			refelemtype;
	bool		refelembyval;
	List	   *refupperindexpr;
	List	   *reflowerindexpr;
	Node	   *refexpr;
	Node	   *refassgnexpr;
B
Bruce Momjian 已提交
443
} ArrayRef;
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
/* ----------------
 * FieldSelect
 *		arg				- input expression
 *		fieldnum		- attribute number of field to extract
 *		resulttype		- type of the field (result type of this node)
 *		resulttypmod	- output typmod (usually -1)
 *
 * FieldSelect represents the operation of extracting one field from a tuple
 * value.  At runtime, the input expression is expected to yield a Datum
 * that contains a pointer-to-TupleTableSlot.  The specified field number
 * is extracted and returned as a Datum.
 * ----------------
 */

typedef struct FieldSelect
{
	NodeTag		type;
	Node	   *arg;
	AttrNumber	fieldnum;
	Oid			resulttype;
	int32		resulttypmod;
} FieldSelect;

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
/* ----------------
 * RelabelType
 *		arg				- input expression
 *		resulttype		- output type of coercion expression
 *		resulttypmod	- output typmod (usually -1)
 *
 * RelabelType represents a "dummy" type coercion between two binary-
 * compatible datatypes, such as reinterpreting the result of an OID
 * expression as an int4.  It is a no-op at runtime; we only need it
 * to provide a place to store the correct type to be attributed to
 * the expression result during type resolution.  (We can't get away
 * with just overwriting the type field of the input expression node,
 * so we need a separate node to show the coercion's result type.)
 * ----------------
 */

typedef struct RelabelType
{
	NodeTag		type;
	Node	   *arg;
	Oid			resulttype;
	int32		resulttypmod;
} RelabelType;

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

/* ----------------------------------------------------------------
 *					node types for join trees
 *
 * The leaves of a join tree structure are RangeTblRef nodes.  Above
 * these, JoinExpr nodes can appear to denote a specific kind of join
 * or qualified join.  A join tree can also contain List nodes --- a list
 * implies an unqualified cross-product join of its members.  The planner
 * is allowed to combine the elements of a list using whatever join order
 * seems good to it.  At present, JoinExpr nodes are always joined in
 * exactly the order implied by the tree structure (except the planner
 * may choose to swap inner and outer members of a join pair).
 *
 * NOTE: currently, the planner only supports a List at the top level of
 * a join tree.  Should generalize this to allow Lists at lower levels.
 *
 * NOTE: the qualification expressions present in JoinExpr nodes are
 * *in addition to* the query's main WHERE clause.  For outer joins there
 * is a real semantic difference between a join qual and a WHERE clause,
 * though if all joins are inner joins they are interchangeable.
 *
 * NOTE: in the raw output of gram.y, a join tree contains RangeVar and
 * RangeSubselect nodes, which are both replaced by RangeTblRef nodes
 * during the parse analysis phase.
 * ----------------------------------------------------------------
 */

/*
 * RangeTblRef - reference to an entry in the query's rangetable
 *
 * We could use direct pointers to the RT entries and skip having these
 * nodes, but multiple pointers to the same node in a querytree cause
 * lots of headaches, so it seems better to store an index into the RT.
 */
typedef struct RangeTblRef
{
	NodeTag		type;
	int			rtindex;
} RangeTblRef;

/*----------
 * JoinExpr - for SQL JOIN expressions
 *
 * isNatural, using, and quals are interdependent.  The user can write only
 * one of NATURAL, USING(), or ON() (this is enforced by the grammar).
 * If he writes NATURAL then parse analysis generates the equivalent USING()
 * list, and from that fills in "quals" with the right equality comparisons.
 * If he writes USING() then "quals" is filled with equality comparisons.
 * If he writes ON() then only "quals" is set.  Note that NATURAL/USING
 * are not equivalent to ON() since they also affect the output column list.
 *
 * alias is an Attr node representing the AS alias-clause attached to the
 * join expression, or NULL if no clause.  During parse analysis, colnames
 * is filled with a list of String nodes giving the column names (real or
 * alias) of the output of the join, and colvars is filled with a list of
 * expressions that can be copied to reference the output columns.
 *----------
 */
typedef struct JoinExpr
{
	NodeTag		type;
	JoinType	jointype;		/* type of join */
	bool		isNatural;		/* Natural join? Will need to shape table */
	Node	   *larg;			/* left subtree */
	Node	   *rarg;			/* right subtree */
	List	   *using;			/* USING clause, if any (list of String) */
	Node	   *quals;			/* qualifiers on join, if any */
	struct Attr *alias;			/* user-written alias clause, if any */
	List	   *colnames;		/* output column names (list of String) */
	List	   *colvars;		/* output column nodes (list of expressions) */
} JoinExpr;

564
#endif	 /* PRIMNODES_H */