8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -56,6 +56,11 @@ ExecUnique(UniqueState *node)
56
56
/*
57
57
* now loop, returning only non-duplicate tuples. We assume that the
58
58
* 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.
59
64
*/
60
65
for (;;)
61
66
{
@@ -64,10 +69,16 @@ ExecUnique(UniqueState *node)
64
69
*/
65
70
slot = ExecProcNode (outerPlan );
66
71
if (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 ;
67
77
return NULL ;
78
+ }
68
79
69
80
/*
70
- * Always return the first tuple from the subplan.
81
+ * Always return the first/last tuple from the subplan.
71
82
*/
72
83
if (node -> priorTuple == NULL )
73
84
break ;