Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4ce24c8

Browse files
committed
UNION work for UNION ALL and other union stuff.
1 parent2730c4a commit4ce24c8

File tree

8 files changed

+178
-84
lines changed

8 files changed

+178
-84
lines changed

‎src/backend/executor/nodeAppend.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.9 1997/09/08 21:43:10 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.10 1997/12/27 06:40:50 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -34,7 +34,7 @@
3434
* nilnil ...... ...
3535
* subplans
3636
*
37-
*Append nodes are currently used to support inheritance
37+
*Append nodes are currently used tounions, and tosupport inheritance
3838
*queries, where several relations need to be scanned.
3939
*For example, in our standard person/student/employee/student-emp
4040
*example, where student and employee inherit from person
@@ -85,6 +85,7 @@ exec_append_initialize_next(Append *node)
8585

8686
intwhichplan;
8787
intnplans;
88+
List*rts;
8889
List*rtentries;
8990
ResTarget*rtentry;
9091

@@ -101,6 +102,7 @@ exec_append_initialize_next(Append *node)
101102

102103
whichplan=unionstate->as_whichplan;
103104
nplans=unionstate->as_nplans;
105+
rts=node->unionrts;
104106
rtentries=node->unionrtentries;
105107

106108
if (whichplan<0)
@@ -140,27 +142,28 @@ exec_append_initialize_next(Append *node)
140142
if (node->unionrelid>0)
141143
{
142144
rtentry=nth(whichplan,rtentries);
143-
if (rtentry==NULL)
144-
elog(DEBUG,"exec_append_initialize_next: rtentry is nil");
145+
Assert(rtentry!=NULL);
145146

146147
unionrelid=node->unionrelid;
147148

148149
rt_store(unionrelid,rangeTable,rtentry);
149-
150-
if (unionstate->as_junkFilter_list)
151-
{
152-
estate->es_junkFilter=
153-
(JunkFilter*)nth(whichplan,
154-
unionstate->as_junkFilter_list);
155-
}
156-
if (unionstate->as_result_relation_info_list)
157-
{
158-
estate->es_result_relation_info=
159-
(RelationInfo*)nth(whichplan,
160-
unionstate->as_result_relation_info_list);
161-
}
162-
result_slot->ttc_whichplan=whichplan;
163150
}
151+
else
152+
estate->es_range_table=nth(whichplan,rts);
153+
154+
if (unionstate->as_junkFilter_list)
155+
{
156+
estate->es_junkFilter=
157+
(JunkFilter*)nth(whichplan,
158+
unionstate->as_junkFilter_list);
159+
}
160+
if (unionstate->as_result_relation_info_list)
161+
{
162+
estate->es_result_relation_info=
163+
(RelationInfo*)nth(whichplan,
164+
unionstate->as_result_relation_info_list);
165+
}
166+
result_slot->ttc_whichplan=whichplan;
164167

165168
return TRUE;
166169
}
@@ -439,8 +442,7 @@ ExecProcAppend(Append *node)
439442
if (exec_append_initialize_next(node))
440443
{
441444
ExecSetSlotDescriptorIsNew(result_slot, true);
442-
return
443-
ExecProcAppend(node);
445+
returnExecProcAppend(node);
444446
}
445447
else
446448
returnExecClearTuple(result_slot);

‎src/backend/nodes/outfuncs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.13 1997/12/23 19:50:54thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.14 1997/12/27 06:40:54momjian Exp $
1111
*
1212
* NOTES
1313
* Every (plan) node in POSTGRES has an associated "out" routine which
@@ -194,7 +194,7 @@ _outQuery(StringInfo str, Query *node)
194194
sprintf(buf," :qual ");
195195
appendStringInfo(str,buf);
196196
_outNode(str,node->qual);
197-
197+
/* how are we handling aggregates, sort, and group by? bjm 1997/12/26 */
198198
}
199199

200200
/*

‎src/backend/nodes/readfuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.11 1997/12/18 12:53:59 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.12 1997/12/27 06:40:59 momjian Exp $
1111
*
1212
* NOTES
1313
* Most of the read functions for plan nodes are tested. (In fact, they
@@ -119,6 +119,7 @@ _readQuery()
119119

120120
token=lsptok(NULL,&length);/* skip :qual */
121121
local_node->qual=nodeRead(true);
122+
/* how are we handling aggregates, sort, and group by? bjm 1997/12/26 */
122123

123124
return (local_node);
124125
}

‎src/backend/optimizer/plan/planner.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.16 1997/12/24 06:06:01 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.17 1997/12/27 06:41:07 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -72,8 +72,6 @@ planner(Query *parse)
7272
{
7373
List*tlist=parse->targetList;
7474
List*rangetable=parse->rtable;
75-
char*uniqueflag=parse->uniqueFlag;
76-
List*sortclause=parse->sortClause;
7775

7876
Plan*result_plan= (Plan*)NULL;
7977

@@ -83,7 +81,7 @@ planner(Query *parse)
8381

8482
if (parse->unionClause)
8583
{
86-
result_plan= (Plan*)plan_union_queries(0,/* none */
84+
result_plan= (Plan*)plan_union_queries(0,/* none */
8785
parse,
8886
UNION_FLAG);
8987
/* XXX do we need to do this? bjm 12/19/97 */
@@ -173,16 +171,16 @@ planner(Query *parse)
173171
* the optimization step later.
174172
*/
175173

176-
if (uniqueflag)
174+
if (parse->uniqueFlag)
177175
{
178-
Plan*sortplan=make_sortplan(tlist,sortclause,result_plan);
176+
Plan*sortplan=make_sortplan(tlist,parse->sortClause,result_plan);
179177

180-
return ((Plan*)make_unique(tlist,sortplan,uniqueflag));
178+
return ((Plan*)make_unique(tlist,sortplan,parse->uniqueFlag));
181179
}
182180
else
183181
{
184-
if (sortclause)
185-
return (make_sortplan(tlist,sortclause,result_plan));
182+
if (parse->sortClause)
183+
return (make_sortplan(tlist,parse->sortClause,result_plan));
186184
else
187185
return ((Plan*)result_plan);
188186
}

‎src/backend/optimizer/prep/prepunion.c

Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.14 1997/12/26 06:02:26 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.15 1997/12/27 06:41:17 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -23,6 +23,7 @@
2323
#include"nodes/relation.h"
2424

2525
#include"parser/parsetree.h"
26+
#include"parser/parse_clause.h"
2627

2728
#include"utils/elog.h"
2829
#include"utils/lsyscache.h"
@@ -42,7 +43,7 @@ static Query *subst_rangetable(Query *root, Index index,
4243
RangeTblEntry*new_entry);
4344
staticvoidfix_parsetree_attnums(Indexrt_index,Oidold_relid,
4445
Oidnew_relid,Query*parsetree);
45-
staticAppend*make_append(List*unionplans,Indexrt_index,
46+
staticAppend*make_append(List*unionplans,List*unionrts,Indexrt_index,
4647
List*union_rt_entries,List*tlist);
4748

4849

@@ -136,73 +137,102 @@ plan_union_queries(Index rt_index,
136137
Query*parse,
137138
UnionFlagflag)
138139
{
139-
List*rangetable=parse->rtable;
140-
RangeTblEntry*rt_entry=rt_fetch(rt_index,rangetable);
141-
List*union_relids=NIL;
142140
List*union_plans=NIL;
143-
List*union_rt_entries=NIL;
144141

145142
switch (flag)
146143
{
147144
caseINHERITS_FLAG:
148-
union_relids=
149-
find_all_inheritors(lconsi(rt_entry->relid,
150-
NIL),
151-
NIL);
152-
/*
153-
* Remove the flag for this relation, since we're about to handle it
154-
* (do it before recursing!). XXX destructive parse tree change
155-
*/
156-
switch (flag)
157145
{
158-
caseINHERITS_FLAG:
159-
rt_fetch(rt_index,rangetable)->inh= false;
160-
break;
161-
default:
162-
break;
163-
}
164-
165-
/*
166-
* XXX - can't find any reason to sort union-relids as paul did, so
167-
* we're leaving it out for now (maybe forever) - jeff & lp
168-
*
169-
* [maybe so. btw, jeff & lp did the lisp conversion, according to Paul.
170-
* -- ay 10/94.]
171-
*/
172-
union_plans=plan_union_query(union_relids,rt_index,rt_entry,
173-
parse,flag,&union_rt_entries);
174-
175-
return (make_append(union_plans,
176-
rt_index,
177-
union_rt_entries,
178-
((Plan*)lfirst(union_plans))->targetlist));
179-
break;
146+
List*rangetable=parse->rtable;
147+
RangeTblEntry*rt_entry=rt_fetch(rt_index,rangetable);
148+
List*union_rt_entries=NIL;
149+
List*union_relids=NIL;
150+
151+
union_relids=
152+
find_all_inheritors(lconsi(rt_entry->relid,
153+
NIL),
154+
NIL);
155+
/*
156+
* Remove the flag for this relation, since we're about to handle it
157+
* (do it before recursing!). XXX destructive parse tree change
158+
*/
159+
switch (flag)
160+
{
161+
caseINHERITS_FLAG:
162+
rt_fetch(rt_index,rangetable)->inh= false;
163+
break;
164+
default:
165+
break;
166+
}
180167

168+
/*
169+
* XXX - can't find any reason to sort union-relids as paul did, so
170+
* we're leaving it out for now (maybe forever) - jeff & lp
171+
*
172+
* [maybe so. btw, jeff & lp did the lisp conversion, according to Paul.
173+
* -- ay 10/94.]
174+
*/
175+
union_plans=plan_union_query(union_relids,rt_index,rt_entry,
176+
parse,flag,&union_rt_entries);
177+
178+
return (make_append(union_plans,
179+
NULL,
180+
rt_index,
181+
union_rt_entries,
182+
((Plan*)lfirst(union_plans))->targetlist));
183+
break;
184+
}
181185
caseUNION_FLAG:
182186
{
183-
List*ulist,*hold_union,*union_plans;
187+
List*ulist,*hold_union,*union_plans,*union_rts;
184188

185189
hold_union=parse->unionClause;
186190
parse->unionClause=NULL;/* prevent looping */
187191

188192
union_plans=lcons(planner(parse),NIL);
189-
193+
union_rts=lcons(parse->rtable,NIL);
190194
foreach(ulist,hold_union)
191-
union_plans=lappend(union_plans,planner(lfirst(ulist)));
192-
return (make_append(union_plans,
193-
rt_index,rangetable,
195+
{
196+
Query*u=lfirst(ulist);
197+
198+
union_plans=lappend(union_plans,planner(u));
199+
union_rts=lappend(union_rts,u->rtable);
200+
}
201+
202+
/* We have already split UNION and UNION ALL */
203+
if (!((Query*)lfirst(hold_union))->unionall)
204+
{
205+
parse->uniqueFlag="*";
206+
parse->sortClause=transformSortClause(NULL,NIL,
207+
((Plan*)lfirst(union_plans))->targetlist,"*");
208+
}
209+
else
210+
{
211+
/* needed so we don't take the flag from the first query */
212+
parse->uniqueFlag=NULL;
213+
parse->sortClause=NIL;
214+
}
215+
216+
parse->havingQual=NULL;
217+
parse->qry_numAgg=0;
218+
parse->qry_aggs=NULL;
219+
220+
return (make_append(union_plans,union_rts,
221+
rt_index/* is 0, none */,NULL,
194222
((Plan*)lfirst(union_plans))->targetlist));
195223
}
196224
break;
197225

226+
#ifdefNOT_USED
198227
caseVERSION_FLAG:
199228
union_relids=VersionGetParents(rt_entry->relid);
200229
break;
201-
230+
#endif
202231
default:
203232
/* do nothing */
204233
break;
205234
}
235+
returnNULL;
206236

207237
return ((Append*)NULL);/* to make gcc happy */
208238
}
@@ -392,13 +422,15 @@ fix_parsetree_attnums(Index rt_index,
392422

393423
staticAppend*
394424
make_append(List*unionplans,
425+
List*unionrts,
395426
Indexrt_index,
396427
List*union_rt_entries,
397428
List*tlist)
398429
{
399430
Append*node=makeNode(Append);
400431

401432
node->unionplans=unionplans;
433+
node->unionrts=unionrts;
402434
node->unionrelid=rt_index;
403435
node->unionrtentries=union_rt_entries;
404436
node->plan.cost=0.0;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp