forked from zhangh43/vectorize_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorTupleSlot.c
448 lines (390 loc) · 11.7 KB
/
vectorTupleSlot.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*-------------------------------------------------------------------------
*
* vectorTupleSlot.c
*
* Copyright (c) 1996-2019, PostgreSQL Global Development Group
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/sysattr.h"
#include "access/tuptoaster.h"
#include "executor/tuptable.h"
#include "utils/expandeddatum.h"
#include "vectorTupleSlot.h"
static void Vslot_deform_tuple(TupleTableSlot *slot, int natts);
/* --------------------------------
* VMakeTupleTableSlot
*
* Basic routine to make an empty VectorTupleTableSlot.
* --------------------------------
*/
TupleTableSlot *
VMakeTupleTableSlot(void)
{
TupleTableSlot *slot;
VectorTupleSlot *vslot;
slot = palloc(sizeof(VectorTupleSlot));
NodeSetTag(slot, T_TupleTableSlot);
slot->tts_isempty = true;
slot->tts_shouldFree = false;
slot->tts_shouldFreeMin = false;
slot->tts_tuple = NULL;
slot->tts_tupleDescriptor = NULL;
slot->tts_mcxt = CurrentMemoryContext;
slot->tts_buffer = InvalidBuffer;
slot->tts_nvalid = 0;
slot->tts_values = NULL;
slot->tts_isnull = NULL;
slot->tts_mintuple = NULL;
/* vectorized fields */
vslot = (VectorTupleSlot*)slot;
vslot->dim = 0;
vslot->bufnum = 0;
memset(vslot->tts_buffers, InvalidBuffer, sizeof(vslot->tts_buffers));
memset(vslot->tts_tuples, 0, sizeof(vslot->tts_tuples));
/* all tuples should be skipped in initialization */
memset(vslot->skip, true, sizeof(vslot->skip));
return slot;
}
/* --------------------------------
* VExecAllocTableSlot
*
* Create a vector tuple table slot within a tuple table (which is just a List).
* --------------------------------
*/
TupleTableSlot *
VExecAllocTableSlot(List **tupleTable)
{
TupleTableSlot *slot = VMakeTupleTableSlot();
*tupleTable = lappend(*tupleTable, slot);
return slot;
}
/*
* slot_deform_tuple
* Given a TupleTableSlot, extract data from the slot's physical tuple
* into its Datum/isnull arrays. Data is extracted up through the
* natts'th column (caller must ensure this is a legal column number).
*
* This is essentially an incremental version of heap_deform_tuple:
* on each call we extract attributes up to the one needed, without
* re-computing information about previously extracted attributes.
* slot->tts_nvalid is the number of attributes already extracted.
*/
static void
Vslot_deform_tuple(TupleTableSlot *slot, int natts)
{
VectorTupleSlot *vslot = (VectorTupleSlot *)slot;
TupleDesc tupleDesc = slot->tts_tupleDescriptor;
HeapTuple tuple;
HeapTupleHeader tup;
bool hasnulls;
Form_pg_attribute *att = tupleDesc->attrs;
int attnum;
char *tp; /* ptr to tuple data */
long off; /* offset in tuple data */
bits8 *bp; /* ptr to null bitmap in tuple */
bool slow; /* can we use/set attcacheoff? */
int row;
vtype *column;
for (row = 0; row < vslot->dim; row++)
{
tuple = &vslot->tts_tuples[row];
tup = tuple->t_data;
bp = tup->t_bits;
hasnulls = HeapTupleHasNulls(tuple);
attnum = slot->tts_nvalid;
/*
* Check whether the first call for this tuple, and initialize or restore
* loop state.
*/
/* vectorize engine deform once for now */
off = 0;
slow = false;
tp = (char *) tup + tup->t_hoff;
for (; attnum < natts; attnum++)
{
Form_pg_attribute thisatt = att[attnum];
column = (vtype *)slot->tts_values[attnum];
if (hasnulls && att_isnull(attnum, bp))
{
column->values[row] = (Datum) 0;
column->isnull[row] = true;
slow = true; /* can't use attcacheoff anymore */
continue;
}
column->isnull[row] = false;
if (!slow && thisatt->attcacheoff >= 0)
off = thisatt->attcacheoff;
else if (thisatt->attlen == -1)
{
/*
* We can only cache the offset for a varlena attribute if the
* offset is already suitably aligned, so that there would be no
* pad bytes in any case: then the offset will be valid for either
* an aligned or unaligned value.
*/
if (!slow &&
off == att_align_nominal(off, thisatt->attalign))
thisatt->attcacheoff = off;
else
{
off = att_align_pointer(off, thisatt->attalign, -1,
tp + off);
slow = true;
}
}
else
{
/* not varlena, so safe to use att_align_nominal */
off = att_align_nominal(off, thisatt->attalign);
if (!slow)
thisatt->attcacheoff = off;
}
column->values[row] = fetchatt(thisatt, tp + off);
off = att_addlength_pointer(off, thisatt->attlen, tp + off);
if (thisatt->attlen <= 0)
slow = true; /* can't use attcacheoff anymore */
}
}
attnum = slot->tts_nvalid;
for (; attnum < natts; attnum++)
{
column = (vtype *)slot->tts_values[attnum];
column->dim = vslot->dim;
}
/*
* Save state for next execution
*/
slot->tts_nvalid = attnum;
}
/*
* slot_getallattrs
* This function forces all the entries of the slot's Datum/isnull
* arrays to be valid. The caller may then extract data directly
* from those arrays instead of using slot_getattr.
*/
void
Vslot_getallattrs(TupleTableSlot *slot)
{
VectorTupleSlot *vslot = (VectorTupleSlot *)slot;
int tdesc_natts = slot->tts_tupleDescriptor->natts;
int attnum;
HeapTuple tuple;
int i;
/* Quick out if we have 'em all already */
if (slot->tts_nvalid == tdesc_natts)
return;
if (vslot->dim == 0)
return;
/*
* otherwise we had better have a physical tuple (tts_nvalid should equal
* natts in all virtual-tuple cases)
*/
for (i = 0; i < vslot->dim; i++)
{
tuple = &vslot->tts_tuples[i];
if (tuple == NULL) /* internal error */
elog(ERROR, "cannot extract attribute from empty tuple slot");
}
/*
* load up any slots available from physical tuple
*/
attnum = HeapTupleHeaderGetNatts(vslot->tts_tuples[0].t_data);
attnum = Min(attnum, tdesc_natts);
Vslot_deform_tuple(slot, attnum);
/*
* If tuple doesn't have all the atts indicated by tupleDesc, read the
* rest as null
*/
for (; attnum < tdesc_natts; attnum++)
{
slot->tts_values[attnum] = (Datum) 0;
slot->tts_isnull[attnum] = true;
}
slot->tts_nvalid = tdesc_natts;
}
/*
* slot_getsomeattrs
* This function forces the entries of the slot's Datum/isnull
* arrays to be valid at least up through the attnum'th entry.
*/
void
Vslot_getsomeattrs(TupleTableSlot *slot, int attnum)
{
/* Quick out if we have 'em all already */
if (slot->tts_nvalid >= attnum)
return;
elog(ERROR, "slot should be deformed in scan for vectorize engine");
}
/* --------------------------------
* VExecClearTuple
*
* This function is used to clear out a slot in the tuple table.
*
* NB: only the tuple is cleared, not the tuple descriptor (if any).
* --------------------------------
*/
TupleTableSlot *
VExecClearTuple(TupleTableSlot *slot) /* slot in which to store tuple */
{
int i;
vtype *column;
VectorTupleSlot *vslot;
/*
* sanity checks
*/
Assert(slot != NULL);
vslot = (VectorTupleSlot *)slot;
/*
* Free the old physical tuple if necessary.
*/
if (slot->tts_shouldFree)
heap_freetuple(slot->tts_tuple);
if (slot->tts_shouldFreeMin)
heap_free_minimal_tuple(slot->tts_mintuple);
slot->tts_tuple = NULL;
slot->tts_mintuple = NULL;
slot->tts_shouldFree = false;
slot->tts_shouldFreeMin = false;
/*
* Drop the pin on the referenced buffer, if there is one.
*/
if (BufferIsValid(slot->tts_buffer))
ReleaseBuffer(slot->tts_buffer);
slot->tts_buffer = InvalidBuffer;
/*
* Mark it empty.
*/
slot->tts_isempty = true;
slot->tts_nvalid = 0;
/* vectorize part */
for(i = 0; i < vslot->bufnum; i++)
{
if(BufferIsValid(vslot->tts_buffers[i]))
{
ReleaseBuffer(vslot->tts_buffers[i]);
vslot->tts_buffers[i] = InvalidBuffer;
}
}
vslot->dim = 0;
vslot->bufnum = 0;
for (i = 0; i < slot->tts_tupleDescriptor->natts; i++)
{
column = (vtype *)DatumGetPointer(slot->tts_values[i]);
column->dim = 0;
}
memset(vslot->skip, true, sizeof(vslot->skip));
return slot;
}
/* --------------------------------
* ExecStoreTuple
*
* This function is used to store a physical tuple into a specified
* slot in the tuple table.
*
* tuple: tuple to store
* slot: slot to store it in
* buffer: disk buffer if tuple is in a disk page, else InvalidBuffer
* shouldFree: true if ExecClearTuple should pfree() the tuple
* when done with it
*
* If 'buffer' is not InvalidBuffer, the tuple table code acquires a pin
* on the buffer which is held until the slot is cleared, so that the tuple
* won't go away on us.
*
* shouldFree is normally set 'true' for tuples constructed on-the-fly.
* It must always be 'false' for tuples that are stored in disk pages,
* since we don't want to try to pfree those.
*
* Another case where it is 'false' is when the referenced tuple is held
* in a tuple table slot belonging to a lower-level executor Proc node.
* In this case the lower-level slot retains ownership and responsibility
* for eventually releasing the tuple. When this method is used, we must
* be certain that the upper-level Proc node will lose interest in the tuple
* sooner than the lower-level one does! If you're not certain, copy the
* lower-level tuple with heap_copytuple and let the upper-level table
* slot assume ownership of the copy!
*
* Return value is just the passed-in slot pointer.
*
* NOTE: before PostgreSQL 8.1, this function would accept a NULL tuple
* pointer and effectively behave like ExecClearTuple (though you could
* still specify a buffer to pin, which would be an odd combination).
* This saved a couple lines of code in a few places, but seemed more likely
* to mask logic errors than to be really useful, so it's now disallowed.
* --------------------------------
*/
TupleTableSlot *
VExecStoreTuple(HeapTuple tuple,
TupleTableSlot *slot,
Buffer buffer,
bool shouldFree)
{
VectorTupleSlot *vslot;
/*
* sanity checks
*/
Assert(tuple != NULL);
Assert(slot != NULL);
Assert(slot->tts_tupleDescriptor != NULL);
/* passing shouldFree=true for a tuple on a disk page is not sane */
Assert(BufferIsValid(buffer) ? (!shouldFree) : true);
vslot = (VectorTupleSlot *)slot;
/*
* Free any old physical tuple belonging to the slot.
*/
if (slot->tts_shouldFree)
heap_freetuple(slot->tts_tuple);
if (slot->tts_shouldFreeMin)
heap_free_minimal_tuple(slot->tts_mintuple);
/*
* Store the new tuple into the specified slot.
*/
slot->tts_isempty = false;
slot->tts_shouldFree = shouldFree;
slot->tts_shouldFreeMin = false;
memcpy(&vslot->tts_tuples[vslot->dim], tuple, sizeof(HeapTupleData));
slot->tts_mintuple = NULL;
/* Mark extracted state invalid */
slot->tts_nvalid = 0;
/*
* If tuple is on a disk page, keep the page pinned as long as we hold a
* pointer into it. We assume the caller already has such a pin.
*
* This is coded to optimize the case where the slot previously held a
* tuple on the same disk page: in that case releasing and re-acquiring
* the pin is a waste of cycles. This is a common situation during
* seqscans, so it's worth troubling over.
*/
if (vslot->bufnum == 0 || vslot->tts_buffers[vslot->bufnum-1] != buffer)
{
if (BufferIsValid(vslot->tts_buffers[vslot->bufnum]))
ReleaseBuffer(vslot->tts_buffers[vslot->bufnum]);
vslot->tts_buffers[vslot->bufnum] = buffer;
vslot->bufnum++;
if (BufferIsValid(buffer))
IncrBufferRefCount(buffer);
}
vslot->dim++;
return slot;
}
void
InitializeVectorSlotColumn(VectorTupleSlot *vslot)
{
TupleDesc desc;
Oid typid;
vtype *column;
int i;
desc = vslot->tts.tts_tupleDescriptor;
/* initailize column in vector slot */
for (i = 0; i < desc->natts; i++)
{
typid = desc->attrs[i]->atttypid;
column = buildvtype(typid, BATCHSIZE, vslot->skip);
column->dim = 0;
vslot->tts.tts_values[i] = PointerGetDatum(column);
/* tts_isnull not used yet */
vslot->tts.tts_isnull[i] = false;
}
}