Commit 22bfa720 authored by Tom Lane's avatar Tom Lane

Remove optimization whereby parser would make only one sort-list entry

when two equal() targetlist items were to be added to an ORDER BY or
DISTINCT list.  Although indeed this would make sorting fractionally
faster by sometimes saving a comparison, it confuses the heck out of
later stages of processing, because it makes it look like the user
wrote DISTINCT ON rather than DISTINCT.  Bug reported by joe@piscitella.com.
parent 315a9ca3
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.95 2002/08/04 19:48:10 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.96 2002/08/18 18:46:15 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -60,7 +60,7 @@ static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node, ...@@ -60,7 +60,7 @@ static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
List *tlist, int clause); List *tlist, int clause);
static List *addTargetToSortList(TargetEntry *tle, List *sortlist, static List *addTargetToSortList(TargetEntry *tle, List *sortlist,
List *targetlist, List *opname); List *targetlist, List *opname);
static bool exprIsInSortList(Node *expr, List *sortList, List *targetList); static bool targetIsInSortList(TargetEntry *tle, List *sortList);
/* /*
...@@ -1138,7 +1138,7 @@ transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist) ...@@ -1138,7 +1138,7 @@ transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
targetlist, GROUP_CLAUSE); targetlist, GROUP_CLAUSE);
/* avoid making duplicate grouplist entries */ /* avoid making duplicate grouplist entries */
if (!exprIsInSortList(tle->expr, glist, targetlist)) if (!targetIsInSortList(tle, glist))
{ {
GroupClause *grpcl = makeNode(GroupClause); GroupClause *grpcl = makeNode(GroupClause);
...@@ -1333,7 +1333,7 @@ addTargetToSortList(TargetEntry *tle, List *sortlist, List *targetlist, ...@@ -1333,7 +1333,7 @@ addTargetToSortList(TargetEntry *tle, List *sortlist, List *targetlist,
List *opname) List *opname)
{ {
/* avoid making duplicate sortlist entries */ /* avoid making duplicate sortlist entries */
if (!exprIsInSortList(tle->expr, sortlist, targetlist)) if (!targetIsInSortList(tle, sortlist))
{ {
SortClause *sortcl = makeNode(SortClause); SortClause *sortcl = makeNode(SortClause);
...@@ -1382,23 +1382,28 @@ assignSortGroupRef(TargetEntry *tle, List *tlist) ...@@ -1382,23 +1382,28 @@ assignSortGroupRef(TargetEntry *tle, List *tlist)
} }
/* /*
* exprIsInSortList * targetIsInSortList
* Is the given expression already in the sortlist? * Is the given target item already in the sortlist?
* Note we will say 'yes' if it is equal() to any sortlist item,
* even though that might be a different targetlist member.
* *
* Works for both SortClause and GroupClause lists. * Works for both SortClause and GroupClause lists. Note that the main
* reason we need this routine (and not just a quick test for nonzeroness
* of ressortgroupref) is that a TLE might be in only one of the lists.
*/ */
static bool static bool
exprIsInSortList(Node *expr, List *sortList, List *targetList) targetIsInSortList(TargetEntry *tle, List *sortList)
{ {
Index ref = tle->resdom->ressortgroupref;
List *i; List *i;
/* no need to scan list if tle has no marker */
if (ref == 0)
return false;
foreach(i, sortList) foreach(i, sortList)
{ {
SortClause *scl = (SortClause *) lfirst(i); SortClause *scl = (SortClause *) lfirst(i);
if (equal(expr, get_sortgroupclause_expr(scl, targetList))) if (scl->tleSortGroupRef == ref)
return true; return true;
} }
return false; return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment