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

Commit6231e16

Browse files
committed
Implementation of UNIONs.
1 parent18adbd9 commit6231e16

File tree

7 files changed

+95
-91
lines changed

7 files changed

+95
-91
lines changed

‎src/backend/nodes/copyfuncs.c

Lines changed: 13 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/copyfuncs.c,v 1.25 1997/12/23 21:49:03 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.26 1997/12/24 06:05:52 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1566,7 +1566,18 @@ _copyQuery(Query *from)
15661566
}
15671567
else
15681568
newnode->qry_aggs=NULL;
1569-
1569+
1570+
if (from->unionClause)
1571+
{
1572+
List*ulist,*temp_list=NIL;
1573+
1574+
foreach(ulist,from->unionClause)
1575+
temp_list=lappend(temp_list,copyObject(lfirst(ulist)));
1576+
newnode->unionClause=temp_list;
1577+
}
1578+
else
1579+
newnode->unionClause=NULL;
1580+
15701581
returnnewnode;
15711582
}
15721583

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

Lines changed: 14 additions & 6 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.15 1997/12/22 05:42:08 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.16 1997/12/24 06:06:01 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -81,11 +81,19 @@ planner(Query *parse)
8181
intrt_index;
8282

8383

84-
/*
85-
* plan inheritance
86-
*/
87-
rt_index=first_matching_rt_entry(rangetable,INHERITS_FLAG);
88-
if (rt_index!=-1)
84+
if (parse->unionClause)
85+
{
86+
result_plan= (Plan*)plan_union_queries(0,/* none */
87+
parse,
88+
UNION_FLAG);
89+
/* XXX do we need to do this? bjm 12/19/97 */
90+
tlist=preprocess_targetlist(tlist,
91+
parse->commandType,
92+
parse->resultRelation,
93+
parse->rtable);
94+
}
95+
elseif ((rt_index=
96+
first_matching_rt_entry(rangetable,INHERITS_FLAG))!=-1)
8997
{
9098
result_plan= (Plan*)plan_union_queries((Index)rt_index,
9199
parse,

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

Lines changed: 37 additions & 34 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.12 1997/12/21 05:18:28 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.13 1997/12/24 06:06:07 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -149,20 +149,51 @@ plan_union_queries(Index rt_index,
149149
find_all_inheritors(lconsi(rt_entry->relid,
150150
NIL),
151151
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)
157+
{
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));
152179
break;
153-
154-
#if0
180+
155181
caseUNION_FLAG:
156182
{
157-
Indexrt_index=0;
183+
List*ulist,*hold_union,*union_plans;
158184

159-
union_plans=handleunion(root,rangetable,tlist,qual);
185+
hold_union=parse->unionClause;
186+
parse->unionClause=NULL;/* prevent looping */
187+
188+
union_plans=lcons(planner(parse),NIL);
189+
190+
foreach(ulist,hold_union)
191+
union_plans=lappend(union_plans,planner(lfirst(ulist)));
160192
return (make_append(union_plans,
161193
rt_index,rangetable,
162194
((Plan*)lfirst(union_plans))->targetlist));
163195
}
164196
break;
165-
#endif
166197

167198
caseVERSION_FLAG:
168199
union_relids=VersionGetParents(rt_entry->relid);
@@ -172,34 +203,6 @@ plan_union_queries(Index rt_index,
172203
/* do nothing */
173204
break;
174205
}
175-
176-
/*
177-
* Remove the flag for this relation, since we're about to handle it
178-
* (do it before recursing!). XXX destructive parse tree change
179-
*/
180-
switch (flag)
181-
{
182-
caseINHERITS_FLAG:
183-
rt_fetch(rt_index,rangetable)->inh= false;
184-
break;
185-
default:
186-
break;
187-
}
188-
189-
/*
190-
* XXX - can't find any reason to sort union-relids as paul did, so
191-
* we're leaving it out for now (maybe forever) - jeff & lp
192-
*
193-
* [maybe so. btw, jeff & lp did the lisp conversion, according to Paul.
194-
* -- ay 10/94.]
195-
*/
196-
union_plans=plan_union_query(union_relids,rt_index,rt_entry,
197-
parse,flag,&union_rt_entries);
198-
199-
return (make_append(union_plans,
200-
rt_index,
201-
union_rt_entries,
202-
((Plan*)lfirst(union_plans))->targetlist));
203206
}
204207

205208

‎src/backend/parser/analyze.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.55 1997/12/23 19:39:42 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.56 1997/12/24 06:06:18 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -821,18 +821,7 @@ transformSelectStmt(ParseState *pstate, RetrieveStmt *stmt)
821821
/* fix where clause */
822822
qry->qual=transformWhereClause(pstate,stmt->whereClause);
823823

824-
/* check subselect clause */
825-
if (stmt->unionClause)
826-
{
827-
elog(NOTICE,"UNION not yet supported; using first SELECT only",NULL);
828-
829-
/* XXX HACK just playing with union clause - thomas 1997-12-19 */
830-
if ((qry->uniqueFlag==NULL)
831-
&& (! ((SubSelect*)lfirst(stmt->unionClause))->unionall))
832-
qry->uniqueFlag="*";
833-
}
834-
835-
/* check subselect clause */
824+
/* check having clause */
836825
if (stmt->havingClause)
837826
elog(NOTICE,"HAVING not yet supported; ignore clause",NULL);
838827

@@ -842,7 +831,6 @@ transformSelectStmt(ParseState *pstate, RetrieveStmt *stmt)
842831
qry->targetList,
843832
qry->uniqueFlag);
844833

845-
/* fix group by clause */
846834
qry->groupClause=transformGroupClause(pstate,
847835
stmt->groupClause,
848836
qry->targetList);
@@ -851,6 +839,20 @@ transformSelectStmt(ParseState *pstate, RetrieveStmt *stmt)
851839
if (pstate->p_numAgg>0)
852840
finalizeAggregates(pstate,qry);
853841

842+
if (stmt->unionClause)
843+
{
844+
List*ulist=NIL;
845+
QueryTreeList*qlist;
846+
inti;
847+
848+
qlist=parse_analyze(stmt->unionClause);
849+
for (i=0;i<qlist->len;i++)
850+
ulist=lappend(ulist,qlist->qtrees[i]);
851+
qry->unionClause=ulist;
852+
}
853+
else
854+
qry->unionClause=NULL;
855+
854856
return (Query*)qry;
855857
}
856858

‎src/backend/parser/gram.y

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 1.80 1997/12/23 19:47:32 thomas Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 1.81 1997/12/24 06:06:26 momjian Exp $
1414
*
1515
* HISTORY
1616
* AUTHORDATEMAJOR EVENT
@@ -120,19 +120,20 @@ Oidparam_type(int t); /* used in parse_expr.c */
120120
ProcedureStmt, RecipeStmt, RemoveAggrStmt, RemoveOperStmt,
121121
RemoveFuncStmt, RemoveStmt,
122122
RenameStmt, RevokeStmt, RuleStmt, TransactionStmt, ViewStmt, LoadStmt,
123-
CreatedbStmt, DestroydbStmt, VacuumStmt,RetrieveStmt, CursorStmt,
124-
ReplaceStmt, AppendStmt, NotifyStmt, DeleteStmt, ClusterStmt,
123+
CreatedbStmt, DestroydbStmt, VacuumStmt,CursorStmt, SubSelect,
124+
ReplaceStmt, AppendStmt,RetrieveStmt,NotifyStmt, DeleteStmt, ClusterStmt,
125125
ExplainStmt, VariableSetStmt, VariableShowStmt, VariableResetStmt,
126126
CreateUserStmt, AlterUserStmt, DropUserStmt
127127

128+
%type <rtstmt>
129+
128130
%type <str>opt_database, location
129131

130132
%type <pboolean> user_createdb_clause, user_createuser_clause
131133
%type <str> user_passwd_clause
132134
%type <str> user_valid_clause
133135
%type <list> user_group_list, user_group_clause
134136

135-
%type <node>SubSelect
136137
%type <str>join_expr, join_outer, join_spec
137138
%type <boolean> TriggerActionTime, TriggerForSpec, PLangTrusted
138139

@@ -1049,19 +1050,10 @@ OptArchiveType: ARCHIVE '=' NONE{ }
10491050

10501051
CreateAsStmt: CREATE TABLE relation_name OptCreateAs AS SubSelect
10511052
{
1052-
RetrieveStmt *n = makeNode(RetrieveStmt);
1053-
SubSelect *s = (SubSelect *)$6;
1054-
n->unique = s->unique;
1055-
n->targetList = s->targetList;
1053+
RetrieveStmt *n = (RetrieveStmt *)$6;
10561054
if ($4 != NIL)
10571055
mapTargetColumns($4, n->targetList);
10581056
n->into = $3;
1059-
n->fromClause = s->fromClause;
1060-
n->whereClause = s->whereClause;
1061-
n->groupClause = s->groupClause;
1062-
n->havingClause = s->havingClause;
1063-
n->unionClause = NULL;
1064-
n->sortClause = NULL;
10651057
$$ = (Node *)n;
10661058
}
10671059
;
@@ -2291,7 +2283,7 @@ RetrieveStmt: SELECT opt_unique res_target_list2
22912283

22922284
union_clause: UNION opt_union select_list
22932285
{
2294-
SubSelect *n = lfirst($3);
2286+
RetrieveStmt *n =(RetrieveStmt *)lfirst($3);
22952287
n->unionall = $2;
22962288
$$ = $3;
22972289
}
@@ -2301,7 +2293,7 @@ union_clause: UNION opt_union select_list
23012293

23022294
select_list: select_list UNION opt_union SubSelect
23032295
{
2304-
SubSelect *n = (SubSelect *)$4;
2296+
RetrieveStmt *n = (RetrieveStmt *)$4;
23052297
n->unionall = $3;
23062298
$$ = lappend($1, $4);
23072299
}
@@ -2313,7 +2305,7 @@ SubSelect:SELECT opt_unique res_target_list2
23132305
from_clause where_clause
23142306
group_clause having_clause
23152307
{
2316-
SubSelect *n = makeNode(SubSelect);
2308+
RetrieveStmt *n = makeNode(RetrieveStmt);
23172309
n->unique = $2;
23182310
n->unionall = FALSE;
23192311
n->targetList = $3;

‎src/include/nodes/parsenodes.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: parsenodes.h,v 1.38 1997/12/23 19:58:12 thomas Exp $
9+
* $Id: parsenodes.h,v 1.39 1997/12/24 06:06:53 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -58,6 +58,8 @@ typedef struct Query
5858
intqry_numAgg;/* number of aggregates in the target list */
5959
Aggreg**qry_aggs;/* the aggregates */
6060

61+
List*unionClause;/* unions are linked under the previous query */
62+
6163
/* internal to planner */
6264
List*base_relation_list_;/* base relation list */
6365
List*join_relation_list_;/* list of relations */
@@ -634,28 +636,14 @@ typedef struct RetrieveStmt
634636
Node*havingClause;/* having conditional-expression */
635637
List*unionClause;/* union subselect parameters */
636638
List*sortClause;/* sort clause (a list of SortGroupBy's) */
639+
intunionall;/* union without unique sort */
637640
}RetrieveStmt;
638641

639642

640643
/****************************************************************************
641644
*Supporting data structures for Parse Trees
642645
****************************************************************************/
643646

644-
/*
645-
* SubSelect - specifies subselect parameters
646-
*/
647-
typedefstructSubSelect
648-
{
649-
NodeTagtype;
650-
char*unique;/* NULL, '*', or unique attribute name */
651-
intunionall;/* union without unique sort */
652-
List*targetList;/* the target list (of ResTarget) */
653-
List*fromClause;/* the from clause */
654-
Node*whereClause;/* qualifications */
655-
List*groupClause;/* group by clause */
656-
Node*havingClause;/* having conditional-expression */
657-
}SubSelect;
658-
659647
/*
660648
* TypeName - specifies a type in definitions
661649
*/

‎src/include/optimizer/prep.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: prep.h,v 1.9 1997/12/20 07:59:44 momjian Exp $
9+
* $Id: prep.h,v 1.10 1997/12/24 06:06:58 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -32,7 +32,7 @@ extern List *preprocess_targetlist(List *tlist, int command_type,
3232
*/
3333
typedefenumUnionFlag
3434
{
35-
INHERITS_FLAG,VERSION_FLAG
35+
INHERITS_FLAG,UNION_FLAG,VERSION_FLAG
3636
}UnionFlag;
3737

3838
externList*find_all_inheritors(List*unexamined_relids,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp