1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-22 23:02:54 +03:00

Handle case of GROUP BY target list column number out of range.

This commit is contained in:
Thomas G. Lockhart 1998-07-09 14:34:05 +00:00
parent a3c9cce930
commit fa2a1d7d52

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.19 1998/07/08 14:04:10 thomas Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.20 1998/07/09 14:34:05 thomas Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -141,7 +141,7 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
target_pos = 0; target_pos = 0;
TargetEntry *target_result = NULL; TargetEntry *target_result = NULL;
if (sortgroupby->range) if (sortgroupby->range != NULL)
real_rtable_pos = refnameRangeTablePosn(pstate, sortgroupby->range, NULL); real_rtable_pos = refnameRangeTablePosn(pstate, sortgroupby->range, NULL);
foreach(i, tlist) foreach(i, tlist)
@ -152,7 +152,8 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
char *resname = resnode->resname; char *resname = resnode->resname;
int test_rtable_pos = var->varno; int test_rtable_pos = var->varno;
if (!sortgroupby->name) /* no name specified? then must have been a column number instead... */
if (sortgroupby->name == NULL)
{ {
if (sortgroupby->resno == ++target_pos) if (sortgroupby->resno == ++target_pos)
{ {
@ -160,11 +161,13 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
break; break;
} }
} }
/* otherwise, try to match name... */
else else
{ {
if (!strcmp(resname, sortgroupby->name)) /* same name? */
if (strcmp(resname, sortgroupby->name) == 0)
{ {
if (sortgroupby->range) if (sortgroupby->range != NULL)
{ {
if (real_rtable_pos == test_rtable_pos) if (real_rtable_pos == test_rtable_pos)
{ {
@ -185,6 +188,18 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
} }
} }
/* No name specified and no target found?
* Then must have been an out-of-range column number instead...
* - thomas 1998-07-09
*/
if ((sortgroupby->name == NULL) && (target_result == NULL))
{
elog(ERROR, "ORDER/GROUP BY position %d is not in target list",
sortgroupby->resno);
}
/* BEGIN add missing target entry hack. /* BEGIN add missing target entry hack.
* *
* Prior to this hack, this function returned NIL if no target_result. * Prior to this hack, this function returned NIL if no target_result.
@ -194,11 +209,12 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
* it will not be projected into the final tuple. * it will not be projected into the final tuple.
* daveh@insightdist.com 5/20/98 * daveh@insightdist.com 5/20/98
*/ */
if ( ! target_result && sortgroupby->name) { if ((target_result == NULL) && (sortgroupby->name != NULL)) {
List *p_target = tlist; List *p_target = tlist;
TargetEntry *tent = makeNode(TargetEntry); TargetEntry *tent = makeNode(TargetEntry);
if (sortgroupby->range) { if (sortgroupby->range != NULL) {
Attr *missingAttr = (Attr *)makeNode(Attr); Attr *missingAttr = (Attr *)makeNode(Attr);
missingAttr->type = T_Attr; missingAttr->type = T_Attr;
@ -211,7 +227,8 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
&missingAttr->relname, NULL, &missingAttr->relname, NULL,
missingAttr->relname, TRUE); missingAttr->relname, TRUE);
} }
else { else
{
Ident *missingIdent = (Ident *)makeNode(Ident); Ident *missingIdent = (Ident *)makeNode(Ident);
missingIdent->type = T_Ident; missingIdent->type = T_Ident;
@ -253,6 +270,7 @@ transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
Resdom *resdom; Resdom *resdom;
restarget = find_targetlist_entry(pstate, lfirst(grouplist), targetlist); restarget = find_targetlist_entry(pstate, lfirst(grouplist), targetlist);
grpcl->entry = restarget; grpcl->entry = restarget;
resdom = restarget->resdom; resdom = restarget->resdom;
grpcl->grpOpoid = oprid(oper("<", grpcl->grpOpoid = oprid(oper("<",
@ -306,8 +324,8 @@ transformSortClause(ParseState *pstate,
TargetEntry *restarget; TargetEntry *restarget;
Resdom *resdom; Resdom *resdom;
restarget = find_targetlist_entry(pstate, sortby, targetlist); restarget = find_targetlist_entry(pstate, sortby, targetlist);
sortcl->resdom = resdom = restarget->resdom; sortcl->resdom = resdom = restarget->resdom;
sortcl->opoid = oprid(oper(sortby->useOp, sortcl->opoid = oprid(oper(sortby->useOp,
resdom->restype, resdom->restype,