88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/executor/nodeUnique.c,v 1.37 2003/01/10 23:54:24 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/executor/nodeUnique.c,v 1.38 2003/02/02 19:08:57 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -56,6 +56,11 @@ ExecUnique(UniqueState *node)
5656/*
5757 * now loop, returning only non-duplicate tuples. We assume that the
5858 * tuples arrive in sorted order so we can detect duplicates easily.
59+ *
60+ * We return the first tuple from each group of duplicates (or the
61+ * last tuple of each group, when moving backwards). At either end
62+ * of the subplan, clear priorTuple so that we correctly return the
63+ * first/last tuple when reversing direction.
5964 */
6065for (;;)
6166{
@@ -64,10 +69,16 @@ ExecUnique(UniqueState *node)
6469 */
6570slot = ExecProcNode (outerPlan );
6671if (TupIsNull (slot ))
72+ {
73+ /* end of subplan; reset in case we change direction */
74+ if (node -> priorTuple != NULL )
75+ heap_freetuple (node -> priorTuple );
76+ node -> priorTuple = NULL ;
6777return NULL ;
78+ }
6879
6980/*
70- * Always return the first tuple from the subplan.
81+ * Always return the first/last tuple from the subplan.
7182 */
7283if (node -> priorTuple == NULL )
7384break ;