summaryrefslogtreecommitdiff
path: root/src/backend/executor/nodeUnique.c
blob: 6078e0f68a98c1b0009d8fee9a79828de77100fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*-------------------------------------------------------------------------
 *
 * nodeUnique.c
 *	  Routines to handle unique'ing of queries where appropriate
 *
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/executor/nodeUnique.c,v 1.26 2000/01/26 05:56:24 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
/*
 * INTERFACE ROUTINES
 *		ExecUnique		- generate a unique'd temporary relation
 *		ExecInitUnique	- initialize node and subnodes..
 *		ExecEndUnique	- shutdown node and subnodes
 *
 * NOTES
 *		Assumes tuples returned from subplan arrive in
 *		sorted order.
 *
 */

#include "postgres.h"

#include "access/heapam.h"
#include "access/printtup.h"
#include "executor/executor.h"
#include "executor/nodeUnique.h"

/* ----------------------------------------------------------------
 *		ExecIdenticalTuples
 *
 *		This is a hack function used by ExecUnique to see if
 *		two tuples are identical.  This should be provided
 *		by the heap tuple code but isn't.  The real problem
 *		is that we assume we can byte compare tuples to determine
 *		if they are "equal".  In fact, if we have user defined
 *		types there may be problems because it's possible that
 *		an ADT may have multiple representations with the
 *		same ADT value. -cim
 * ----------------------------------------------------------------
 */
static bool						/* true if tuples are identical, false
								 * otherwise */
ExecIdenticalTuples(TupleTableSlot *t1, TupleTableSlot *t2)
{
	HeapTuple	h1;
	HeapTuple	h2;
	char	   *d1;
	char	   *d2;
	int			len;

	h1 = t1->val;
	h2 = t2->val;

	/* ----------------
	 *	if tuples aren't the same length then they are
	 *	obviously different (one may have null attributes).
	 * ----------------
	 */
	if (h1->t_len != h2->t_len)
		return false;

	/* ----------------
	 *	if the tuples have different header offsets then
	 *	they are different.  This will prevent us from returning
	 *	true when comparing tuples of one attribute where one of
	 *	two we're looking at is null (t_len - t_hoff == 0).
	 *	THE t_len FIELDS CAN BE THE SAME IN THIS CASE!!
	 * ----------------
	 */
	if (h1->t_data->t_hoff != h2->t_data->t_hoff)
		return false;

	/* ----------------
	 *	ok, now get the pointers to the data and the
	 *	size of the attribute portion of the tuple.
	 * ----------------
	 */
	d1 = (char *) GETSTRUCT(h1);
	d2 = (char *) GETSTRUCT(h2);
	len = (int) h1->t_len - (int) h1->t_data->t_hoff;

	/* ----------------
	 *	byte compare the data areas and return the result.
	 * ----------------
	 */
	if (memcmp(d1, d2, len) != 0)
		return false;

	return true;
}

/* ----------------------------------------------------------------
 *		ExecUnique
 *
 *		This is a very simple node which filters out duplicate
 *		tuples from a stream of sorted tuples from a subplan.
 *
 *		XXX see comments below regarding freeing tuples.
 * ----------------------------------------------------------------
 */
TupleTableSlot *				/* return: a tuple or NULL */
ExecUnique(Unique *node)
{
	UniqueState *uniquestate;
	TupleTableSlot *resultTupleSlot;
	TupleTableSlot *slot;
	Plan	   *outerPlan;
	char	   *uniqueAttr;
	AttrNumber	uniqueAttrNum;
	TupleDesc	tupDesc;
	Oid			typoutput,
				typelem;

	/* ----------------
	 *	get information from the node
	 * ----------------
	 */
	uniquestate = node->uniquestate;
	outerPlan = outerPlan((Plan *) node);
	resultTupleSlot = uniquestate->cs_ResultTupleSlot;
	uniqueAttr = node->uniqueAttr;
	uniqueAttrNum = node->uniqueAttrNum;

	if (uniqueAttr)
	{
		tupDesc = ExecGetResultType(uniquestate);
		getTypeOutAndElem((Oid) tupDesc->attrs[uniqueAttrNum - 1]->atttypid,
						  &typoutput, &typelem);
	}
	else
	{							/* keep compiler quiet */
		tupDesc = NULL;
		typoutput = InvalidOid;
		typelem = InvalidOid;
	}

	/* ----------------
	 *	now loop, returning only non-duplicate tuples.
	 *	We assume that the tuples arrive in sorted order
	 *	so we can detect duplicates easily.
	 * ----------------
	 */
	for (;;)
	{
		/* ----------------
		 *	 fetch a tuple from the outer subplan
		 * ----------------
		 */
		slot = ExecProcNode(outerPlan, (Plan *) node);
		if (TupIsNull(slot))
			return NULL;

		/* ----------------
		 *	 we use the result tuple slot to hold our saved tuples.
		 *	 if we haven't a saved tuple to compare our new tuple with,
		 *	 then we exit the loop. This new tuple as the saved tuple
		 *	 the next time we get here.
		 * ----------------
		 */
		if (TupIsNull(resultTupleSlot))
			break;

		/* ----------------
		 *	 now test if the new tuple and the previous
		 *	 tuple match.  If so then we loop back and fetch
		 *	 another new tuple from the subplan.
		 * ----------------
		 */

		if (uniqueAttr)
		{

			/*
			 * to check equality, we check to see if the typoutput of the
			 * attributes are equal
			 */
			bool		isNull1,
						isNull2;
			Datum		attr1,
						attr2;
			char	   *val1,
					   *val2;

			attr1 = heap_getattr(slot->val,
								 uniqueAttrNum, tupDesc, &isNull1);
			attr2 = heap_getattr(resultTupleSlot->val,
								 uniqueAttrNum, tupDesc, &isNull2);

			if (isNull1 == isNull2)
			{
				if (isNull1)	/* both are null, they are equal */
					continue;
				val1 = fmgr(typoutput, attr1, typelem,
							tupDesc->attrs[uniqueAttrNum - 1]->atttypmod);
				val2 = fmgr(typoutput, attr2, typelem,
							tupDesc->attrs[uniqueAttrNum - 1]->atttypmod);

				/*
				 * now, val1 and val2 are ascii representations so we can
				 * use strcmp for comparison
				 */
				if (strcmp(val1, val2) == 0)	/* they are equal */
				{
					pfree(val1);
					pfree(val2);
					continue;
				}
				pfree(val1);
				pfree(val2);
				break;
			}
			else
/* one is null and the other isn't, they aren't equal */
				break;

		}
		else
		{
			if (!ExecIdenticalTuples(slot, resultTupleSlot))
				break;
		}

	}

	/* ----------------
	 *	we have a new tuple different from the previous saved tuple
	 *	so we save it in the saved tuple slot.	We copy the tuple
	 *	so we don't increment the buffer ref count.
	 * ----------------
	 */
	ExecStoreTuple(heap_copytuple(slot->val),
				   resultTupleSlot,
				   InvalidBuffer,
				   true);

	return resultTupleSlot;
}

/* ----------------------------------------------------------------
 *		ExecInitUnique
 *
 *		This initializes the unique node state structures and
 *		the node's subplan.
 * ----------------------------------------------------------------
 */
bool							/* return: initialization status */
ExecInitUnique(Unique *node, EState *estate, Plan *parent)
{
	UniqueState *uniquestate;
	Plan	   *outerPlan;
	char	   *uniqueAttr;

	/* ----------------
	 *	assign execution state to node
	 * ----------------
	 */
	node->plan.state = estate;

	/* ----------------
	 *	create new UniqueState for node
	 * ----------------
	 */
	uniquestate = makeNode(UniqueState);
	node->uniquestate = uniquestate;
	uniqueAttr = node->uniqueAttr;

	/* ----------------
	 *	Miscellanious initialization
	 *
	 *		 +	assign node's base_id
	 *		 +	assign debugging hooks and
	 *
	 *	Unique nodes have no ExprContext initialization because
	 *	they never call ExecQual or ExecTargetList.
	 * ----------------
	 */
	ExecAssignNodeBaseInfo(estate, uniquestate, parent);

#define UNIQUE_NSLOTS 1
	/* ------------
	 * Tuple table initialization
	 * ------------
	 */
	ExecInitResultTupleSlot(estate, uniquestate);

	/* ----------------
	 *	then initialize outer plan
	 * ----------------
	 */
	outerPlan = outerPlan((Plan *) node);
	ExecInitNode(outerPlan, estate, (Plan *) node);

	/* ----------------
	 *	unique nodes do no projections, so initialize
	 *	projection info for this node appropriately
	 * ----------------
	 */
	ExecAssignResultTypeFromOuterPlan((Plan *) node, uniquestate);
	uniquestate->cs_ProjInfo = NULL;

	if (uniqueAttr)
	{
		TupleDesc	tupDesc;
		int			i = 0;

		tupDesc = ExecGetResultType(uniquestate);

		/*
		 * the parser should have ensured that uniqueAttr is a legal
		 * attribute name
		 */
		while (strcmp(NameStr(tupDesc->attrs[i]->attname), uniqueAttr) != 0)
			i++;
		node->uniqueAttrNum = i + 1;	/* attribute numbers start from 1 */
	}
	else
		node->uniqueAttrNum = InvalidAttrNumber;

	/* ----------------
	 *	all done.
	 * ----------------
	 */
	return TRUE;
}

int
ExecCountSlotsUnique(Unique *node)
{
	return ExecCountSlotsNode(outerPlan(node)) +
	ExecCountSlotsNode(innerPlan(node)) +
	UNIQUE_NSLOTS;
}

/* ----------------------------------------------------------------
 *		ExecEndUnique
 *
 *		This shuts down the subplan and frees resources allocated
 *		to this node.
 * ----------------------------------------------------------------
 */
void
ExecEndUnique(Unique *node)
{
	UniqueState *uniquestate;

	uniquestate = node->uniquestate;
	ExecEndNode(outerPlan((Plan *) node), (Plan *) node);
	ExecClearTuple(uniquestate->cs_ResultTupleSlot);
}


void
ExecReScanUnique(Unique *node, ExprContext *exprCtxt, Plan *parent)
{
	UniqueState *uniquestate = node->uniquestate;

	ExecClearTuple(uniquestate->cs_ResultTupleSlot);

	/*
	 * if chgParam of subnode is not null then plan will be re-scanned by
	 * first ExecProcNode.
	 */
	if (((Plan *) node)->lefttree->chgParam == NULL)
		ExecReScan(((Plan *) node)->lefttree, exprCtxt, (Plan *) node);

}