primnodes.h 16.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * primnodes.h
4
 *	  Definitions for parse tree/query tree ("primitive") nodes.
5 6
 *
 *
B
Add:  
Bruce Momjian 已提交
7 8
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $Id: primnodes.h,v 1.46 2000/08/08 15:42:59 tgl Exp $
11 12 13 14
 *
 *-------------------------------------------------------------------------
 */
#ifndef PRIMNODES_H
15
#define PRIMNODES_H
16

17 18
#include "access/attnum.h"
#include "nodes/pg_list.h"
B
Bruce Momjian 已提交
19
#include "utils/fcache.h"
20 21

/* ----------------------------------------------------------------
22
 *						node definitions
23 24 25 26 27
 * ----------------------------------------------------------------
 */

/* ----------------
 * Resdom (Result Domain)
28
 *		resno			- attribute number
29 30
 *		restype			- type of the value
 *		restypmod		- type-specific modifier of the value
31
 *		resname			- name of the resdom (could be NULL)
32
 *		ressortgroupref - nonzero if referenced by a sort/group clause
33
 *		reskey			- order of key in a sort (for those > 0)
34
 *		reskeyop		- sort operator's regproc Oid
B
Bruce Momjian 已提交
35 36
 *		resjunk			- set to true to eliminate the attribute
 *						  from final target list
37
 *
38 39
 * Notes:
 * ressortgroupref is the parse/plan-time representation of ORDER BY and
40
 * GROUP BY items.	Targetlist entries with ressortgroupref=0 are not
41
 * sort/group items.  If ressortgroupref>0, then this item is an ORDER BY or
42
 * GROUP BY value.	No two entries in a targetlist may have the same nonzero
43
 * ressortgroupref --- but there is no particular meaning to the nonzero
44 45
 * values, except as tags.	(For example, one must not assume that lower
 * ressortgroupref means a more significant sort key.)	The order of the
46 47 48 49 50 51 52 53 54 55
 * 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.
56 57
 * ----------------
 */
58 59
typedef struct Resdom
{
60 61 62
	NodeTag		type;
	AttrNumber	resno;
	Oid			restype;
63
	int32		restypmod;
64
	char	   *resname;
65
	Index		ressortgroupref;
66 67
	Index		reskey;
	Oid			reskeyop;
B
Bruce Momjian 已提交
68
	bool		resjunk;
69
} Resdom;
70 71 72

/* -------------
 * Fjoin
73 74 75 76 77 78 79 80 81 82 83 84 85 86
 *		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}.
87
 */
88 89
typedef struct Fjoin
{
90 91 92 93 94 95
	NodeTag		type;
	bool		fj_initialized;
	int			fj_nNodes;
	List	   *fj_innerNode;
	DatumPtr	fj_results;
	BoolPtr		fj_alwaysDone;
96
} Fjoin;
97 98 99

/* ----------------
 * Expr
100 101
 *		typeOid			- oid of the type of this expression
 *		opType			- type of this expression
102
 *		oper			- operator node if needed (Oper, Func, or SubPlan)
103
 *		args			- arguments to this expression
104 105
 * ----------------
 */
106 107
typedef enum OpType
{
108
	OP_EXPR, FUNC_EXPR, OR_EXPR, AND_EXPR, NOT_EXPR, SUBPLAN_EXPR
109
} OpType;
110

111 112
typedef struct Expr
{
113 114 115
	NodeTag		type;
	Oid			typeOid;		/* oid of the type of this expr */
	OpType		opType;			/* type of the op */
116
	Node	   *oper;			/* could be Oper or Func or SubPlan */
117
	List	   *args;			/* list of argument nodes */
118
} Expr;
119 120 121

/* ----------------
 * Var
122
 *		varno			- index of this var's relation in the range table
123
 *						  (could also be INNER or OUTER)
124
 *		varattno		- attribute number of this var, or zero for all
125
 *		vartype			- pg_type tuple OID for the type of this var
126
 *		vartypmod		- pg_attribute typmod value
127 128 129 130 131 132 133 134 135 136 137 138 139
 *		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.
140 141
 * ----------------
 */
142 143
#define    INNER		65000
#define    OUTER		65001
144

145 146
#define    PRS2_OLD_VARNO			1
#define    PRS2_NEW_VARNO			2
147

148 149
typedef struct Var
{
150 151 152 153
	NodeTag		type;
	Index		varno;
	AttrNumber	varattno;
	Oid			vartype;
154
	int32		vartypmod;
155
	Index		varlevelsup;	/* erased by upper optimizer */
156 157
	Index		varnoold;		/* mainly for debugging --- see above */
	AttrNumber	varoattno;
158
} Var;
159 160 161

/* ----------------
 * Oper
162
 *		opno			- PG_OPERATOR OID of the operator
163
 *		opid			- PG_PROC OID for the operator's underlying function
164
 *		opresulttype	- PG_TYPE OID of the operator's return value
165
 *		op_fcache		- runtime state while running the function
166 167 168
 *
 * ----
 * NOTE: in the good old days 'opno' used to be both (or either, or
169
 * neither) the pg_operator oid, and/or the pg_proc oid depending
170 171 172 173 174 175
 * 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
176
 * immediately removes all bugs from the code...		[ sp :-) ].
177 178 179 180
 *
 * 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.
181 182
 * ----------------
 */
183 184
typedef struct Oper
{
185 186 187 188
	NodeTag		type;
	Oid			opno;
	Oid			opid;
	Oid			opresulttype;
189
	FunctionCachePtr op_fcache;
190
} Oper;
191 192 193 194


/* ----------------
 * Const
195 196 197 198 199 200 201 202 203 204 205 206
 *		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.
207 208
 * ----------------
 */
209 210
typedef struct Const
{
211 212
	NodeTag		type;
	Oid			consttype;
213
	int			constlen;
214 215 216 217 218
	Datum		constvalue;
	bool		constisnull;
	bool		constbyval;
	bool		constisset;
	bool		constiscast;
219
} Const;
220 221 222

/* ----------------
 * Param
223 224
 *		paramkind - specifies the kind of parameter. The possible values
 *		for this field are specified in "params.h", and they are:
225
 *
226 227 228
 *		PARAM_NAMED: The parameter has a name, i.e. something
 *				like `$.salary' or `$.foobar'.
 *				In this case field `paramname' must be a valid Name.
229
 *
230 231 232
 *		PARAM_NUM:	 The parameter has only a numeric identifier,
 *				i.e. something like `$1', `$2' etc.
 *				The number is contained in the `paramid' field.
233
 *
234 235 236 237
 *		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.
238
 *
239 240
 *		PARAM_OLD:	 Same as PARAM_NEW, but in this case we refer to
 *				the "OLD" tuple.
241
 *
242 243 244
 *		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
245 246
 * ----------------
 */
247 248
typedef struct Param
{
249 250 251 252 253
	NodeTag		type;
	int			paramkind;
	AttrNumber	paramid;
	char	   *paramname;
	Oid			paramtype;
254
} Param;
255 256 257 258


/* ----------------
 * Func
259
 *		funcid			- PG_PROC OID of the function
260 261 262 263 264
 *		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
265 266
 * ----------------
 */
267 268
typedef struct Func
{
269 270 271
	NodeTag		type;
	Oid			funcid;
	Oid			functype;
272
	FunctionCachePtr func_fcache;
273
} Func;
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288
/* ----------------
 * 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;

289
/* ----------------
B
Bruce Momjian 已提交
290
 * Aggref
291
 *		aggname			- name of the aggregate
292
 *		basetype		- base type Oid of the aggregate (ie, input type)
293
 *		aggtype			- type Oid of final result of the aggregate
294
 *		target			- attribute or expression we are aggregating on
295
 *		aggstar			- TRUE if argument was really '*'
296 297
 *		aggdistinct		- TRUE if it's agg(DISTINCT ...)
 *		aggno			- workspace for executor (see nodeAgg.c)
298 299
 * ----------------
 */
B
Bruce Momjian 已提交
300
typedef struct Aggref
301
{
302 303
	NodeTag		type;
	char	   *aggname;
304 305
	Oid			basetype;
	Oid			aggtype;
306
	Node	   *target;
307 308
	bool		aggstar;
	bool		aggdistinct;
309
	int			aggno;
310
} Aggref;
311

312 313
/* ----------------
 * SubLink
314 315
 *		subLinkType		- EXISTS, ALL, ANY, MULTIEXPR, EXPR
 *		useor			- TRUE to combine column results with "OR" not "AND"
316
 *		lefthand		- list of outer-query expressions on the left
317
 *		oper			- list of Oper nodes for combining operators
318
 *		subselect		- subselect as Query* or parsetree
319
 *
320
 * A SubLink represents a subselect appearing in an expression, and in some
321
 * cases also the combining operator(s) just above it.	The subLinkType
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
 * 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.
 *
339 340 341
 * 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
342 343
 *	  lefthand, and sets oper to a single A_Expr (not a list!) containing
 *	  the string name of the operator, but no arguments.
344
 * 2. The parser's expression transformation transforms lefthand normally,
345 346 347 348 349 350
 *	  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.
351
 * 3. Finally, the planner converts the oper list to a list of normal Expr
352 353 354 355 356
 *	  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.
357 358 359 360 361 362
 *
 * 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.
 *
363
 * In EXISTS and EXPR SubLinks, both lefthand and oper are unused and are
364
 * always NIL.	useor is not significant either for these sublink types.
365 366 367 368
 * ----------------
 */
typedef enum SubLinkType
{
369
	EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, MULTIEXPR_SUBLINK, EXPR_SUBLINK
370 371 372 373 374 375
} SubLinkType;


typedef struct SubLink
{
	NodeTag		type;
376
	SubLinkType subLinkType;
377
	bool		useor;
378 379 380
	List	   *lefthand;
	List	   *oper;
	Node	   *subselect;
381 382
} SubLink;

383
/* ----------------
384 385 386 387 388 389
 *	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
390 391
 * appropriate part of the array; the result of the operation is an
 * entire new modified array value.
392
 *
393 394 395
 *		refattrlength	- typlen of array type
 *		refelemtype		- type of the result of the ArrayRef operation
 *		refelemlength	- typlen of the array element type
396 397
 *		refelembyval	- is the element type pass-by-value?
 *		refupperindexpr - expressions that evaluate to upper array indexes
398
 *		reflowerindexpr - expressions that evaluate to lower array indexes
399 400 401 402
 *		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
403
 * element at the subscripts given by refupperindexpr.	Otherwise we are
404 405 406 407 408 409 410
 * 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
411
 * varlena structures and have refattrlength = -1.	In any case,
412 413 414 415 416
 * 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
417
 * result type of the ArrayRef operator...
418 419
 * ----------------
 */
420 421
typedef struct ArrayRef
{
422 423 424 425 426 427 428 429 430
	NodeTag		type;
	int			refattrlength;
	int			refelemlength;
	Oid			refelemtype;
	bool		refelembyval;
	List	   *refupperindexpr;
	List	   *reflowerindexpr;
	Node	   *refexpr;
	Node	   *refassgnexpr;
B
Bruce Momjian 已提交
431
} ArrayRef;
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
/* ----------------
 * 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;

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/* ----------------
 * 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;

480
#endif	 /* PRIMNODES_H */