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

Commitfa2a1d7

Browse files
author
Thomas G. Lockhart
committed
Handle case of GROUP BY target list column number out of range.
1 parenta3c9cce commitfa2a1d7

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

‎src/backend/parser/parse_clause.c

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.19 1998/07/08 14:04:10 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.20 1998/07/09 14:34:05 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -136,12 +136,12 @@ parseFromClause(ParseState *pstate, List *frmList)
136136
staticTargetEntry*
137137
find_targetlist_entry(ParseState*pstate,SortGroupBy*sortgroupby,List*tlist)
138138
{
139-
List*i;
140-
intreal_rtable_pos=0,
141-
target_pos=0;
139+
List*i;
140+
intreal_rtable_pos=0,
141+
target_pos=0;
142142
TargetEntry*target_result=NULL;
143143

144-
if (sortgroupby->range)
144+
if (sortgroupby->range!=NULL)
145145
real_rtable_pos=refnameRangeTablePosn(pstate,sortgroupby->range,NULL);
146146

147147
foreach(i,tlist)
@@ -152,19 +152,22 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
152152
char*resname=resnode->resname;
153153
inttest_rtable_pos=var->varno;
154154

155-
if (!sortgroupby->name)
155+
/* no name specified? then must have been a column number instead... */
156+
if (sortgroupby->name==NULL)
156157
{
157158
if (sortgroupby->resno==++target_pos)
158159
{
159160
target_result=target;
160161
break;
161162
}
162163
}
164+
/* otherwise, try to match name... */
163165
else
164166
{
165-
if (!strcmp(resname,sortgroupby->name))
167+
/* same name? */
168+
if (strcmp(resname,sortgroupby->name)==0)
166169
{
167-
if (sortgroupby->range)
170+
if (sortgroupby->range!=NULL)
168171
{
169172
if (real_rtable_pos==test_rtable_pos)
170173
{
@@ -185,20 +188,33 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
185188
}
186189
}
187190

188-
/* BEGIN add missing target entry hack.
191+
192+
/* No name specified and no target found?
193+
* Then must have been an out-of-range column number instead...
194+
* - thomas 1998-07-09
195+
*/
196+
if ((sortgroupby->name==NULL)&& (target_result==NULL))
197+
{
198+
elog(ERROR,"ORDER/GROUP BY position %d is not in target list",
199+
sortgroupby->resno);
200+
}
201+
202+
203+
/* BEGIN add missing target entry hack.
189204
*
190-
*Prior to this hack, this function returned NIL if no target_result.
191-
*Thus, ORDER/GROUP BY required the attributes be in the target list.
192-
*Now it constructs a new target entry which is appended to the end of
193-
*the target list. This target is set to be resjunk = TRUE so that
194-
*it will not be projected into the final tuple.
195-
*daveh@insightdist.com 5/20/98
205+
* Prior to this hack, this function returned NIL if no target_result.
206+
* Thus, ORDER/GROUP BY required the attributes be in the target list.
207+
* Now it constructs a new target entry which is appended to the end of
208+
* the target list. This target is set to be resjunk = TRUE so that
209+
* it will not be projected into the final tuple.
210+
* daveh@insightdist.com 5/20/98
196211
*/
197-
if ( !target_result&&sortgroupby->name) {
212+
if ((target_result==NULL)&& (sortgroupby->name!=NULL)) {
213+
198214
List*p_target=tlist;
199215
TargetEntry*tent=makeNode(TargetEntry);
200-
201-
if (sortgroupby->range) {
216+
217+
if (sortgroupby->range!=NULL) {
202218
Attr*missingAttr= (Attr*)makeNode(Attr);
203219
missingAttr->type=T_Attr;
204220

@@ -211,7 +227,8 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
211227
&missingAttr->relname,NULL,
212228
missingAttr->relname, TRUE);
213229
}
214-
else {
230+
else
231+
{
215232
Ident*missingIdent= (Ident*)makeNode(Ident);
216233
missingIdent->type=T_Ident;
217234

@@ -224,7 +241,7 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
224241
}
225242

226243
/* Add to the end of the target list */
227-
while (lnext(p_target)!=NIL){
244+
while (lnext(p_target)!=NIL) {
228245
p_target=lnext(p_target);
229246
}
230247
lnext(p_target)=lcons(tent,NIL);
@@ -253,6 +270,7 @@ transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
253270
Resdom*resdom;
254271

255272
restarget=find_targetlist_entry(pstate,lfirst(grouplist),targetlist);
273+
256274
grpcl->entry=restarget;
257275
resdom=restarget->resdom;
258276
grpcl->grpOpoid=oprid(oper("<",
@@ -306,8 +324,8 @@ transformSortClause(ParseState *pstate,
306324
TargetEntry*restarget;
307325
Resdom*resdom;
308326

309-
310327
restarget=find_targetlist_entry(pstate,sortby,targetlist);
328+
311329
sortcl->resdom=resdom=restarget->resdom;
312330
sortcl->opoid=oprid(oper(sortby->useOp,
313331
resdom->restype,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp