]> granicus.if.org Git - postgresql/blob - src/include/nodes/nodes.h
Implement SEMI and ANTI joins in the planner and executor. (Semijoins replace
[postgresql] / src / include / nodes / nodes.h
1 /*-------------------------------------------------------------------------
2  *
3  * nodes.h
4  *        Definitions for tagged nodes.
5  *
6  *
7  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/include/nodes/nodes.h,v 1.208 2008/08/14 18:48:00 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef NODES_H
15 #define NODES_H
16
17 /*
18  * The first field of every node is NodeTag. Each node created (with makeNode)
19  * will have one of the following tags as the value of its first field.
20  *
21  * Note that the numbers of the node tags are not contiguous. We left holes
22  * here so that we can add more tags without changing the existing enum's.
23  * (Since node tag numbers never exist outside backend memory, there's no
24  * real harm in renumbering, it just costs a full rebuild ...)
25  */
26 typedef enum NodeTag
27 {
28         T_Invalid = 0,
29
30         /*
31          * TAGS FOR EXECUTOR NODES (execnodes.h)
32          */
33         T_IndexInfo = 10,
34         T_ExprContext,
35         T_ProjectionInfo,
36         T_JunkFilter,
37         T_ResultRelInfo,
38         T_EState,
39         T_TupleTableSlot,
40
41         /*
42          * TAGS FOR PLAN NODES (plannodes.h)
43          */
44         T_Plan = 100,
45         T_Result,
46         T_Append,
47         T_BitmapAnd,
48         T_BitmapOr,
49         T_Scan,
50         T_SeqScan,
51         T_IndexScan,
52         T_BitmapIndexScan,
53         T_BitmapHeapScan,
54         T_TidScan,
55         T_SubqueryScan,
56         T_FunctionScan,
57         T_ValuesScan,
58         T_Join,
59         T_NestLoop,
60         T_MergeJoin,
61         T_HashJoin,
62         T_Material,
63         T_Sort,
64         T_Group,
65         T_Agg,
66         T_Unique,
67         T_Hash,
68         T_SetOp,
69         T_Limit,
70
71         /*
72          * TAGS FOR PLAN STATE NODES (execnodes.h)
73          *
74          * These should correspond one-to-one with Plan node types.
75          */
76         T_PlanState = 200,
77         T_ResultState,
78         T_AppendState,
79         T_BitmapAndState,
80         T_BitmapOrState,
81         T_ScanState,
82         T_SeqScanState,
83         T_IndexScanState,
84         T_BitmapIndexScanState,
85         T_BitmapHeapScanState,
86         T_TidScanState,
87         T_SubqueryScanState,
88         T_FunctionScanState,
89         T_ValuesScanState,
90         T_JoinState,
91         T_NestLoopState,
92         T_MergeJoinState,
93         T_HashJoinState,
94         T_MaterialState,
95         T_SortState,
96         T_GroupState,
97         T_AggState,
98         T_UniqueState,
99         T_HashState,
100         T_SetOpState,
101         T_LimitState,
102
103         /*
104          * TAGS FOR PRIMITIVE NODES (primnodes.h)
105          */
106         T_Alias = 300,
107         T_RangeVar,
108         T_Expr,
109         T_Var,
110         T_Const,
111         T_Param,
112         T_Aggref,
113         T_ArrayRef,
114         T_FuncExpr,
115         T_OpExpr,
116         T_DistinctExpr,
117         T_ScalarArrayOpExpr,
118         T_BoolExpr,
119         T_SubLink,
120         T_SubPlan,
121         T_FieldSelect,
122         T_FieldStore,
123         T_RelabelType,
124         T_CoerceViaIO,
125         T_ArrayCoerceExpr,
126         T_ConvertRowtypeExpr,
127         T_CaseExpr,
128         T_CaseWhen,
129         T_CaseTestExpr,
130         T_ArrayExpr,
131         T_RowExpr,
132         T_RowCompareExpr,
133         T_CoalesceExpr,
134         T_MinMaxExpr,
135         T_XmlExpr,
136         T_NullIfExpr,
137         T_NullTest,
138         T_BooleanTest,
139         T_CoerceToDomain,
140         T_CoerceToDomainValue,
141         T_SetToDefault,
142         T_CurrentOfExpr,
143         T_TargetEntry,
144         T_RangeTblRef,
145         T_JoinExpr,
146         T_FromExpr,
147         T_IntoClause,
148
149         /*
150          * TAGS FOR EXPRESSION STATE NODES (execnodes.h)
151          *
152          * These correspond (not always one-for-one) to primitive nodes derived
153          * from Expr.
154          */
155         T_ExprState = 400,
156         T_GenericExprState,
157         T_AggrefExprState,
158         T_ArrayRefExprState,
159         T_FuncExprState,
160         T_ScalarArrayOpExprState,
161         T_BoolExprState,
162         T_SubPlanState,
163         T_FieldSelectState,
164         T_FieldStoreState,
165         T_CoerceViaIOState,
166         T_ArrayCoerceExprState,
167         T_ConvertRowtypeExprState,
168         T_CaseExprState,
169         T_CaseWhenState,
170         T_ArrayExprState,
171         T_RowExprState,
172         T_RowCompareExprState,
173         T_CoalesceExprState,
174         T_MinMaxExprState,
175         T_XmlExprState,
176         T_NullTestState,
177         T_CoerceToDomainState,
178         T_DomainConstraintState,
179
180         /*
181          * TAGS FOR PLANNER NODES (relation.h)
182          */
183         T_PlannerInfo = 500,
184         T_PlannerGlobal,
185         T_RelOptInfo,
186         T_IndexOptInfo,
187         T_Path,
188         T_IndexPath,
189         T_BitmapHeapPath,
190         T_BitmapAndPath,
191         T_BitmapOrPath,
192         T_NestPath,
193         T_MergePath,
194         T_HashPath,
195         T_TidPath,
196         T_AppendPath,
197         T_ResultPath,
198         T_MaterialPath,
199         T_UniquePath,
200         T_EquivalenceClass,
201         T_EquivalenceMember,
202         T_PathKey,
203         T_RestrictInfo,
204         T_InnerIndexscanInfo,
205         T_FlattenedSubLink,
206         T_SpecialJoinInfo,
207         T_AppendRelInfo,
208         T_PlannerParamItem,
209
210         /*
211          * TAGS FOR MEMORY NODES (memnodes.h)
212          */
213         T_MemoryContext = 600,
214         T_AllocSetContext,
215
216         /*
217          * TAGS FOR VALUE NODES (value.h)
218          */
219         T_Value = 650,
220         T_Integer,
221         T_Float,
222         T_String,
223         T_BitString,
224         T_Null,
225
226         /*
227          * TAGS FOR LIST NODES (pg_list.h)
228          */
229         T_List,
230         T_IntList,
231         T_OidList,
232
233         /*
234          * TAGS FOR STATEMENT NODES (mostly in parsenodes.h)
235          */
236         T_Query = 700,
237         T_PlannedStmt,
238         T_InsertStmt,
239         T_DeleteStmt,
240         T_UpdateStmt,
241         T_SelectStmt,
242         T_AlterTableStmt,
243         T_AlterTableCmd,
244         T_AlterDomainStmt,
245         T_SetOperationStmt,
246         T_GrantStmt,
247         T_GrantRoleStmt,
248         T_ClosePortalStmt,
249         T_ClusterStmt,
250         T_CopyStmt,
251         T_CreateStmt,
252         T_DefineStmt,
253         T_DropStmt,
254         T_TruncateStmt,
255         T_CommentStmt,
256         T_FetchStmt,
257         T_IndexStmt,
258         T_CreateFunctionStmt,
259         T_AlterFunctionStmt,
260         T_RemoveFuncStmt,
261         T_RenameStmt,
262         T_RuleStmt,
263         T_NotifyStmt,
264         T_ListenStmt,
265         T_UnlistenStmt,
266         T_TransactionStmt,
267         T_ViewStmt,
268         T_LoadStmt,
269         T_CreateDomainStmt,
270         T_CreatedbStmt,
271         T_DropdbStmt,
272         T_VacuumStmt,
273         T_ExplainStmt,
274         T_CreateSeqStmt,
275         T_AlterSeqStmt,
276         T_VariableSetStmt,
277         T_VariableShowStmt,
278         T_DiscardStmt,
279         T_CreateTrigStmt,
280         T_DropPropertyStmt,
281         T_CreatePLangStmt,
282         T_DropPLangStmt,
283         T_CreateRoleStmt,
284         T_AlterRoleStmt,
285         T_DropRoleStmt,
286         T_LockStmt,
287         T_ConstraintsSetStmt,
288         T_ReindexStmt,
289         T_CheckPointStmt,
290         T_CreateSchemaStmt,
291         T_AlterDatabaseStmt,
292         T_AlterDatabaseSetStmt,
293         T_AlterRoleSetStmt,
294         T_CreateConversionStmt,
295         T_CreateCastStmt,
296         T_DropCastStmt,
297         T_CreateOpClassStmt,
298         T_CreateOpFamilyStmt,
299         T_AlterOpFamilyStmt,
300         T_RemoveOpClassStmt,
301         T_RemoveOpFamilyStmt,
302         T_PrepareStmt,
303         T_ExecuteStmt,
304         T_DeallocateStmt,
305         T_DeclareCursorStmt,
306         T_CreateTableSpaceStmt,
307         T_DropTableSpaceStmt,
308         T_AlterObjectSchemaStmt,
309         T_AlterOwnerStmt,
310         T_DropOwnedStmt,
311         T_ReassignOwnedStmt,
312         T_CompositeTypeStmt,
313         T_CreateEnumStmt,
314         T_AlterTSDictionaryStmt,
315         T_AlterTSConfigurationStmt,
316
317         /*
318          * TAGS FOR PARSE TREE NODES (parsenodes.h)
319          */
320         T_A_Expr = 900,
321         T_ColumnRef,
322         T_ParamRef,
323         T_A_Const,
324         T_FuncCall,
325         T_A_Indices,
326         T_A_Indirection,
327         T_A_ArrayExpr,
328         T_ResTarget,
329         T_TypeCast,
330         T_SortBy,
331         T_RangeSubselect,
332         T_RangeFunction,
333         T_TypeName,
334         T_ColumnDef,
335         T_IndexElem,
336         T_Constraint,
337         T_DefElem,
338         T_RangeTblEntry,
339         T_SortGroupClause,
340         T_FkConstraint,
341         T_PrivGrantee,
342         T_FuncWithArgs,
343         T_PrivTarget,
344         T_CreateOpClassItem,
345         T_InhRelation,
346         T_FunctionParameter,
347         T_LockingClause,
348         T_RowMarkClause,
349         T_XmlSerialize,
350
351         /*
352          * TAGS FOR RANDOM OTHER STUFF
353          *
354          * These are objects that aren't part of parse/plan/execute node tree
355          * structures, but we give them NodeTags anyway for identification
356          * purposes (usually because they are involved in APIs where we want to
357          * pass multiple object types through the same pointer).
358          */
359         T_TriggerData = 950,            /* in commands/trigger.h */
360         T_ReturnSetInfo,                        /* in nodes/execnodes.h */
361         T_TIDBitmap                                     /* in nodes/tidbitmap.h */
362 } NodeTag;
363
364 /*
365  * The first field of a node of any type is guaranteed to be the NodeTag.
366  * Hence the type of any node can be gotten by casting it to Node. Declaring
367  * a variable to be of Node * (instead of void *) can also facilitate
368  * debugging.
369  */
370 typedef struct Node
371 {
372         NodeTag         type;
373 } Node;
374
375 #define nodeTag(nodeptr)                (((Node*)(nodeptr))->type)
376
377 /*
378  * newNode -
379  *        create a new node of the specified size and tag the node with the
380  *        specified tag.
381  *
382  * !WARNING!: Avoid using newNode directly. You should be using the
383  *        macro makeNode.  eg. to create a Query node, use makeNode(Query)
384  *
385  *      There is no way to dereference the palloc'ed pointer to assign the
386  *      tag, and also return the pointer itself, so we need a holder variable.
387  *      Fortunately, this macro isn't recursive so we just define
388  *      a global variable for this purpose.
389  */
390 extern PGDLLIMPORT Node *newNodeMacroHolder;
391
392 #define newNode(size, tag) \
393 ( \
394         AssertMacro((size) >= sizeof(Node)),            /* need the tag, at least */ \
395         newNodeMacroHolder = (Node *) palloc0fast(size), \
396         newNodeMacroHolder->type = (tag), \
397         newNodeMacroHolder \
398 )
399
400
401 #define makeNode(_type_)                ((_type_ *) newNode(sizeof(_type_),T_##_type_))
402 #define NodeSetTag(nodeptr,t)   (((Node*)(nodeptr))->type = (t))
403
404 #define IsA(nodeptr,_type_)             (nodeTag(nodeptr) == T_##_type_)
405
406 /* ----------------------------------------------------------------
407  *                                        extern declarations follow
408  * ----------------------------------------------------------------
409  */
410
411 /*
412  * nodes/{outfuncs.c,print.c}
413  */
414 extern char *nodeToString(void *obj);
415
416 /*
417  * nodes/{readfuncs.c,read.c}
418  */
419 extern void *stringToNode(char *str);
420
421 /*
422  * nodes/copyfuncs.c
423  */
424 extern void *copyObject(void *obj);
425
426 /*
427  * nodes/equalfuncs.c
428  */
429 extern bool equal(void *a, void *b);
430
431
432 /*
433  * Typedefs for identifying qualifier selectivities and plan costs as such.
434  * These are just plain "double"s, but declaring a variable as Selectivity
435  * or Cost makes the intent more obvious.
436  *
437  * These could have gone into plannodes.h or some such, but many files
438  * depend on them...
439  */
440 typedef double Selectivity;             /* fraction of tuples a qualifier will pass */
441 typedef double Cost;                    /* execution cost (in page-access units) */
442
443
444 /*
445  * CmdType -
446  *        enums for type of operation represented by a Query or PlannedStmt
447  *
448  * This is needed in both parsenodes.h and plannodes.h, so put it here...
449  */
450 typedef enum CmdType
451 {
452         CMD_UNKNOWN,
453         CMD_SELECT,                                     /* select stmt */
454         CMD_UPDATE,                                     /* update stmt */
455         CMD_INSERT,                                     /* insert stmt */
456         CMD_DELETE,
457         CMD_UTILITY,                            /* cmds like create, destroy, copy, vacuum,
458                                                                  * etc. */
459         CMD_NOTHING                                     /* dummy command for instead nothing rules
460                                                                  * with qual */
461 } CmdType;
462
463
464 /*
465  * JoinType -
466  *        enums for types of relation joins
467  *
468  * JoinType determines the exact semantics of joining two relations using
469  * a matching qualification.  For example, it tells what to do with a tuple
470  * that has no match in the other relation.
471  *
472  * This is needed in both parsenodes.h and plannodes.h, so put it here...
473  */
474 typedef enum JoinType
475 {
476         /*
477          * The canonical kinds of joins according to the SQL JOIN syntax.
478          * Only these codes can appear in parser output (e.g., JoinExpr nodes).
479          */
480         JOIN_INNER,                                     /* matching tuple pairs only */
481         JOIN_LEFT,                                      /* pairs + unmatched LHS tuples */
482         JOIN_FULL,                                      /* pairs + unmatched LHS + unmatched RHS */
483         JOIN_RIGHT,                                     /* pairs + unmatched RHS tuples */
484
485         /*
486          * Semijoins and anti-semijoins (as defined in relational theory) do
487          * not appear in the SQL JOIN syntax, but there are standard idioms for
488          * representing them (e.g., using EXISTS).  The planner recognizes these
489          * cases and converts them to joins.  So the planner and executor must
490          * support these codes.  NOTE: in JOIN_SEMI output, it is unspecified
491          * which matching RHS row is joined to.  In JOIN_ANTI output, the row
492          * is guaranteed to be null-extended.
493          */
494         JOIN_SEMI,                                      /* 1 copy of each LHS row that has match(es) */
495         JOIN_ANTI,                                      /* 1 copy of each LHS row that has no match */
496
497         /*
498          * These codes are used internally in the planner, but are not supported
499          * by the executor (nor, indeed, by most of the planner).
500          */
501         JOIN_UNIQUE_OUTER,                      /* LHS path must be made unique */
502         JOIN_UNIQUE_INNER                       /* RHS path must be made unique */
503
504         /*
505          * We might need additional join types someday.
506          */
507 } JoinType;
508
509 /*
510  * OUTER joins are those for which pushed-down quals must behave differently
511  * from the join's own quals.  This is in fact everything except INNER joins.
512  * However, this macro must also exclude the JOIN_UNIQUE symbols since those
513  * are temporary proxies for what will eventually be an INNER join.
514  *
515  * Note: in some places it is preferable to treat JOIN_SEMI as not being
516  * an outer join, since it doesn't produce null-extended rows.  Be aware
517  * of that distinction when deciding whether to use this macro.
518  */
519 #define IS_OUTER_JOIN(jointype) \
520         ((jointype) > JOIN_INNER && (jointype) < JOIN_UNIQUE_OUTER)
521
522 #endif   /* NODES_H */