primnodes.h 27.0 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * primnodes.h
4 5 6 7
 *	  Definitions for "primitive" node types, those that are used in more
 *	  than one of the parse/plan/execute stages of the query pipeline.
 *	  Currently, these are mostly nodes for executable expressions
 *	  and join trees.
8 9
 *
 *
B
Bruce Momjian 已提交
10
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
11
 * Portions Copyright (c) 1994, Regents of the University of California
12
 *
13
 * $Id: primnodes.h,v 1.77 2003/01/10 21:08:15 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
 * Resdom (Result Domain)
 *
32 33
 * Notes:
 * ressortgroupref is the parse/plan-time representation of ORDER BY and
34
 * GROUP BY items.	Targetlist entries with ressortgroupref=0 are not
35
 * sort/group items.  If ressortgroupref>0, then this item is an ORDER BY or
36
 * GROUP BY value.	No two entries in a targetlist may have the same nonzero
37
 * ressortgroupref --- but there is no particular meaning to the nonzero
38 39
 * values, except as tags.	(For example, one must not assume that lower
 * ressortgroupref means a more significant sort key.)	The order of the
40 41 42 43 44
 * associated SortClause or GroupClause lists determine the semantics.
 *
 * reskey and reskeyop are the execution-time representation of sorting.
 * reskey must be zero in any non-sort-key item.  The reskey of sort key
 * targetlist items for a sort plan node is 1,2,...,n for the n sort keys.
45 46
 * The reskeyop of each such targetlist item is the sort operator's OID.
 * reskeyop will be zero in non-sort-key items.
47 48 49
 *
 * Both reskey and reskeyop are typically zero during parse/plan stages.
 * The executor does not pay any attention to ressortgroupref.
50
 *--------------------
51
 */
52 53
typedef struct Resdom
{
B
Bruce Momjian 已提交
54 55 56 57 58
	NodeTag		type;
	AttrNumber	resno;			/* attribute number */
	Oid			restype;		/* type of the value */
	int32		restypmod;		/* type-specific modifier of the value */
	char	   *resname;		/* name of the resdom (could be NULL) */
59
	Index		ressortgroupref;
B
Bruce Momjian 已提交
60 61
	/* nonzero if referenced by a sort/group clause */
	Index		reskey;			/* order of key in a sort (for those > 0) */
62
	Oid			reskeyop;		/* sort operator's Oid */
B
Bruce Momjian 已提交
63 64
	bool		resjunk;		/* set to true to eliminate the attribute
								 * from final target list */
65
} Resdom;
66

67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/*
 * Alias -
 *	  specifies an alias for a range variable; the alias might also
 *	  specify renaming of columns within the table.
 */
typedef struct Alias
{
	NodeTag		type;
	char	   *aliasname;		/* aliased rel name (never qualified) */
	List	   *colnames;		/* optional list of column aliases */
	/* Note: colnames is a list of Value nodes (always strings) */
} 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 已提交
102 103
	InhOption	inhOpt;			/* expand rel by inheritance? recursively
								 * act on children? */
104 105 106 107 108
	bool		istemp;			/* is this a temp relation/sequence? */
	Alias	   *alias;			/* table alias & optional column aliases */
} RangeVar;


109 110 111 112 113
/* ----------------------------------------------------------------
 *					node types for executable expressions
 * ----------------------------------------------------------------
 */

114
/*
115
 * Expr - generic superclass for executable-expression nodes
116
 *
117 118 119 120
 * 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.
121
 */
122 123
typedef struct Expr
{
124
	NodeTag		type;
125
} Expr;
126

127
/*
128
 * Var - expression node representing a variable (ie, a table column)
129 130 131 132 133 134 135 136 137
 *
 * 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.
138
 */
139 140
#define    INNER		65000
#define    OUTER		65001
141

142 143
#define    PRS2_OLD_VARNO			1
#define    PRS2_NEW_VARNO			2
144

145 146
typedef struct Var
{
147
	Expr		xpr;
B
Bruce Momjian 已提交
148 149 150 151 152 153 154 155
	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 */
	Oid			vartype;		/* pg_type tuple OID for the type of this
								 * var */
	int32		vartypmod;		/* pg_attribute typmod value */
156
	Index		varlevelsup;
B
Bruce Momjian 已提交
157 158 159 160 161 162 163

	/*
	 * for subquery variables referencing outer relations; 0 in a normal
	 * var, >0 means N levels up
	 */
	Index		varnoold;		/* original value of varno, for debugging */
	AttrNumber	varoattno;		/* original value of varattno */
164
} Var;
165

166
/*
167 168
 * Const
 */
169 170
typedef struct Const
{
171
	Expr		xpr;
172 173
	Oid			consttype;		/* PG_TYPE OID of the constant's datatype */
	int			constlen;		/* typlen of the constant's datatype */
174 175
	Datum		constvalue;		/* the constant's value */
	bool		constisnull;	/* whether the constant is null (if true,
176 177 178 179 180
								 * constvalue is undefined) */
	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
B
Bruce Momjian 已提交
181
								 * pointer to the information. */
182
} Const;
183 184 185

/* ----------------
 * Param
186 187
 *		paramkind - specifies the kind of parameter. The possible values
 *		for this field are specified in "params.h", and they are:
188
 *
189 190
 *		PARAM_NAMED: The parameter has a name, i.e. something
 *				like `$.salary' or `$.foobar'.
191
 *				In this case field `paramname' must be a valid name.
192
 *
193 194 195
 *		PARAM_NUM:	 The parameter has only a numeric identifier,
 *				i.e. something like `$1', `$2' etc.
 *				The number is contained in the `paramid' field.
196
 *
197 198
 *		PARAM_EXEC:	 The parameter is an internal executor parameter.
 *				It has a number contained in the `paramid' field.
199 200
 * ----------------
 */
201 202
typedef struct Param
{
203
	Expr		xpr;
204 205 206 207
	int			paramkind;		/* kind of parameter. See above */
	AttrNumber	paramid;		/* numeric ID for parameter ("$1") */
	char	   *paramname;		/* name for parameter ("$.foo") */
	Oid			paramtype;		/* PG_TYPE OID of parameter's datatype */
208
} Param;
209

210
/*
B
Bruce Momjian 已提交
211
 * Aggref
212
 */
B
Bruce Momjian 已提交
213
typedef struct Aggref
214
{
215
	Expr		xpr;
216 217
	Oid			aggfnoid;		/* pg_proc Oid of the aggregate */
	Oid			aggtype;		/* type Oid of result of the aggregate */
218
	Expr	   *target;			/* expression we are aggregating on */
B
Bruce Momjian 已提交
219 220
	bool		aggstar;		/* TRUE if argument was really '*' */
	bool		aggdistinct;	/* TRUE if it's agg(DISTINCT ...) */
221
} Aggref;
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/* ----------------
 *	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: array types can be fixed-length (refattrlength > 0), but only
 * when the element type is itself fixed-length.  Otherwise they are
 * varlena structures and have refattrlength = -1.	In any case,
 * an array type is never pass-by-value.
 *
 * Note: refrestype is NOT the element type, but the array type,
 * when doing subarray fetch or either type of store.  It might be a good
 * idea to include a refelemtype field as well.
 * ----------------
 */
typedef struct ArrayRef
{
	Expr		xpr;
	Oid			refrestype;		/* type of the result of the ArrayRef
								 * operation */
	int			refattrlength;	/* typlen of array type */
	int			refelemlength;	/* typlen of the array element type */
	bool		refelembyval;	/* is the element type pass-by-value? */
	char		refelemalign;	/* typalign of the element type */
	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 */
} 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 */
} CoercionContext;

/*
 * 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 */
	COERCE_DONTCARE				/* special case for pathkeys */
} CoercionForm;

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

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

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

/*
 * 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
 * handle any number of arguments.  The parser treats AND and OR as binary
 * 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
} BoolExprType;

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

358 359
/* ----------------
 * SubLink
360
 *
361
 * A SubLink represents a subselect appearing in an expression, and in some
362
 * cases also the combining operator(s) just above it.	The subLinkType
363 364 365 366 367 368 369 370 371 372 373 374 375 376
 * indicates the form of the expression represented:
 *	EXISTS_SUBLINK		EXISTS(SELECT ...)
 *	ALL_SUBLINK			(lefthand) op ALL (SELECT ...)
 *	ANY_SUBLINK			(lefthand) op ANY (SELECT ...)
 *	MULTIEXPR_SUBLINK	(lefthand) op (SELECT ...)
 *	EXPR_SUBLINK		(SELECT with single targetlist item ...)
 * For ALL, ANY, and MULTIEXPR, the lefthand is a list of expressions of the
 * same length as the subselect's targetlist.  MULTIEXPR will *always* have
 * a list with more than one entry; if the subselect has just one target
 * then the parser will create an EXPR_SUBLINK instead (and any operator
 * above the subselect will be represented separately).  Note that both
 * MULTIEXPR and EXPR require the subselect to deliver only one row.
 * ALL, ANY, and MULTIEXPR require the combining operators to deliver boolean
 * results.  These are reduced to one result per row using OR or AND semantics
377
 * depending on the "useOr" flag.  ALL and ANY combine the per-row results
378 379
 * using AND and OR semantics respectively.
 *
380
 * SubLink is classed as an Expr node, but it is not actually executable;
381
 * it must be replaced in the expression tree by a SubPlan node during
382 383
 * planning.
 *
384 385 386
 * NOTE: in the raw output of gram.y, lefthand contains a list of raw
 * expressions; useOr and operOids are not filled in yet.  Also, subselect
 * is a raw parsetree.  During parse analysis, the parser transforms the
387
 * lefthand expression list using normal expression transformation rules.
388 389 390 391
 * It fills operOids with the OIDs representing the specific operator(s)
 * to apply to each pair of lefthand and targetlist expressions.
 * And subselect is transformed to a Query.  This is the representation
 * seen in saved rules and in the rewriter.
392
 *
393 394 395
 * In EXISTS and EXPR SubLinks, lefthand, operName, and operOids are unused
 * and are always NIL.  useOr is not significant either for these sublink
 * types.
396 397 398 399
 * ----------------
 */
typedef enum SubLinkType
{
400
	EXISTS_SUBLINK, ALL_SUBLINK, ANY_SUBLINK, MULTIEXPR_SUBLINK, EXPR_SUBLINK
401 402 403 404 405
} SubLinkType;


typedef struct SubLink
{
406
	Expr		xpr;
B
Bruce Momjian 已提交
407
	SubLinkType subLinkType;	/* EXISTS, ALL, ANY, MULTIEXPR, EXPR */
408
	bool		useOr;			/* TRUE to combine column results with
B
Bruce Momjian 已提交
409 410 411
								 * "OR" not "AND" */
	List	   *lefthand;		/* list of outer-query expressions on the
								 * left */
412 413
	List	   *operName;		/* originally specified operator name */
	List	   *operOids;		/* OIDs of actual combining operators */
B
Bruce Momjian 已提交
414
	Node	   *subselect;		/* subselect as Query* or parsetree */
415 416
} SubLink;

417
/*
418 419 420 421
 * 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
422 423 424 425 426 427 428 429 430 431
 * a sub-plantree and rtable instead of a sub-Query.
 *
 * In an ordinary subplan, "exprs" points to a list of executable expressions
 * (OpExpr trees) for the combining operators; their left-hand arguments are
 * the original lefthand expressions, and their right-hand arguments are
 * PARAM_EXEC Param nodes representing the outputs of the sub-select.
 * (NOTE: runtime coercion functions may be inserted as well.)  But if the
 * sub-select becomes an initplan rather than a subplan, these executable
 * expressions are part of the outer plan's expression tree (and the SubPlan
 * node itself is not).  In this case "exprs" is NIL to avoid duplication.
432
 *
433 434 435 436 437 438 439
 * The planner also derives lists of the values that need to be passed into
 * and out of the subplan.  Input values are represented as a list "args" of
 * 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
 * (the parParam and args lists must have the same length).  setParam is a
 * list of the PARAM_EXEC params that are computed by the sub-select, if it
440
 * is an initplan.
441
 */
442
typedef struct SubPlan
443
{
444
	Expr		xpr;
445 446
	/* Fields copied from original SubLink: */
	SubLinkType subLinkType;	/* EXISTS, ALL, ANY, MULTIEXPR, EXPR */
447
	bool		useOr;			/* TRUE to combine column results with
448
								 * "OR" not "AND" */
449 450 451
	/* The combining operators, transformed to executable expressions: */
	List	   *exprs;			/* list of OpExpr expression trees */
	List	   *paramIds;		/* IDs of Params embedded in the above */
452
	/* The subselect, transformed to a Plan: */
453 454 455 456 457 458
	struct Plan *plan;			/* subselect plan itself */
	int			plan_id;		/* dummy thing because of we haven't equal
								 * funcs for plan nodes... actually, we
								 * could put *plan itself somewhere else
								 * (TopPlan node ?)... */
	List	   *rtable;			/* range table for subselect */
459 460 461 462 463 464
	/* Information about execution strategy: */
	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 */
465
	/* Information for passing params into and out of the subselect: */
466
	/* setParam and parParam are lists of integers (param IDs) */
467 468
	List	   *setParam;		/* initplan subqueries have to set these
								 * Params for parent plan */
469 470
	List	   *parParam;		/* indices of input Params from parent plan */
	List	   *args;			/* exprs to pass as parParam values */
471
} SubPlan;
472

473 474 475 476 477 478 479 480 481 482 483 484
/* ----------------
 * FieldSelect
 *
 * FieldSelect represents the operation of extracting one field from a tuple
 * value.  At runtime, the input expression is expected to yield a Datum
 * that contains a pointer-to-TupleTableSlot.  The specified field number
 * is extracted and returned as a Datum.
 * ----------------
 */

typedef struct FieldSelect
{
485 486
	Expr		xpr;
	Expr	   *arg;			/* input expression */
487 488 489 490
	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) */
491 492
} FieldSelect;

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
/* ----------------
 * 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
{
508 509
	Expr		xpr;
	Expr	   *arg;			/* input expression */
510 511
	Oid			resulttype;		/* output type of coercion expression */
	int32		resulttypmod;	/* output typmod (usually -1) */
512
	CoercionForm relabelformat;	/* how to display this node */
513 514
} RelabelType;

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
/*
 * CaseExpr - a CASE expression
 */
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;

/*
 * CaseWhen - an argument to a CASE expression
 */
typedef struct CaseWhen
{
	Expr		xpr;
	Expr	   *expr;			/* condition expression */
	Expr	   *result;			/* substitution result */
} CaseWhen;

/* ----------------
 * NullTest
 *
 * NullTest represents the operation of testing a value for NULLness.
 * Currently, we only support scalar input values, but eventually a
 * row-constructor input should be supported.
 * The appropriate test is performed and returned as a boolean Datum.
 * ----------------
 */

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;

/*
 * ConstraintTest
 *
 * ConstraintTest represents the operation of testing a value to see whether
 * it meets a constraint.  If so, the input value is returned as the result;
 * if not, an error is raised.
 */

typedef enum ConstraintTestType
{
	CONSTR_TEST_NOTNULL,
	CONSTR_TEST_CHECK
} ConstraintTestType;

typedef struct ConstraintTest
{
	Expr		xpr;
	Expr	   *arg;			/* input expression */
	ConstraintTestType testtype;	/* test type */
	char	   *name;			/* name of constraint (for error msgs) */
	char	   *domname; 		/* name of domain (for error messages) */
	Expr	   *check_expr;		/* for CHECK test, a boolean expression */
} ConstraintTest;

/*
605 606 607 608 609 610 611
 * Placeholder node for the value to be processed by a domain's check
 * constraint.  This is effectively like a Param, but can be implemented more
 * 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.
612 613 614 615
 */
typedef struct ConstraintTestValue
{
	Expr		xpr;
616 617
	Oid			typeId;			/* type for substituted value */
	int32		typeMod;		/* typemod for substituted value */
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
} ConstraintTestValue;


/*
 * 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.
 *
 * The separation between TargetEntry and Resdom is historical.  One of these
 * days, Resdom should probably get folded into TargetEntry.
 */
typedef struct TargetEntry
{
	Expr		xpr;
	Resdom	   *resdom;			/* descriptor for targetlist item */
	Expr	   *expr;			/* expression to evaluate */
} TargetEntry;

640 641 642 643 644 645

/* ----------------------------------------------------------------
 *					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
646 647 648 649 650 651 652 653 654 655 656 657
 * 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
 * may have any number of child nodes, not just two.  Also, there is an
 * implementation-defined difference: the planner is allowed to join the
 * children of a FromExpr using whatever join order seems good to it.
 * At present, JoinExpr nodes are always joined in exactly the order
 * implied by the jointree structure (except the planner may choose to
 * swap inner and outer members of a join pair).
 *
 * 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.
658 659
 *
 * NOTE: the qualification expressions present in JoinExpr nodes are
660
 * *in addition to* the query's main WHERE clause, which appears as the
B
Bruce Momjian 已提交
661
 * qual of the top-level FromExpr.	The reason for associating quals with
662 663 664 665 666
 * 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.
667
 *
668 669 670 671 672
 * 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.
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
 * ----------------------------------------------------------------
 */

/*
 * 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 已提交
692
 * isNatural, using, and quals are interdependent.	The user can write only
693 694 695 696
 * 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 已提交
697
 * If he writes ON() then only "quals" is set.	Note that NATURAL/USING
698 699
 * are not equivalent to ON() since they also affect the output column list.
 *
700
 * alias is an Alias node representing the AS alias-clause attached to the
701 702 703 704
 * 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.
 *
705
 * During parse analysis, an RTE is created for the Join, and its index
B
Bruce Momjian 已提交
706
 * is filled into rtindex.	This RTE is present mainly so that Vars can
707
 * be created that refer to the outputs of the join.
708 709 710 711 712 713 714 715 716 717 718
 *----------
 */
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 */
719
	Alias	   *alias;			/* user-written alias clause, if any */
720
	int			rtindex;		/* RT index assigned for join */
721 722
} JoinExpr;

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
/*----------
 * 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;
738

739
#endif   /* PRIMNODES_H */