primnodes.h 49.5 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
 *
 *
10
 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
11
 * Portions Copyright (c) 1994, Regents of the University of California
12
 *
13
 * src/include/nodes/primnodes.h
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
 *						node definitions
26 27 28
 * ----------------------------------------------------------------
 */

29 30 31 32
/*
 * Alias -
 *	  specifies an alias for a range variable; the alias might also
 *	  specify renaming of columns within the table.
33
 *
34 35
 * Note: colnames is a list of Value nodes (always strings).  In Alias structs
 * associated with RTEs, there may be entries corresponding to dropped
B
Bruce Momjian 已提交
36
 * columns; these are normally empty strings ("").	See parsenodes.h for info.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 */
typedef struct Alias
{
	NodeTag		type;
	char	   *aliasname;		/* aliased rel name (never qualified) */
	List	   *colnames;		/* optional list of column aliases */
} Alias;

typedef enum InhOption
{
	INH_NO,						/* Do NOT scan child tables */
	INH_YES,					/* DO scan child tables */
	INH_DEFAULT					/* Use current SQL_inheritance option */
} InhOption;

52 53 54 55 56 57 58 59 60
/* What to do at commit time for temporary relations */
typedef enum OnCommitAction
{
	ONCOMMIT_NOOP,				/* No ON COMMIT clause (do nothing) */
	ONCOMMIT_PRESERVE_ROWS,		/* ON COMMIT PRESERVE ROWS (do nothing) */
	ONCOMMIT_DELETE_ROWS,		/* ON COMMIT DELETE ROWS */
	ONCOMMIT_DROP				/* ON COMMIT DROP */
} OnCommitAction;

61 62 63 64 65 66 67 68 69 70 71 72 73 74
/*
 * RangeVar - range variable, used in FROM clauses
 *
 * Also used to represent table names in utility statements; there, the alias
 * field is not used, and inhOpt shows whether to apply the operation
 * recursively to child tables.  In some contexts it is also useful to carry
 * a TEMP table indication here.
 */
typedef struct RangeVar
{
	NodeTag		type;
	char	   *catalogname;	/* the catalog (database) name, or NULL */
	char	   *schemaname;		/* the schema name, or NULL */
	char	   *relname;		/* the relation/sequence name */
B
Bruce Momjian 已提交
75 76
	InhOption	inhOpt;			/* expand rel by inheritance? recursively act
								 * on children? */
77
	char		relpersistence; /* see RELPERSISTENCE_* in pg_class.h */
78
	Alias	   *alias;			/* table alias & optional column aliases */
79
	int			location;		/* token location, or -1 if unknown */
80 81
} RangeVar;

82 83 84 85 86 87 88 89 90 91 92
/*
 * IntoClause - target information for SELECT INTO and CREATE TABLE AS
 */
typedef struct IntoClause
{
	NodeTag		type;

	RangeVar   *rel;			/* target relation name */
	List	   *colNames;		/* column names to assign, or NIL */
	List	   *options;		/* options from WITH clause */
	OnCommitAction onCommit;	/* what do we do at COMMIT? */
B
Bruce Momjian 已提交
93
	char	   *tableSpaceName; /* table space to use, or NULL */
94
	bool		skipData;		/* true for WITH NO DATA */
95
} IntoClause;
96

97

98 99 100 101 102
/* ----------------------------------------------------------------
 *					node types for executable expressions
 * ----------------------------------------------------------------
 */

103
/*
104
 * Expr - generic superclass for executable-expression nodes
105
 *
106 107 108 109
 * All node types that are used in executable expression trees should derive
 * from Expr (that is, have Expr as their first field).  Since Expr only
 * contains NodeTag, this is a formality, but it is an easy form of
 * documentation.  See also the ExprState node types in execnodes.h.
110
 */
111 112
typedef struct Expr
{
113
	NodeTag		type;
114
} Expr;
115

116
/*
117
 * Var - expression node representing a variable (ie, a table column)
118 119 120 121
 *
 * 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
122 123 124
 * subplans; for example, in a join node varno becomes INNER_VAR or OUTER_VAR
 * and varattno becomes the index of the proper element of that subplan's
 * target list.  But varnoold/varoattno continue to hold the original values.
125 126
 * The code doesn't really need varnoold/varoattno, but they are very useful
 * for debugging and interpreting completed plans, so we keep them around.
127
 */
128 129 130
#define    INNER_VAR		65000		/* reference to inner subplan */
#define    OUTER_VAR		65001		/* reference to outer subplan */
#define    INDEX_VAR		65002		/* reference to index column */
131

132 133 134
#define IS_SPECIAL_VARNO(varno)		((varno) >= INNER_VAR)

/* Symbols for the indexes of the special RTE entries in rules */
135 136
#define    PRS2_OLD_VARNO			1
#define    PRS2_NEW_VARNO			2
137

138 139
typedef struct Var
{
140
	Expr		xpr;
B
Bruce Momjian 已提交
141
	Index		varno;			/* index of this var's relation in the range
142
								 * table, or INNER_VAR/OUTER_VAR/INDEX_VAR */
B
Bruce Momjian 已提交
143 144
	AttrNumber	varattno;		/* attribute number of this var, or zero for
								 * all */
145
	Oid			vartype;		/* pg_type OID for the type of this var */
B
Bruce Momjian 已提交
146
	int32		vartypmod;		/* pg_attribute typmod value */
147
	Oid			varcollid;		/* OID of collation, or InvalidOid if none */
148 149 150
	Index		varlevelsup;	/* for subquery variables referencing outer
								 * relations; 0 in a normal var, >0 means N
								 * levels up */
B
Bruce Momjian 已提交
151 152
	Index		varnoold;		/* original value of varno, for debugging */
	AttrNumber	varoattno;		/* original value of varattno */
153
	int			location;		/* token location, or -1 if unknown */
154
} Var;
155

156
/*
157 158
 * Const
 */
159 160
typedef struct Const
{
161
	Expr		xpr;
162 163
	Oid			consttype;		/* pg_type OID of the constant's datatype */
	int32		consttypmod;	/* typmod value, if any */
164
	Oid			constcollid;	/* OID of collation, or InvalidOid if none */
165
	int			constlen;		/* typlen of the constant's datatype */
166 167
	Datum		constvalue;		/* the constant's value */
	bool		constisnull;	/* whether the constant is null (if true,
168
								 * constvalue is undefined) */
B
Bruce Momjian 已提交
169 170 171 172
	bool		constbyval;		/* whether this datatype is 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. */
173
	int			location;		/* token location, or -1 if unknown */
174
} Const;
175 176 177

/* ----------------
 * Param
178
 *		paramkind - specifies the kind of parameter. The possible values
179
 *		for this field are:
180
 *
181 182
 *		PARAM_EXTERN:  The parameter value is supplied from outside the plan.
 *				Such parameters are numbered from 1 to n.
183
 *
184
 *		PARAM_EXEC:  The parameter is an internal executor parameter, used
185 186
 *				for passing values into and out of sub-queries or from
 *				nestloop joins to their inner scans.
187 188
 *				For historical reasons, such parameters are numbered from 0.
 *				These numbers are independent of PARAM_EXTERN numbers.
189
 *
B
Bruce Momjian 已提交
190
 *		PARAM_SUBLINK:	The parameter represents an output column of a SubLink
191 192 193
 *				node's sub-select.  The column number is contained in the
 *				`paramid' field.  (This type of Param is converted to
 *				PARAM_EXEC during planning.)
194 195 196 197 198
 *
 * Note: currently, paramtypmod is valid for PARAM_SUBLINK Params, and for
 * PARAM_EXEC Params generated from them; it is always -1 for PARAM_EXTERN
 * params, since the APIs that supply values for such parameters don't carry
 * any typmod info.
199 200
 * ----------------
 */
201 202 203 204 205 206 207
typedef enum ParamKind
{
	PARAM_EXTERN,
	PARAM_EXEC,
	PARAM_SUBLINK
} ParamKind;

208 209
typedef struct Param
{
210
	Expr		xpr;
211 212
	ParamKind	paramkind;		/* kind of parameter. See above */
	int			paramid;		/* numeric ID for parameter */
213 214
	Oid			paramtype;		/* pg_type OID of parameter's datatype */
	int32		paramtypmod;	/* typmod value, if known */
215
	Oid			paramcollid;	/* OID of collation, or InvalidOid if none */
216
	int			location;		/* token location, or -1 if unknown */
217
} Param;
218

219
/*
B
Bruce Momjian 已提交
220
 * Aggref
221 222
 *
 * The aggregate's args list is a targetlist, ie, a list of TargetEntry nodes
B
Bruce Momjian 已提交
223
 * (before Postgres 9.0 it was just bare expressions).	The non-resjunk TLEs
224 225 226 227 228 229
 * represent the aggregate's regular arguments (if any) and resjunk TLEs can
 * be added at the end to represent ORDER BY expressions that are not also
 * arguments.  As in a top-level Query, the TLEs can be marked with
 * ressortgroupref indexes to let them be referenced by SortGroupClause
 * entries in the aggorder and/or aggdistinct lists.  This represents ORDER BY
 * and DISTINCT operations to be applied to the aggregate input rows before
B
Bruce Momjian 已提交
230
 * they are passed to the transition function.	The grammar only allows a
231 232
 * simple "DISTINCT" specifier for the arguments, but we use the full
 * query-level representation to allow more code sharing.
233
 */
B
Bruce Momjian 已提交
234
typedef struct Aggref
235
{
236
	Expr		xpr;
237 238
	Oid			aggfnoid;		/* pg_proc Oid of the aggregate */
	Oid			aggtype;		/* type Oid of result of the aggregate */
239 240
	Oid			aggcollid;		/* OID of collation of result */
	Oid			inputcollid;	/* OID of collation that function should use */
241 242 243
	List	   *args;			/* arguments and sort expressions */
	List	   *aggorder;		/* ORDER BY (list of SortGroupClause) */
	List	   *aggdistinct;	/* DISTINCT (list of SortGroupClause) */
244
	bool		aggstar;		/* TRUE if argument list was really '*' */
245
	Index		agglevelsup;	/* > 0 if agg belongs to outer query */
246
	int			location;		/* token location, or -1 if unknown */
247
} Aggref;
248

T
Tom Lane 已提交
249 250 251 252 253 254 255 256
/*
 * WindowFunc
 */
typedef struct WindowFunc
{
	Expr		xpr;
	Oid			winfnoid;		/* pg_proc Oid of the function */
	Oid			wintype;		/* type Oid of result of the window function */
257 258
	Oid			wincollid;		/* OID of collation of result */
	Oid			inputcollid;	/* OID of collation that function should use */
T
Tom Lane 已提交
259 260 261 262 263 264 265
	List	   *args;			/* arguments to the window function */
	Index		winref;			/* index of associated WindowClause */
	bool		winstar;		/* TRUE if argument list was really '*' */
	bool		winagg;			/* is function a simple aggregate? */
	int			location;		/* token location, or -1 if unknown */
} WindowFunc;

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/* ----------------
 *	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
 * appropriate part of the array; the result of the operation is an
 * entire new modified array value.
 *
 * If reflowerindexpr = NIL, then we are fetching or storing a single array
 * element at the subscripts given by refupperindexpr.	Otherwise we are
 * 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.
 *
283 284 285
 * Note: the result datatype is the element type when fetching a single
 * element; but it is the array type when doing subarray fetch or either
 * type of store.
286 287 288 289 290
 * ----------------
 */
typedef struct ArrayRef
{
	Expr		xpr;
291 292
	Oid			refarraytype;	/* type of the array proper */
	Oid			refelemtype;	/* type of the array elements */
293
	int32		reftypmod;		/* typmod of the array (and elements too) */
294
	Oid			refcollid;		/* OID of collation, or InvalidOid if none */
B
Bruce Momjian 已提交
295 296 297 298 299 300 301 302
	List	   *refupperindexpr;/* expressions that evaluate to upper array
								 * indexes */
	List	   *reflowerindexpr;/* expressions that evaluate to lower array
								 * indexes */
	Expr	   *refexpr;		/* the expression that evaluates to an array
								 * value */
	Expr	   *refassgnexpr;	/* expression for the source value, or NULL if
								 * fetch */
303 304 305 306 307 308 309 310 311 312 313 314 315
} ArrayRef;

/*
 * CoercionContext - distinguishes the allowed set of type casts
 *
 * NB: ordering of the alternatives is significant; later (larger) values
 * allow more casts than earlier ones.
 */
typedef enum CoercionContext
{
	COERCION_IMPLICIT,			/* coercion in context of expression */
	COERCION_ASSIGNMENT,		/* coercion in context of assignment */
	COERCION_EXPLICIT			/* explicit cast operation */
316
} CoercionContext;
317 318 319

/*
 * CoercionForm - information showing how to display a function-call node
320 321 322 323 324 325
 *
 * NB: equal() ignores CoercionForm fields, therefore this *must* not carry
 * any semantically significant information.  We need that behavior so that
 * the planner will consider equivalent implicit and explicit casts to be
 * equivalent.  In cases where those actually behave differently, the coercion
 * function's arguments will be different.
326 327 328 329 330 331
 */
typedef enum CoercionForm
{
	COERCE_EXPLICIT_CALL,		/* display as a function call */
	COERCE_EXPLICIT_CAST,		/* display as an explicit cast */
	COERCE_IMPLICIT_CAST,		/* implicit cast, so hide it */
332
	COERCE_DONTCARE				/* special case for planner */
333
} CoercionForm;
334 335 336 337 338 339 340 341 342 343 344

/*
 * FuncExpr - expression node for a function call
 */
typedef struct FuncExpr
{
	Expr		xpr;
	Oid			funcid;			/* PG_PROC OID of the function */
	Oid			funcresulttype; /* PG_TYPE OID of result value */
	bool		funcretset;		/* true if function returns set */
	CoercionForm funcformat;	/* how to display this function call */
345 346
	Oid			funccollid;		/* OID of collation of result */
	Oid			inputcollid;	/* OID of collation that function should use */
347
	List	   *args;			/* arguments to the function */
348
	int			location;		/* token location, or -1 if unknown */
349
} FuncExpr;
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
/*
 * NamedArgExpr - a named argument of a function
 *
 * This node type can only appear in the args list of a FuncCall or FuncExpr
 * node.  We support pure positional call notation (no named arguments),
 * named notation (all arguments are named), and mixed notation (unnamed
 * arguments followed by named ones).
 *
 * Parse analysis sets argnumber to the positional index of the argument,
 * but doesn't rearrange the argument list.
 *
 * The planner will convert argument lists to pure positional notation
 * during expression preprocessing, so execution never sees a NamedArgExpr.
 */
typedef struct NamedArgExpr
{
	Expr		xpr;
	Expr	   *arg;			/* the argument expression */
	char	   *name;			/* the name */
	int			argnumber;		/* argument's number in positional notation */
	int			location;		/* argument name location, or -1 if unknown */
} NamedArgExpr;

374 375 376 377 378 379 380
/*
 * OpExpr - expression node for an operator invocation
 *
 * Semantically, this is essentially the same as a function call.
 *
 * Note that opfuncid is not necessarily filled in immediately on creation
 * of the node.  The planner makes sure it is valid before passing the node
381
 * tree to the executor, but during parsing/planning opfuncid can be 0.
382 383 384 385 386 387 388 389
 */
typedef struct OpExpr
{
	Expr		xpr;
	Oid			opno;			/* PG_OPERATOR OID of the operator */
	Oid			opfuncid;		/* PG_PROC OID of underlying function */
	Oid			opresulttype;	/* PG_TYPE OID of result value */
	bool		opretset;		/* true if operator returns set */
390 391
	Oid			opcollid;		/* OID of collation of result */
	Oid			inputcollid;	/* OID of collation that operator should use */
392
	List	   *args;			/* arguments to the operator (1 or 2) */
393
	int			location;		/* token location, or -1 if unknown */
394
} OpExpr;
395 396 397 398 399 400 401 402 403 404 405 406 407

/*
 * DistinctExpr - expression node for "x IS DISTINCT FROM y"
 *
 * Except for the nodetag, this is represented identically to an OpExpr
 * referencing the "=" operator for x and y.
 * We use "=", not the more obvious "<>", because more datatypes have "="
 * than "<>".  This means the executor must invert the operator result.
 * Note that the operator function won't be called at all if either input
 * is NULL, since then the result can be determined directly.
 */
typedef OpExpr DistinctExpr;

408 409 410 411 412 413 414 415
/*
 * NullIfExpr - a NULLIF expression
 *
 * Like DistinctExpr, this is represented the same as an OpExpr referencing
 * the "=" operator for x and y.
 */
typedef OpExpr NullIfExpr;

416 417 418 419 420 421 422 423
/*
 * ScalarArrayOpExpr - expression node for "scalar op ANY/ALL (array)"
 *
 * The operator must yield boolean.  It is applied to the left operand
 * and each element of the righthand array, and the results are combined
 * with OR or AND (for ANY or ALL respectively).  The node representation
 * is almost the same as for the underlying operator, but we need a useOr
 * flag to remember whether it's ANY or ALL, and we don't have to store
424
 * the result type (or the collation) because it must be boolean.
425 426 427 428 429 430 431
 */
typedef struct ScalarArrayOpExpr
{
	Expr		xpr;
	Oid			opno;			/* PG_OPERATOR OID of the operator */
	Oid			opfuncid;		/* PG_PROC OID of underlying function */
	bool		useOr;			/* true for ANY, false for ALL */
432
	Oid			inputcollid;	/* OID of collation that operator should use */
433
	List	   *args;			/* the scalar and array operands */
434
	int			location;		/* token location, or -1 if unknown */
435
} ScalarArrayOpExpr;
436

437 438 439 440 441
/*
 * BoolExpr - expression node for the basic Boolean operators AND, OR, NOT
 *
 * Notice the arguments are given as a List.  For NOT, of course the list
 * must always have exactly one element.  For AND and OR, the executor can
442
 * handle any number of arguments.	The parser generally treats AND and OR
443 444 445 446
 * as binary and so it typically only produces two-element lists, but the
 * optimizer will flatten trees of AND and OR nodes to produce longer lists
 * when possible.  There are also a few special cases where more arguments
 * can appear before optimization.
447 448 449 450
 */
typedef enum BoolExprType
{
	AND_EXPR, OR_EXPR, NOT_EXPR
451
} BoolExprType;
452 453 454 455 456 457

typedef struct BoolExpr
{
	Expr		xpr;
	BoolExprType boolop;
	List	   *args;			/* arguments to this expression */
458
	int			location;		/* token location, or -1 if unknown */
459
} BoolExpr;
460

461
/*
462
 * SubLink
463
 *
B
Bruce Momjian 已提交
464 465 466
 * A SubLink represents a subselect appearing in an expression, and in some
 * cases also the combining operator(s) just above it.	The subLinkType
 * indicates the form of the expression represented:
467 468 469
 *	EXISTS_SUBLINK		EXISTS(SELECT ...)
 *	ALL_SUBLINK			(lefthand) op ALL (SELECT ...)
 *	ANY_SUBLINK			(lefthand) op ANY (SELECT ...)
470
 *	ROWCOMPARE_SUBLINK	(lefthand) op (SELECT ...)
471
 *	EXPR_SUBLINK		(SELECT with single targetlist item ...)
472
 *	ARRAY_SUBLINK		ARRAY(SELECT with single targetlist item ...)
473
 *	CTE_SUBLINK			WITH query (never actually part of an expression)
474 475
 * For ALL, ANY, and ROWCOMPARE, the lefthand is a list of expressions of the
 * same length as the subselect's targetlist.  ROWCOMPARE will *always* have
476 477 478
 * 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
479 480 481 482
 * ROWCOMPARE and EXPR require the subselect to deliver only one row.
 * ALL, ANY, and ROWCOMPARE require the combining operators to deliver boolean
 * results.  ALL and ANY combine the per-row results using AND and OR
 * semantics respectively.
483
 * ARRAY requires just one target column, and creates an array of the target
484
 * column's type using any number of rows resulting from the subselect.
485
 *
486
 * SubLink is classed as an Expr node, but it is not actually executable;
487
 * it must be replaced in the expression tree by a SubPlan node during
488 489
 * planning.
 *
490 491
 * NOTE: in the raw output of gram.y, testexpr contains just the raw form
 * of the lefthand expression (if any), and operName is the String name of
B
Bruce Momjian 已提交
492
 * the combining operator.	Also, subselect is a raw parsetree.  During parse
493 494 495 496 497 498 499
 * analysis, the parser transforms testexpr into a complete boolean expression
 * that compares the lefthand value(s) to PARAM_SUBLINK nodes representing the
 * output columns of the subselect.  And subselect is transformed to a Query.
 * This is the representation seen in saved rules and in the rewriter.
 *
 * In EXISTS, EXPR, and ARRAY SubLinks, testexpr and operName are unused and
 * are always null.
500 501 502
 *
 * The CTE_SUBLINK case never occurs in actual SubLink nodes, but it is used
 * in SubPlans generated for WITH subqueries.
503 504 505
 */
typedef enum SubLinkType
{
506 507 508
	EXISTS_SUBLINK,
	ALL_SUBLINK,
	ANY_SUBLINK,
509
	ROWCOMPARE_SUBLINK,
510
	EXPR_SUBLINK,
511 512
	ARRAY_SUBLINK,
	CTE_SUBLINK					/* for SubPlans only */
513 514 515 516 517
} SubLinkType;


typedef struct SubLink
{
518
	Expr		xpr;
519 520
	SubLinkType subLinkType;	/* see above */
	Node	   *testexpr;		/* outer-query test for ALL/ANY/ROWCOMPARE */
521
	List	   *operName;		/* originally specified operator name */
B
Bruce Momjian 已提交
522
	Node	   *subselect;		/* subselect as Query* or parsetree */
523
	int			location;		/* token location, or -1 if unknown */
524 525
} SubLink;

526
/*
527 528 529
 * SubPlan - executable expression node for a subplan (sub-SELECT)
 *
 * The planner replaces SubLink nodes in expression trees with SubPlan
530 531 532 533
 * nodes after it has finished planning the subquery.  SubPlan references
 * a sub-plantree stored in the subplans list of the toplevel PlannedStmt.
 * (We avoid a direct link to make it easier to copy expression trees
 * without causing multiple processing of the subplan.)
534
 *
535 536 537 538 539 540 541 542 543 544 545
 * In an ordinary subplan, testexpr points to an executable expression
 * (OpExpr, an AND/OR tree of OpExprs, or RowCompareExpr) for the combining
 * operator(s); the left-hand arguments are the original lefthand expressions,
 * and the right-hand arguments are PARAM_EXEC Param nodes representing the
 * outputs of the sub-select.  (NOTE: runtime coercion functions may be
 * inserted as well.)  This is just the same expression tree as testexpr in
 * the original SubLink node, but the PARAM_SUBLINK nodes are replaced by
 * suitably numbered PARAM_EXEC nodes.
 *
 * If the sub-select becomes an initplan rather than a subplan, the executable
 * expression is part of the outer plan's expression tree (and the SubPlan
546 547
 * node itself is not, but rather is found in the outer plan's initPlan
 * list).  In this case testexpr is NULL to avoid duplication.
548
 *
549
 * The planner also derives lists of the values that need to be passed into
B
Bruce Momjian 已提交
550
 * and out of the subplan.	Input values are represented as a list "args" of
551 552 553
 * expressions to be evaluated in the outer-query context (currently these
 * args are always just Vars, but in principle they could be any expression).
 * The values are assigned to the global PARAM_EXEC params indexed by parParam
554
 * (the parParam and args lists must have the same ordering).  setParam is a
555
 * list of the PARAM_EXEC params that are computed by the sub-select, if it
556 557 558
 * is an initplan; they are listed in order by sub-select output column
 * position.  (parParam and setParam are integer Lists, not Bitmapsets,
 * because their ordering is significant.)
559 560 561 562
 *
 * Also, the planner computes startup and per-call costs for use of the
 * SubPlan.  Note that these include the cost of the subquery proper,
 * evaluation of the testexpr if any, and any hashtable management overhead.
563
 */
564
typedef struct SubPlan
565
{
566
	Expr		xpr;
567
	/* Fields copied from original SubLink: */
568 569 570
	SubLinkType subLinkType;	/* see above */
	/* The combining operators, transformed to an executable expression: */
	Node	   *testexpr;		/* OpExpr or RowCompareExpr expression tree */
571
	List	   *paramIds;		/* IDs of Params embedded in the above */
572 573
	/* Identification of the Plan tree to use: */
	int			plan_id;		/* Index (from 1) in PlannedStmt.subplans */
574 575
	/* Identification of the SubPlan for EXPLAIN and debugging purposes: */
	char	   *plan_name;		/* A name assigned during planning */
576
	/* Extra data useful for determining subplan's output type: */
577
	Oid			firstColType;	/* Type of first column of subplan result */
578
	int32		firstColTypmod; /* Typmod of first column of subplan result */
579 580
	Oid			firstColCollation;		/* Collation of first column of
										 * subplan result */
581
	/* Information about execution strategy: */
B
Bruce Momjian 已提交
582 583 584 585 586
	bool		useHashTable;	/* TRUE to store subselect output in a hash
								 * table (implies we are doing "IN") */
	bool		unknownEqFalse; /* TRUE if it's okay to return FALSE when the
								 * spec result is UNKNOWN; this allows much
								 * simpler handling of null values */
587
	/* Information for passing params into and out of the subselect: */
588
	/* setParam and parParam are lists of integers (param IDs) */
589 590
	List	   *setParam;		/* initplan subqueries have to set these
								 * Params for parent plan */
B
Bruce Momjian 已提交
591
	List	   *parParam;		/* indices of input Params from parent plan */
592
	List	   *args;			/* exprs to pass as parParam values */
593 594 595
	/* Estimated execution costs: */
	Cost		startup_cost;	/* one-time setup cost */
	Cost		per_call_cost;	/* cost for each subplan evaluation */
596
} SubPlan;
597

598 599 600 601 602 603 604 605 606 607 608 609 610 611
/*
 * AlternativeSubPlan - expression node for a choice among SubPlans
 *
 * The subplans are given as a List so that the node definition need not
 * change if there's ever more than two alternatives.  For the moment,
 * though, there are always exactly two; and the first one is the fast-start
 * plan.
 */
typedef struct AlternativeSubPlan
{
	Expr		xpr;
	List	   *subplans;		/* SubPlan(s) with equivalent results */
} AlternativeSubPlan;

612 613 614 615
/* ----------------
 * FieldSelect
 *
 * FieldSelect represents the operation of extracting one field from a tuple
616 617
 * value.  At runtime, the input expression is expected to yield a rowtype
 * Datum.  The specified field number is extracted and returned as a Datum.
618 619 620 621 622
 * ----------------
 */

typedef struct FieldSelect
{
623 624
	Expr		xpr;
	Expr	   *arg;			/* input expression */
625 626 627 628
	AttrNumber	fieldnum;		/* attribute number of field to extract */
	Oid			resulttype;		/* type of the field (result type of this
								 * node) */
	int32		resulttypmod;	/* output typmod (usually -1) */
629
	Oid			resultcollid;	/* OID of collation of the field */
630 631
} FieldSelect;

632 633 634 635 636 637 638 639 640
/* ----------------
 * FieldStore
 *
 * FieldStore represents the operation of modifying one field in a tuple
 * value, yielding a new tuple value (the input is not touched!).  Like
 * the assign case of ArrayRef, this is used to implement UPDATE of a
 * portion of a column.
 *
 * A single FieldStore can actually represent updates of several different
B
Bruce Momjian 已提交
641
 * fields.	The parser only generates FieldStores with single-element lists,
642 643 644 645 646 647 648 649 650 651 652 653
 * but the planner will collapse multiple updates of the same base column
 * into one FieldStore.
 * ----------------
 */

typedef struct FieldStore
{
	Expr		xpr;
	Expr	   *arg;			/* input tuple value */
	List	   *newvals;		/* new value(s) for field(s) */
	List	   *fieldnums;		/* integer list of field attnums */
	Oid			resulttype;		/* type of result (same as type of arg) */
654
	/* Like RowExpr, we deliberately omit a typmod and collation here */
655 656
} FieldStore;

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
/* ----------------
 * RelabelType
 *
 * 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
{
672 673
	Expr		xpr;
	Expr	   *arg;			/* input expression */
674 675
	Oid			resulttype;		/* output type of coercion expression */
	int32		resulttypmod;	/* output typmod (usually -1) */
676
	Oid			resultcollid;	/* OID of collation, or InvalidOid if none */
B
Bruce Momjian 已提交
677
	CoercionForm relabelformat; /* how to display this node */
678
	int			location;		/* token location, or -1 if unknown */
679 680
} RelabelType;

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
/* ----------------
 * CoerceViaIO
 *
 * CoerceViaIO represents a type coercion between two types whose textual
 * representations are compatible, implemented by invoking the source type's
 * typoutput function then the destination type's typinput function.
 * ----------------
 */

typedef struct CoerceViaIO
{
	Expr		xpr;
	Expr	   *arg;			/* input expression */
	Oid			resulttype;		/* output type of coercion */
	/* output typmod is not stored, but is presumed -1 */
696
	Oid			resultcollid;	/* OID of collation, or InvalidOid if none */
697
	CoercionForm coerceformat;	/* how to display this node */
698
	int			location;		/* token location, or -1 if unknown */
699
} CoerceViaIO;
700

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
/* ----------------
 * ArrayCoerceExpr
 *
 * ArrayCoerceExpr represents a type coercion from one array type to another,
 * which is implemented by applying the indicated element-type coercion
 * function to each element of the source array.  If elemfuncid is InvalidOid
 * then the element types are binary-compatible, but the coercion still
 * requires some effort (we have to fix the element type ID stored in the
 * array header).
 * ----------------
 */

typedef struct ArrayCoerceExpr
{
	Expr		xpr;
	Expr	   *arg;			/* input expression (yields an array) */
	Oid			elemfuncid;		/* OID of element coercion function, or 0 */
	Oid			resulttype;		/* output type of coercion (an array type) */
	int32		resulttypmod;	/* output typmod (also element typmod) */
720
	Oid			resultcollid;	/* OID of collation, or InvalidOid if none */
721 722
	bool		isExplicit;		/* conversion semantics flag to pass to func */
	CoercionForm coerceformat;	/* how to display this node */
723
	int			location;		/* token location, or -1 if unknown */
724
} ArrayCoerceExpr;
725

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
/* ----------------
 * ConvertRowtypeExpr
 *
 * ConvertRowtypeExpr represents a type coercion from one composite type
 * to another, where the source type is guaranteed to contain all the columns
 * needed for the destination type plus possibly others; the columns need not
 * be in the same positions, but are matched up by name.  This is primarily
 * used to convert a whole-row value of an inheritance child table into a
 * valid whole-row value of its parent table's rowtype.
 * ----------------
 */

typedef struct ConvertRowtypeExpr
{
	Expr		xpr;
	Expr	   *arg;			/* input expression */
	Oid			resulttype;		/* output type (always a composite type) */
743
	/* Like RowExpr, we deliberately omit a typmod and collation here */
744
	CoercionForm convertformat; /* how to display this node */
745
	int			location;		/* token location, or -1 if unknown */
746 747
} ConvertRowtypeExpr;

748 749
/*----------
 * CollateExpr - COLLATE
750 751 752
 *
 * The planner replaces CollateExpr with RelabelType during expression
 * preprocessing, so execution never sees a CollateExpr.
753 754 755 756 757 758 759 760 761 762
 *----------
 */
typedef struct CollateExpr
{
	Expr		xpr;
	Expr	   *arg;			/* input expression */
	Oid			collOid;		/* collation's OID */
	int			location;		/* token location, or -1 if unknown */
} CollateExpr;

763
/*----------
764
 * CaseExpr - a CASE expression
765 766 767 768 769 770 771 772
 *
 * We support two distinct forms of CASE expression:
 *		CASE WHEN boolexpr THEN expr [ WHEN boolexpr THEN expr ... ]
 *		CASE testexpr WHEN compexpr THEN expr [ WHEN compexpr THEN expr ... ]
 * These are distinguishable by the "arg" field being NULL in the first case
 * and the testexpr in the second case.
 *
 * In the raw grammar output for the second form, the condition expressions
B
Bruce Momjian 已提交
773
 * of the WHEN clauses are just the comparison values.	Parse analysis
774 775 776 777 778 779 780 781 782 783
 * converts these to valid boolean expressions of the form
 *		CaseTestExpr '=' compexpr
 * where the CaseTestExpr node is a placeholder that emits the correct
 * value at runtime.  This structure is used so that the testexpr need be
 * evaluated only once.  Note that after parse analysis, the condition
 * expressions always yield boolean.
 *
 * Note: we can test whether a CaseExpr has been through parse analysis
 * yet by checking whether casetype is InvalidOid or not.
 *----------
784 785 786 787 788
 */
typedef struct CaseExpr
{
	Expr		xpr;
	Oid			casetype;		/* type of expression result */
789
	Oid			casecollid;		/* OID of collation, or InvalidOid if none */
790 791 792
	Expr	   *arg;			/* implicit equality comparison argument */
	List	   *args;			/* the arguments (list of WHEN clauses) */
	Expr	   *defresult;		/* the default result (ELSE clause) */
793
	int			location;		/* token location, or -1 if unknown */
794 795 796
} CaseExpr;

/*
797
 * CaseWhen - one arm of a CASE expression
798 799 800 801 802 803
 */
typedef struct CaseWhen
{
	Expr		xpr;
	Expr	   *expr;			/* condition expression */
	Expr	   *result;			/* substitution result */
804
	int			location;		/* token location, or -1 if unknown */
805 806
} CaseWhen;

807 808 809 810
/*
 * Placeholder node for the test value to be processed by a CASE expression.
 * This is effectively like a Param, but can be implemented more simply
 * since we need only one replacement value at a time.
811 812 813
 *
 * We also use this in nested UPDATE expressions.
 * See transformAssignmentIndirection().
814 815 816 817 818 819
 */
typedef struct CaseTestExpr
{
	Expr		xpr;
	Oid			typeId;			/* type for substituted value */
	int32		typeMod;		/* typemod for substituted value */
P
Peter Eisentraut 已提交
820
	Oid			collation;		/* collation for the substituted value */
821 822
} CaseTestExpr;

823 824 825
/*
 * ArrayExpr - an ARRAY[] expression
 *
826 827 828 829
 * Note: if multidims is false, the constituent expressions all yield the
 * scalar type identified by element_typeid.  If multidims is true, the
 * constituent expressions all yield arrays of element_typeid (ie, the same
 * type as array_typeid); at runtime we must check for compatible subscripts.
830 831 832 833 834
 */
typedef struct ArrayExpr
{
	Expr		xpr;
	Oid			array_typeid;	/* type of expression result */
835
	Oid			array_collid;	/* OID of collation, or InvalidOid if none */
836 837 838
	Oid			element_typeid; /* common type of array elements */
	List	   *elements;		/* the array elements or sub-arrays */
	bool		multidims;		/* true if elements are sub-arrays */
839
	int			location;		/* token location, or -1 if unknown */
840
} ArrayExpr;
841

842 843
/*
 * RowExpr - a ROW() expression
844 845 846
 *
 * Note: the list of fields must have a one-for-one correspondence with
 * physical fields of the associated rowtype, although it is okay for it
B
Bruce Momjian 已提交
847
 * to be shorter than the rowtype.	That is, the N'th list element must
848 849
 * match up with the N'th physical field.  When the N'th physical field
 * is a dropped column (attisdropped) then the N'th list element can just
B
Bruce Momjian 已提交
850
 * be a NULL constant.	(This case can only occur for named composite types,
851 852 853
 * not RECORD types, since those are built from the RowExpr itself rather
 * than vice versa.)  It is important not to assume that length(args) is
 * the same as the number of columns logically present in the rowtype.
854
 *
855
 * colnames provides field names in cases where the names can't easily be
856
 * obtained otherwise.	Names *must* be provided if row_typeid is RECORDOID.
857 858 859 860 861
 * If row_typeid identifies a known composite type, colnames can be NIL to
 * indicate the type's cataloged field names apply.  Note that colnames can
 * be non-NIL even for a composite type, and typically is when the RowExpr
 * was created by expanding a whole-row Var.  This is so that we can retain
 * the column alias names of the RTE that the Var referenced (which would
862
 * otherwise be very difficult to extract from the parsetree).	Like the
863
 * args list, colnames is one-for-one with physical fields of the rowtype.
864 865 866 867 868 869
 */
typedef struct RowExpr
{
	Expr		xpr;
	List	   *args;			/* the fields */
	Oid			row_typeid;		/* RECORDOID or a composite type's ID */
B
Bruce Momjian 已提交
870

871
	/*
B
Bruce Momjian 已提交
872 873 874 875
	 * Note: we deliberately do NOT store a typmod.  Although a typmod will be
	 * associated with specific RECORD types at runtime, it will differ for
	 * different backends, and so cannot safely be stored in stored
	 * parsetrees.	We must assume typmod -1 for a RowExpr node.
876 877 878
	 *
	 * We don't need to store a collation either.  The result type is
	 * necessarily composite, and composite types never have a collation.
879 880
	 */
	CoercionForm row_format;	/* how to display this node */
881
	List	   *colnames;		/* list of String, or NIL */
882
	int			location;		/* token location, or -1 if unknown */
883 884
} RowExpr;

885 886 887 888 889
/*
 * RowCompareExpr - row-wise comparison, such as (a, b) <= (1, 2)
 *
 * We support row comparison for any operator that can be determined to
 * act like =, <>, <, <=, >, or >= (we determine this by looking for the
890
 * operator in btree opfamilies).  Note that the same operator name might
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
 * map to a different operator for each pair of row elements, since the
 * element datatypes can vary.
 *
 * A RowCompareExpr node is only generated for the < <= > >= cases;
 * the = and <> cases are translated to simple AND or OR combinations
 * of the pairwise comparisons.  However, we include = and <> in the
 * RowCompareType enum for the convenience of parser logic.
 */
typedef enum RowCompareType
{
	/* Values of this enum are chosen to match btree strategy numbers */
	ROWCOMPARE_LT = 1,			/* BTLessStrategyNumber */
	ROWCOMPARE_LE = 2,			/* BTLessEqualStrategyNumber */
	ROWCOMPARE_EQ = 3,			/* BTEqualStrategyNumber */
	ROWCOMPARE_GE = 4,			/* BTGreaterEqualStrategyNumber */
	ROWCOMPARE_GT = 5,			/* BTGreaterStrategyNumber */
	ROWCOMPARE_NE = 6			/* no such btree strategy */
} RowCompareType;

typedef struct RowCompareExpr
{
	Expr		xpr;
	RowCompareType rctype;		/* LT LE GE or GT, never EQ or NE */
	List	   *opnos;			/* OID list of pairwise comparison ops */
915
	List	   *opfamilies;		/* OID list of containing operator families */
916
	List	   *inputcollids;	/* OID list of collations for comparisons */
917 918 919 920
	List	   *largs;			/* the left-hand input arguments */
	List	   *rargs;			/* the right-hand input arguments */
} RowCompareExpr;

921 922 923 924 925
/*
 * CoalesceExpr - a COALESCE expression
 */
typedef struct CoalesceExpr
{
B
Bruce Momjian 已提交
926 927
	Expr		xpr;
	Oid			coalescetype;	/* type of expression result */
928
	Oid			coalescecollid; /* OID of collation, or InvalidOid if none */
B
Bruce Momjian 已提交
929
	List	   *args;			/* the arguments */
930
	int			location;		/* token location, or -1 if unknown */
931
} CoalesceExpr;
932

933 934 935 936 937 938 939 940 941 942 943 944 945
/*
 * MinMaxExpr - a GREATEST or LEAST function
 */
typedef enum MinMaxOp
{
	IS_GREATEST,
	IS_LEAST
} MinMaxOp;

typedef struct MinMaxExpr
{
	Expr		xpr;
	Oid			minmaxtype;		/* common type of arguments and result */
946 947
	Oid			minmaxcollid;	/* OID of collation of result */
	Oid			inputcollid;	/* OID of collation that function should use */
948 949
	MinMaxOp	op;				/* function to execute */
	List	   *args;			/* the arguments */
950
	int			location;		/* token location, or -1 if unknown */
951 952
} MinMaxExpr;

953 954 955 956 957 958
/*
 * XmlExpr - various SQL/XML functions requiring special grammar productions
 *
 * 'name' carries the "NAME foo" argument (already XML-escaped).
 * 'named_args' and 'arg_names' represent an xml_attribute list.
 * 'args' carries all other arguments.
959 960
 *
 * Note: result type/typmod/collation are not stored, but can be deduced
961
 * from the XmlExprOp.	The type/typmod fields are just used for display
962
 * purposes, and are NOT the true result type of the node.
963 964 965 966 967 968 969 970
 */
typedef enum XmlExprOp
{
	IS_XMLCONCAT,				/* XMLCONCAT(args) */
	IS_XMLELEMENT,				/* XMLELEMENT(name, xml_attributes, args) */
	IS_XMLFOREST,				/* XMLFOREST(xml_attributes) */
	IS_XMLPARSE,				/* XMLPARSE(text, is_doc, preserve_ws) */
	IS_XMLPI,					/* XMLPI(name [, args]) */
971
	IS_XMLROOT,					/* XMLROOT(xml, version, standalone) */
972
	IS_XMLSERIALIZE,			/* XMLSERIALIZE(is_document, xmlval) */
973
	IS_DOCUMENT					/* xmlval IS DOCUMENT */
974
} XmlExprOp;
975

976 977 978 979
typedef enum
{
	XMLOPTION_DOCUMENT,
	XMLOPTION_CONTENT
980
} XmlOptionType;
981

982 983 984 985 986 987 988 989
typedef struct XmlExpr
{
	Expr		xpr;
	XmlExprOp	op;				/* xml function ID */
	char	   *name;			/* name in xml(NAME foo ...) syntaxes */
	List	   *named_args;		/* non-XML expressions for xml_attributes */
	List	   *arg_names;		/* parallel list of Value strings */
	List	   *args;			/* list of expressions */
990
	XmlOptionType xmloption;	/* DOCUMENT or CONTENT */
991
	Oid			type;			/* target type/typmod for XMLSERIALIZE */
992
	int32		typmod;
993
	int			location;		/* token location, or -1 if unknown */
994
} XmlExpr;
995

996 997 998 999 1000
/* ----------------
 * NullTest
 *
 * NullTest represents the operation of testing a value for NULLness.
 * The appropriate test is performed and returned as a boolean Datum.
1001 1002
 *
 * NOTE: the semantics of this for rowtype inputs are noticeably different
1003
 * from the scalar case.  We provide an "argisrow" flag to reflect that.
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
 * ----------------
 */

typedef enum NullTestType
{
	IS_NULL, IS_NOT_NULL
} NullTestType;

typedef struct NullTest
{
	Expr		xpr;
	Expr	   *arg;			/* input expression */
	NullTestType nulltesttype;	/* IS NULL, IS NOT NULL */
1017
	bool		argisrow;		/* T if input is of a composite type */
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
} NullTest;

/*
 * BooleanTest
 *
 * BooleanTest represents the operation of determining whether a boolean
 * is TRUE, FALSE, or UNKNOWN (ie, NULL).  All six meaningful combinations
 * are supported.  Note that a NULL input does *not* cause a NULL result.
 * The appropriate test is performed and returned as a boolean Datum.
 */

typedef enum BoolTestType
{
	IS_TRUE, IS_NOT_TRUE, IS_FALSE, IS_NOT_FALSE, IS_UNKNOWN, IS_NOT_UNKNOWN
} BoolTestType;

typedef struct BooleanTest
{
	Expr		xpr;
	Expr	   *arg;			/* input expression */
	BoolTestType booltesttype;	/* test type */
} BooleanTest;

/*
1042
 * CoerceToDomain
1043
 *
1044 1045
 * CoerceToDomain represents the operation of coercing a value to a domain
 * type.  At runtime (and not before) the precise set of constraints to be
B
Bruce Momjian 已提交
1046 1047
 * checked will be determined.	If the value passes, it is returned as the
 * result; if not, an error is raised.	Note that this is equivalent to
1048
 * RelabelType in the scenario where no constraints are applied.
1049
 */
1050
typedef struct CoerceToDomain
1051 1052 1053
{
	Expr		xpr;
	Expr	   *arg;			/* input expression */
1054 1055
	Oid			resulttype;		/* domain type ID (result type) */
	int32		resulttypmod;	/* output typmod (currently always -1) */
1056
	Oid			resultcollid;	/* OID of collation, or InvalidOid if none */
B
Bruce Momjian 已提交
1057
	CoercionForm coercionformat;	/* how to display this node */
1058
	int			location;		/* token location, or -1 if unknown */
1059
} CoerceToDomain;
1060 1061

/*
1062
 * Placeholder node for the value to be processed by a domain's check
B
Bruce Momjian 已提交
1063
 * constraint.	This is effectively like a Param, but can be implemented more
1064 1065
 * simply since we need only one replacement value at a time.
 *
1066 1067 1068
 * Note: the typeId/typeMod/collation will be set from the domain's base type,
 * not the domain itself.  This is because we shouldn't consider the value
 * to be a member of the domain if we haven't yet checked its constraints.
1069
 */
1070
typedef struct CoerceToDomainValue
1071 1072
{
	Expr		xpr;
1073 1074
	Oid			typeId;			/* type for substituted value */
	int32		typeMod;		/* typemod for substituted value */
1075
	Oid			collation;		/* collation for the substituted value */
1076
	int			location;		/* token location, or -1 if unknown */
1077
} CoerceToDomainValue;
1078

1079 1080 1081 1082
/*
 * Placeholder node for a DEFAULT marker in an INSERT or UPDATE command.
 *
 * This is not an executable expression: it must be replaced by the actual
B
Bruce Momjian 已提交
1083
 * column default expression during rewriting.	But it is convenient to
1084 1085 1086 1087 1088 1089 1090
 * treat it as an expression node during parsing and rewriting.
 */
typedef struct SetToDefault
{
	Expr		xpr;
	Oid			typeId;			/* type for substituted value */
	int32		typeMod;		/* typemod for substituted value */
1091
	Oid			collation;		/* collation for the substituted value */
1092
	int			location;		/* token location, or -1 if unknown */
1093
} SetToDefault;
1094

1095 1096 1097 1098 1099 1100 1101
/*
 * Node representing [WHERE] CURRENT OF cursor_name
 *
 * CURRENT OF is a bit like a Var, in that it carries the rangetable index
 * of the target relation being constrained; this aids placing the expression
 * correctly during planning.  We can assume however that its "levelsup" is
 * always zero, due to the syntactic constraints on where it can appear.
1102 1103 1104 1105
 *
 * The referenced cursor can be represented either as a hardwired string
 * or as a reference to a run-time parameter of type REFCURSOR.  The latter
 * case is for the convenience of plpgsql.
1106 1107 1108 1109 1110
 */
typedef struct CurrentOfExpr
{
	Expr		xpr;
	Index		cvarno;			/* RT index of target relation */
1111 1112
	char	   *cursor_name;	/* name of referenced cursor, or NULL */
	int			cursor_param;	/* refcursor parameter number, or 0 */
1113
} CurrentOfExpr;
1114

1115
/*--------------------
1116 1117 1118 1119 1120 1121 1122 1123
 * TargetEntry -
 *	   a target entry (used in query target lists)
 *
 * Strictly speaking, a TargetEntry isn't an expression node (since it can't
 * be evaluated by ExecEvalExpr).  But we treat it as one anyway, since in
 * very many places it's convenient to process a whole query targetlist as a
 * single expression tree.
 *
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
 * In a SELECT's targetlist, resno should always be equal to the item's
 * ordinal position (counting from 1).	However, in an INSERT or UPDATE
 * targetlist, resno represents the attribute number of the destination
 * column for the item; so there may be missing or out-of-order resnos.
 * It is even legal to have duplicated resnos; consider
 *		UPDATE table SET arraycol[1] = ..., arraycol[2] = ..., ...
 * The two meanings come together in the executor, because the planner
 * transforms INSERT/UPDATE tlists into a normalized form with exactly
 * one entry for each column of the destination table.	Before that's
 * happened, however, it is risky to assume that resno == position.
 * Generally get_tle_by_resno() should be used rather than list_nth()
 * to fetch tlist entries by resno, and only in SELECT should you assume
 * that resno is a unique identifier.
 *
 * resname is required to represent the correct column name in non-resjunk
 * entries of top-level SELECT targetlists, since it will be used as the
 * column title sent to the frontend.  In most other contexts it is only
 * a debugging aid, and may be wrong or even NULL.	(In particular, it may
 * be wrong in a tlist from a stored rule, if the referenced column has been
 * renamed by ALTER TABLE since the rule was made.	Also, the planner tends
 * to store NULL rather than look up a valid name for tlist entries in
 * non-toplevel plan nodes.)  In resjunk entries, resname should be either
 * a specific system-generated name (such as "ctid") or NULL; anything else
 * risks confusing ExecGetJunkAttribute!
 *
1149
 * ressortgroupref is used in the representation of ORDER BY, GROUP BY, and
1150
 * DISTINCT items.	Targetlist entries with ressortgroupref=0 are not
1151
 * sort/group items.  If ressortgroupref>0, then this item is an ORDER BY,
1152
 * GROUP BY, and/or DISTINCT target value.	No two entries in a targetlist
1153 1154 1155 1156
 * may have the same nonzero ressortgroupref --- but there is no particular
 * meaning to the nonzero values, except as tags.  (For example, one must
 * not assume that lower ressortgroupref means a more significant sort key.)
 * The order of the associated SortGroupClause lists determine the semantics.
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
 *
 * resorigtbl/resorigcol identify the source of the column, if it is a
 * simple reference to a column of a base table (or view).	If it is not
 * a simple reference, these fields are zeroes.
 *
 * If resjunk is true then the column is a working column (such as a sort key)
 * that should be removed from the final output of the query.  Resjunk columns
 * must have resnos that cannot duplicate any regular column's resno.  Also
 * note that there are places that assume resjunk columns come after non-junk
 * columns.
 *--------------------
1168 1169 1170 1171 1172
 */
typedef struct TargetEntry
{
	Expr		xpr;
	Expr	   *expr;			/* expression to evaluate */
1173 1174 1175 1176 1177 1178
	AttrNumber	resno;			/* attribute number (see notes above) */
	char	   *resname;		/* name of the column (could be NULL) */
	Index		ressortgroupref;/* nonzero if referenced by a sort/group
								 * clause */
	Oid			resorigtbl;		/* OID of column's source table */
	AttrNumber	resorigcol;		/* column's number in source table */
B
Bruce Momjian 已提交
1179 1180
	bool		resjunk;		/* set to true to eliminate the attribute from
								 * final target list */
1181 1182
} TargetEntry;

1183 1184 1185 1186 1187 1188

/* ----------------------------------------------------------------
 *					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
1189 1190 1191
 * or qualified join.  Also, FromExpr nodes can appear to denote an
 * ordinary cross-product join ("FROM foo, bar, baz WHERE ...").
 * FromExpr is like a JoinExpr of jointype JOIN_INNER, except that it
1192
 * may have any number of child nodes, not just two.
1193 1194 1195
 *
 * NOTE: the top level of a Query's jointree is always a FromExpr.
 * Even if the jointree contains no rels, there will be a FromExpr.
1196 1197
 *
 * NOTE: the qualification expressions present in JoinExpr nodes are
1198
 * *in addition to* the query's main WHERE clause, which appears as the
B
Bruce Momjian 已提交
1199
 * qual of the top-level FromExpr.	The reason for associating quals with
1200 1201 1202 1203 1204
 * specific nodes in the jointree is that the position of a qual is critical
 * when outer joins are present.  (If we enforce a qual too soon or too late,
 * that may cause the outer join to produce the wrong set of NULL-extended
 * rows.)  If all joins are inner joins then all the qual positions are
 * semantically interchangeable.
1205
 *
1206 1207 1208 1209 1210
 * NOTE: in the raw output of gram.y, a join tree contains RangeVar,
 * RangeSubselect, and RangeFunction nodes, which are all replaced by
 * RangeTblRef nodes during the parse analysis phase.  Also, the top-level
 * FromExpr is added during parse analysis; the grammar regards FROM and
 * WHERE as separate.
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
 * ----------------------------------------------------------------
 */

/*
 * 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
 *
1230 1231
 * isNatural, usingClause, and quals are interdependent.  The user can write
 * only one of NATURAL, USING(), or ON() (this is enforced by the grammar).
1232 1233 1234
 * 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.
B
Bruce Momjian 已提交
1235
 * If he writes ON() then only "quals" is set.	Note that NATURAL/USING
1236 1237
 * are not equivalent to ON() since they also affect the output column list.
 *
1238
 * alias is an Alias node representing the AS alias-clause attached to the
1239 1240 1241 1242
 * join expression, or NULL if no clause.  NB: presence or absence of the
 * alias has a critical impact on semantics, because a join with an alias
 * restricts visibility of the tables/columns inside it.
 *
1243
 * During parse analysis, an RTE is created for the Join, and its index
B
Bruce Momjian 已提交
1244
 * is filled into rtindex.	This RTE is present mainly so that Vars can
1245 1246 1247
 * be created that refer to the outputs of the join.  The planner sometimes
 * generates JoinExprs internally; these can have rtindex = 0 if there are
 * no join alias variables referencing such joins.
1248 1249 1250 1251 1252 1253 1254 1255 1256
 *----------
 */
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 */
1257
	List	   *usingClause;	/* USING clause, if any (list of String) */
1258
	Node	   *quals;			/* qualifiers on join, if any */
1259
	Alias	   *alias;			/* user-written alias clause, if any */
1260
	int			rtindex;		/* RT index assigned for join, or 0 */
1261 1262
} JoinExpr;

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
/*----------
 * FromExpr - represents a FROM ... WHERE ... construct
 *
 * This is both more flexible than a JoinExpr (it can have any number of
 * children, including zero) and less so --- we don't need to deal with
 * aliases and so on.  The output column set is implicitly just the union
 * of the outputs of the children.
 *----------
 */
typedef struct FromExpr
{
	NodeTag		type;
	List	   *fromlist;		/* List of join subtrees */
	Node	   *quals;			/* qualifiers on join, if any */
} FromExpr;
1278

1279
#endif   /* PRIMNODES_H */