From 0e1bfe92c75975711cefb776a7518ee9468193f6 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Fri, 22 Oct 1999 11:51:35 +0000
Subject: [PATCH] I have a patch for postgresql-snapshot(1999-10-22).

This patch fix a TODO list item.
* require SELECT DISTINCT target list to have all ORDER BY columns

example
ogawa=> select distinct x from t1 order by y;
ERROR:  ORDER BY columns must appear in SELECT DISTINCT target list

---
Atsushi Ogawa
---
 src/backend/parser/parse_clause.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c
index 81f3f88669d..a0e2bab371e 100644
--- a/src/backend/parser/parse_clause.c
+++ b/src/backend/parser/parse_clause.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.46 1999/10/07 04:23:12 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.47 1999/10/22 11:51:35 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -31,7 +31,8 @@
 static char *clauseText[] = {"ORDER", "GROUP"};
 
 static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
-										List *tlist, int clause);
+										List *tlist, int clause,
+										char *uniqFlag);
 static void parseFromClause(ParseState *pstate, List *frmList, Node **qual);
 static char	*transformTableEntry(ParseState *pstate, RangeVar *r);
 static List *addTargetToSortList(TargetEntry *tle, List *sortlist,
@@ -363,7 +364,8 @@ parseFromClause(ParseState *pstate, List *frmList, Node **qual)
  * clause	identifies clause type for error messages.
  */
 static TargetEntry *
-findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
+findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause,
+					char *uniqueFlag)
 {
 	TargetEntry *target_result = NULL;
 	List	   *tl;
@@ -462,6 +464,10 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
 	 * the end of the target list.	 This target is set to be  resjunk =
 	 * TRUE so that it will not be projected into the final tuple.
 	 */
+	if(clause == ORDER_CLAUSE && uniqueFlag) {
+		elog(ERROR, "ORDER BY columns must appear in SELECT DISTINCT target list");
+	}
+
 	target_result = transformTargetEntry(pstate, node, expr, NULL, true);
 	lappend(tlist, target_result);
 
@@ -485,7 +491,7 @@ transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
 		TargetEntry *tle;
 
 		tle = findTargetlistEntry(pstate, lfirst(gl),
-								  targetlist, GROUP_CLAUSE);
+								  targetlist, GROUP_CLAUSE, NULL);
 
 		/* avoid making duplicate grouplist entries */
 		if (! exprIsInSortList(tle->expr, glist, targetlist))
@@ -527,7 +533,7 @@ transformSortClause(ParseState *pstate,
 		TargetEntry	   *tle;
 
 		tle = findTargetlistEntry(pstate, sortby->node,
-								  targetlist, ORDER_CLAUSE);
+								  targetlist, ORDER_CLAUSE, uniqueFlag);
 
 		sortlist = addTargetToSortList(tle, sortlist, targetlist,
 									   sortby->useOp);
-- 
GitLab