primnodes.h 36.4 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-2006, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
11
 * Portions Copyright (c) 1994, Regents of the University of California
12
 *
13
 * $PostgreSQL: pgsql/src/include/nodes/primnodes.h,v 1.118 2006/12/10 22:13:27 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
 *						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 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 */
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;

/*
 * 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 已提交
66 67
	InhOption	inhOpt;			/* expand rel by inheritance? recursively act
								 * on children? */
68 69 70 71 72
	bool		istemp;			/* is this a temp relation/sequence? */
	Alias	   *alias;			/* table alias & optional column aliases */
} RangeVar;


73 74 75 76 77
/* ----------------------------------------------------------------
 *					node types for executable expressions
 * ----------------------------------------------------------------
 */

78
/*
79
 * Expr - generic superclass for executable-expression nodes
80
 *
81 82 83 84
 * 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.
85
 */
86 87
typedef struct Expr
{
88
	NodeTag		type;
89
} Expr;
90

91
/*
92
 * Var - expression node representing a variable (ie, a table column)
93 94 95 96 97 98 99 100 101
 *
 * 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.
102
 */
103 104
#define    INNER		65000
#define    OUTER		65001
105

106 107
#define    PRS2_OLD_VARNO			1
#define    PRS2_NEW_VARNO			2
108

109 110
typedef struct Var
{
111
	Expr		xpr;
B
Bruce Momjian 已提交
112 113 114 115
	Index		varno;			/* index of this var's relation in the range
								 * table (could also be INNER or OUTER) */
	AttrNumber	varattno;		/* attribute number of this var, or zero for
								 * all */
116
	Oid			vartype;		/* pg_type OID for the type of this var */
B
Bruce Momjian 已提交
117
	int32		vartypmod;		/* pg_attribute typmod value */
118
	Index		varlevelsup;
B
Bruce Momjian 已提交
119 120

	/*
B
Bruce Momjian 已提交
121 122
	 * for subquery variables referencing outer relations; 0 in a normal var,
	 * >0 means N levels up
B
Bruce Momjian 已提交
123 124 125
	 */
	Index		varnoold;		/* original value of varno, for debugging */
	AttrNumber	varoattno;		/* original value of varattno */
126
} Var;
127

128
/*
129 130
 * Const
 */
131 132
typedef struct Const
{
133
	Expr		xpr;
134 135
	Oid			consttype;		/* PG_TYPE OID of the constant's datatype */
	int			constlen;		/* typlen of the constant's datatype */
136 137
	Datum		constvalue;		/* the constant's value */
	bool		constisnull;	/* whether the constant is null (if true,
138
								 * constvalue is undefined) */
B
Bruce Momjian 已提交
139 140 141 142
	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. */
143
} Const;
144 145 146

/* ----------------
 * Param
147
 *		paramkind - specifies the kind of parameter. The possible values
148
 *		for this field are:
149
 *
150 151
 *		PARAM_EXTERN:  The parameter value is supplied from outside the plan.
 *				Such parameters are numbered from 1 to n.
152
 *
153 154 155 156
 *		PARAM_EXEC:  The parameter is an internal executor parameter, used
 *				for passing values into and out of sub-queries.
 *				For historical reasons, such parameters are numbered from 0.
 *				These numbers are independent of PARAM_EXTERN numbers.
157
 *
B
Bruce Momjian 已提交
158
 *		PARAM_SUBLINK:	The parameter represents an output column of a SubLink
159 160 161
 *				node's sub-select.  The column number is contained in the
 *				`paramid' field.  (This type of Param is converted to
 *				PARAM_EXEC during planning.)
162 163 164 165 166
 *
 * 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.
167 168
 * ----------------
 */
169 170 171 172 173 174 175
typedef enum ParamKind
{
	PARAM_EXTERN,
	PARAM_EXEC,
	PARAM_SUBLINK
} ParamKind;

176 177
typedef struct Param
{
178
	Expr		xpr;
179 180
	ParamKind	paramkind;		/* kind of parameter. See above */
	int			paramid;		/* numeric ID for parameter */
181 182
	Oid			paramtype;		/* pg_type OID of parameter's datatype */
	int32		paramtypmod;	/* typmod value, if known */
183
} Param;
184

185
/*
B
Bruce Momjian 已提交
186
 * Aggref
187
 */
B
Bruce Momjian 已提交
188
typedef struct Aggref
189
{
190
	Expr		xpr;
191 192
	Oid			aggfnoid;		/* pg_proc Oid of the aggregate */
	Oid			aggtype;		/* type Oid of result of the aggregate */
193
	List	   *args;			/* arguments to the aggregate */
194
	Index		agglevelsup;	/* > 0 if agg belongs to outer query */
195
	bool		aggstar;		/* TRUE if argument list was really '*' */
B
Bruce Momjian 已提交
196
	bool		aggdistinct;	/* TRUE if it's agg(DISTINCT ...) */
197
} Aggref;
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/* ----------------
 *	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.
 *
 * Note: refrestype is NOT the element type, but the array type,
217
 * when doing subarray fetch or either type of store.
218 219 220 221 222 223 224
 * ----------------
 */
typedef struct ArrayRef
{
	Expr		xpr;
	Oid			refrestype;		/* type of the result of the ArrayRef
								 * operation */
225 226
	Oid			refarraytype;	/* type of the array proper */
	Oid			refelemtype;	/* type of the array elements */
B
Bruce Momjian 已提交
227 228 229 230 231 232 233 234
	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 */
235 236 237 238 239 240 241 242 243 244 245 246 247
} 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 */
248
} CoercionContext;
249 250 251 252 253 254 255 256 257

/*
 * CoercionForm - information showing how to display a function-call node
 */
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 */
258
	COERCE_DONTCARE				/* special case for planner */
259
} CoercionForm;
260 261 262 263 264 265 266 267 268 269 270 271

/*
 * 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 */
	List	   *args;			/* arguments to the function */
272
} FuncExpr;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

/*
 * 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
 * tree to the executor, but during parsing/planning opfuncid is typically 0.
 */
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 */
	List	   *args;			/* arguments to the operator (1 or 2) */
291
} OpExpr;
292 293 294 295 296 297 298 299 300 301 302 303 304

/*
 * 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;

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/*
 * 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
 * the result type because it must be boolean.
 */
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 */
	List	   *args;			/* the scalar and array operands */
322
} ScalarArrayOpExpr;
323

324 325 326 327 328
/*
 * 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
B
Bruce Momjian 已提交
329
 * handle any number of arguments.	The parser treats AND and OR as binary
330 331 332 333 334 335
 * and so it only produces two-element lists, but the optimizer will flatten
 * trees of AND and OR nodes to produce longer lists when possible.
 */
typedef enum BoolExprType
{
	AND_EXPR, OR_EXPR, NOT_EXPR
336
} BoolExprType;
337 338 339 340 341 342

typedef struct BoolExpr
{
	Expr		xpr;
	BoolExprType boolop;
	List	   *args;			/* arguments to this expression */
343
} BoolExpr;
344

345
/*
346
 * SubLink
347
 *
B
Bruce Momjian 已提交
348 349 350
 * 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:
351 352 353
 *	EXISTS_SUBLINK		EXISTS(SELECT ...)
 *	ALL_SUBLINK			(lefthand) op ALL (SELECT ...)
 *	ANY_SUBLINK			(lefthand) op ANY (SELECT ...)
354
 *	ROWCOMPARE_SUBLINK	(lefthand) op (SELECT ...)
355
 *	EXPR_SUBLINK		(SELECT with single targetlist item ...)
356
 *	ARRAY_SUBLINK		ARRAY(SELECT with single targetlist item ...)
357 358
 * 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
359 360 361
 * 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
362 363 364 365
 * 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.
366 367
 * ARRAY requires just one target column, and creates an array of the target
 * column's type using one or more rows resulting from the subselect.
368
 *
369
 * SubLink is classed as an Expr node, but it is not actually executable;
370
 * it must be replaced in the expression tree by a SubPlan node during
371 372
 * planning.
 *
373 374
 * 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 已提交
375
 * the combining operator.	Also, subselect is a raw parsetree.  During parse
376 377 378 379 380 381 382
 * 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.
383 384 385
 */
typedef enum SubLinkType
{
386 387 388
	EXISTS_SUBLINK,
	ALL_SUBLINK,
	ANY_SUBLINK,
389
	ROWCOMPARE_SUBLINK,
390 391
	EXPR_SUBLINK,
	ARRAY_SUBLINK
392 393 394 395 396
} SubLinkType;


typedef struct SubLink
{
397
	Expr		xpr;
398 399
	SubLinkType subLinkType;	/* see above */
	Node	   *testexpr;		/* outer-query test for ALL/ANY/ROWCOMPARE */
400
	List	   *operName;		/* originally specified operator name */
B
Bruce Momjian 已提交
401
	Node	   *subselect;		/* subselect as Query* or parsetree */
402 403
} SubLink;

404
/*
405 406 407 408
 * SubPlan - executable expression node for a subplan (sub-SELECT)
 *
 * The planner replaces SubLink nodes in expression trees with SubPlan
 * nodes after it has finished planning the subquery.  SubPlan contains
409 410
 * a sub-plantree and rtable instead of a sub-Query.
 *
411 412 413 414 415 416 417 418 419 420 421 422
 * 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
 * node itself is not).  In this case testexpr is NULL to avoid duplication.
423
 *
424
 * The planner also derives lists of the values that need to be passed into
B
Bruce Momjian 已提交
425
 * and out of the subplan.	Input values are represented as a list "args" of
426 427 428
 * 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
429
 * (the parParam and args lists must have the same ordering).  setParam is a
430
 * list of the PARAM_EXEC params that are computed by the sub-select, if it
431 432 433
 * 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.)
434
 */
435
typedef struct SubPlan
436
{
437
	Expr		xpr;
438
	/* Fields copied from original SubLink: */
439 440 441
	SubLinkType subLinkType;	/* see above */
	/* The combining operators, transformed to an executable expression: */
	Node	   *testexpr;		/* OpExpr or RowCompareExpr expression tree */
442
	List	   *paramIds;		/* IDs of Params embedded in the above */
443
	/* The subselect, transformed to a Plan: */
444 445
	struct Plan *plan;			/* subselect plan itself */
	int			plan_id;		/* dummy thing because of we haven't equal
B
Bruce Momjian 已提交
446 447 448
								 * funcs for plan nodes... actually, we could
								 * put *plan itself somewhere else (TopPlan
								 * node ?)... */
449
	List	   *rtable;			/* range table for subselect */
450
	/* Information about execution strategy: */
B
Bruce Momjian 已提交
451 452 453 454 455
	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 */
456
	/* Information for passing params into and out of the subselect: */
457
	/* setParam and parParam are lists of integers (param IDs) */
458 459
	List	   *setParam;		/* initplan subqueries have to set these
								 * Params for parent plan */
B
Bruce Momjian 已提交
460
	List	   *parParam;		/* indices of input Params from parent plan */
461
	List	   *args;			/* exprs to pass as parParam values */
462
} SubPlan;
463

464 465 466 467
/* ----------------
 * FieldSelect
 *
 * FieldSelect represents the operation of extracting one field from a tuple
468 469
 * value.  At runtime, the input expression is expected to yield a rowtype
 * Datum.  The specified field number is extracted and returned as a Datum.
470 471 472 473 474
 * ----------------
 */

typedef struct FieldSelect
{
475 476
	Expr		xpr;
	Expr	   *arg;			/* input expression */
477 478 479 480
	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) */
481 482
} FieldSelect;

483 484 485 486 487 488 489 490 491
/* ----------------
 * 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 已提交
492
 * fields.	The parser only generates FieldStores with single-element lists,
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
 * 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) */
	/* Like RowExpr, we deliberately omit a typmod here */
} FieldStore;

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
/* ----------------
 * 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
{
523 524
	Expr		xpr;
	Expr	   *arg;			/* input expression */
525 526
	Oid			resulttype;		/* output type of coercion expression */
	int32		resulttypmod;	/* output typmod (usually -1) */
B
Bruce Momjian 已提交
527
	CoercionForm relabelformat; /* how to display this node */
528 529
} RelabelType;

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/* ----------------
 * 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) */
	/* result typmod is not stored, but must be -1; see RowExpr comments */
	CoercionForm convertformat; /* how to display this node */
} ConvertRowtypeExpr;

551
/*----------
552
 * CaseExpr - a CASE expression
553 554 555 556 557 558 559 560
 *
 * 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 已提交
561
 * of the WHEN clauses are just the comparison values.	Parse analysis
562 563 564 565 566 567 568 569 570 571
 * 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.
 *----------
572 573 574 575 576 577 578 579 580 581 582
 */
typedef struct CaseExpr
{
	Expr		xpr;
	Oid			casetype;		/* type of expression result */
	Expr	   *arg;			/* implicit equality comparison argument */
	List	   *args;			/* the arguments (list of WHEN clauses) */
	Expr	   *defresult;		/* the default result (ELSE clause) */
} CaseExpr;

/*
583
 * CaseWhen - one arm of a CASE expression
584 585 586 587 588 589 590 591
 */
typedef struct CaseWhen
{
	Expr		xpr;
	Expr	   *expr;			/* condition expression */
	Expr	   *result;			/* substitution result */
} CaseWhen;

592 593 594 595
/*
 * 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.
596 597 598
 *
 * We also use this in nested UPDATE expressions.
 * See transformAssignmentIndirection().
599 600 601 602 603 604 605 606
 */
typedef struct CaseTestExpr
{
	Expr		xpr;
	Oid			typeId;			/* type for substituted value */
	int32		typeMod;		/* typemod for substituted value */
} CaseTestExpr;

607 608 609
/*
 * ArrayExpr - an ARRAY[] expression
 *
610 611 612 613
 * 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.
614 615 616 617 618
 */
typedef struct ArrayExpr
{
	Expr		xpr;
	Oid			array_typeid;	/* type of expression result */
619 620 621
	Oid			element_typeid; /* common type of array elements */
	List	   *elements;		/* the array elements or sub-arrays */
	bool		multidims;		/* true if elements are sub-arrays */
622
} ArrayExpr;
623

624 625
/*
 * RowExpr - a ROW() expression
626 627 628
 *
 * 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 已提交
629
 * to be shorter than the rowtype.	That is, the N'th list element must
630 631
 * 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 已提交
632
 * be a NULL constant.	(This case can only occur for named composite types,
633 634 635
 * 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.
636 637 638 639 640 641
 */
typedef struct RowExpr
{
	Expr		xpr;
	List	   *args;			/* the fields */
	Oid			row_typeid;		/* RECORDOID or a composite type's ID */
B
Bruce Momjian 已提交
642

643
	/*
B
Bruce Momjian 已提交
644 645 646 647
	 * 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.
648 649 650 651
	 */
	CoercionForm row_format;	/* how to display this node */
} RowExpr;

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
/*
 * 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
 * operator in btree opclasses).  Note that the same operator name might
 * 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 */
	List	   *opclasses;		/* OID list of containing operator classes */
	List	   *largs;			/* the left-hand input arguments */
	List	   *rargs;			/* the right-hand input arguments */
} RowCompareExpr;

687 688 689 690 691
/*
 * CoalesceExpr - a COALESCE expression
 */
typedef struct CoalesceExpr
{
B
Bruce Momjian 已提交
692 693 694
	Expr		xpr;
	Oid			coalescetype;	/* type of expression result */
	List	   *args;			/* the arguments */
695
} CoalesceExpr;
696

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
/*
 * 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 */
	MinMaxOp	op;				/* function to execute */
	List	   *args;			/* the arguments */
} MinMaxExpr;

714 715 716 717 718 719 720 721
/*
 * NullIfExpr - a NULLIF expression
 *
 * Like DistinctExpr, this is represented the same as an OpExpr referencing
 * the "=" operator for x and y.
 */
typedef OpExpr NullIfExpr;

722 723 724 725 726
/* ----------------
 * NullTest
 *
 * NullTest represents the operation of testing a value for NULLness.
 * The appropriate test is performed and returned as a boolean Datum.
727 728 729 730 731
 *
 * NOTE: the semantics of this for rowtype inputs are noticeably different
 * from the scalar case.  It would probably be a good idea to include an
 * "argisrow" flag in the struct to reflect that, but for the moment,
 * we do not do so to avoid forcing an initdb during 8.2beta.
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
 * ----------------
 */

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 */
} 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;

/*
769
 * CoerceToDomain
770
 *
771 772
 * 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 已提交
773 774
 * 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
775
 * RelabelType in the scenario where no constraints are applied.
776
 */
777
typedef struct CoerceToDomain
778 779 780
{
	Expr		xpr;
	Expr	   *arg;			/* input expression */
781 782
	Oid			resulttype;		/* domain type ID (result type) */
	int32		resulttypmod;	/* output typmod (currently always -1) */
B
Bruce Momjian 已提交
783
	CoercionForm coercionformat;	/* how to display this node */
784
} CoerceToDomain;
785 786

/*
787
 * Placeholder node for the value to be processed by a domain's check
B
Bruce Momjian 已提交
788
 * constraint.	This is effectively like a Param, but can be implemented more
789 790 791 792 793
 * simply since we need only one replacement value at a time.
 *
 * Note: the typeId/typeMod 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.
794
 */
795
typedef struct CoerceToDomainValue
796 797
{
	Expr		xpr;
798 799
	Oid			typeId;			/* type for substituted value */
	int32		typeMod;		/* typemod for substituted value */
800
} CoerceToDomainValue;
801

802 803 804 805
/*
 * 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 已提交
806
 * column default expression during rewriting.	But it is convenient to
807 808 809 810 811 812 813
 * 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 */
814
} SetToDefault;
815

816
/*--------------------
817 818 819 820 821 822 823 824
 * 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.
 *
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
 * 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!
 *
 * ressortgroupref is used in the representation of ORDER BY and
 * GROUP BY items.	Targetlist entries with ressortgroupref=0 are not
 * sort/group items.  If ressortgroupref>0, then this item is an ORDER BY or
 * GROUP BY value.	No two entries in a targetlist 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 SortClause or GroupClause lists determine the semantics.
 *
 * 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.
 *--------------------
869 870 871 872 873
 */
typedef struct TargetEntry
{
	Expr		xpr;
	Expr	   *expr;			/* expression to evaluate */
874 875 876 877 878 879
	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 已提交
880 881
	bool		resjunk;		/* set to true to eliminate the attribute from
								 * final target list */
882 883
} TargetEntry;

884 885 886 887 888 889

/* ----------------------------------------------------------------
 *					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
890 891 892
 * 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
893
 * may have any number of child nodes, not just two.
894 895 896
 *
 * 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.
897 898
 *
 * NOTE: the qualification expressions present in JoinExpr nodes are
899
 * *in addition to* the query's main WHERE clause, which appears as the
B
Bruce Momjian 已提交
900
 * qual of the top-level FromExpr.	The reason for associating quals with
901 902 903 904 905
 * 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.
906
 *
907 908 909 910 911
 * 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.
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
 * ----------------------------------------------------------------
 */

/*
 * 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
 *
B
Bruce Momjian 已提交
931
 * isNatural, using, and quals are interdependent.	The user can write only
932 933 934 935
 * 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.
B
Bruce Momjian 已提交
936
 * If he writes ON() then only "quals" is set.	Note that NATURAL/USING
937 938
 * are not equivalent to ON() since they also affect the output column list.
 *
939
 * alias is an Alias node representing the AS alias-clause attached to the
940 941 942 943
 * 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.
 *
944
 * During parse analysis, an RTE is created for the Join, and its index
B
Bruce Momjian 已提交
945
 * is filled into rtindex.	This RTE is present mainly so that Vars can
946
 * be created that refer to the outputs of the join.
947 948 949 950 951 952 953 954 955 956 957
 *----------
 */
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 */
958
	Alias	   *alias;			/* user-written alias clause, if any */
959
	int			rtindex;		/* RT index assigned for join */
960 961
} JoinExpr;

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
/*----------
 * 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;
977

978
#endif   /* PRIMNODES_H */