77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.46 1999/10/07 04:23:12 tgl Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.47 1999/10/22 11:51:35 momjian Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
3131static char * clauseText []= {"ORDER" ,"GROUP" };
3232
3333static TargetEntry * findTargetlistEntry (ParseState * pstate ,Node * node ,
34- List * tlist ,int clause );
34+ List * tlist ,int clause ,
35+ char * uniqFlag );
3536static void parseFromClause (ParseState * pstate ,List * frmList ,Node * * qual );
3637static char * transformTableEntry (ParseState * pstate ,RangeVar * r );
3738static List * addTargetToSortList (TargetEntry * tle ,List * sortlist ,
@@ -363,7 +364,8 @@ parseFromClause(ParseState *pstate, List *frmList, Node **qual)
363364 * clauseidentifies clause type for error messages.
364365 */
365366static TargetEntry *
366- findTargetlistEntry (ParseState * pstate ,Node * node ,List * tlist ,int clause )
367+ findTargetlistEntry (ParseState * pstate ,Node * node ,List * tlist ,int clause ,
368+ char * uniqueFlag )
367369{
368370TargetEntry * target_result = NULL ;
369371List * tl ;
@@ -462,6 +464,10 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
462464 * the end of the target list. This target is set to be resjunk =
463465 * TRUE so that it will not be projected into the final tuple.
464466 */
467+ if (clause == ORDER_CLAUSE && uniqueFlag ) {
468+ elog (ERROR ,"ORDER BY columns must appear in SELECT DISTINCT target list" );
469+ }
470+
465471target_result = transformTargetEntry (pstate ,node ,expr ,NULL , true);
466472lappend (tlist ,target_result );
467473
@@ -485,7 +491,7 @@ transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
485491TargetEntry * tle ;
486492
487493tle = findTargetlistEntry (pstate ,lfirst (gl ),
488- targetlist ,GROUP_CLAUSE );
494+ targetlist ,GROUP_CLAUSE , NULL );
489495
490496/* avoid making duplicate grouplist entries */
491497if (!exprIsInSortList (tle -> expr ,glist ,targetlist ))
@@ -527,7 +533,7 @@ transformSortClause(ParseState *pstate,
527533TargetEntry * tle ;
528534
529535tle = findTargetlistEntry (pstate ,sortby -> node ,
530- targetlist ,ORDER_CLAUSE );
536+ targetlist ,ORDER_CLAUSE , uniqueFlag );
531537
532538sortlist = addTargetToSortList (tle ,sortlist ,targetlist ,
533539sortby -> useOp );