-
Notifications
You must be signed in to change notification settings - Fork 6
/
nodeUnbatch.c
213 lines (176 loc) · 4.89 KB
/
nodeUnbatch.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
/*-------------------------------------------------------------------------
*
* unbatch.c
*
* Copyright (c) 1996-2019, PostgreSQL Global Development Group
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "optimizer/planner.h"
#include "executor/nodeCustom.h"
#include "nodeUnbatch.h"
#include "execTuples.h"
#include "vtype/vtype.h"
#include "utils.h"
#include "vectorTupleSlot.h"
/*
* UnbatchState - state object of vectorscan on executor.
*/
typedef struct UnbatchState
{
CustomScanState css;
TupleTableSlot *ps_ResultVTupleSlot; /* slot for my result tuples */
int iter;
/* Attributes for vectorization */
} UnbatchState;
static Node *CreateUnbatchState(CustomScan *custom_plan);
static TupleTableSlot *FetchRowFromBatch(UnbatchState *ubs);
static bool ReadNextVectorSlot(UnbatchState *ubs);
/* CustomScanExecMethods */
static void BeginUnbatch(CustomScanState *node, EState *estate, int eflags);
static TupleTableSlot *ExecUnbatch(CustomScanState *node);
static void EndUnbatch(CustomScanState *node);
static CustomScanMethods unbatch_methods = {
"unbatch", /* CustomName */
CreateUnbatchState, /* CreateCustomScanState */
};
static CustomExecMethods unbatch_exec_methods = {
"unbatch", /* CustomName */
BeginUnbatch, /* BeginCustomScan */
ExecUnbatch, /* ExecCustomScan */
EndUnbatch, /* EndCustomScan */
NULL, /* ReScanCustomScan */
NULL, /* MarkPosCustomScan */
NULL, /* RestrPosCustomScan */
NULL, /* EstimateDSMCustomScan */
NULL, /* InitializeDSMCustomScan */
NULL, /* InitializeWorkerCustomScan */
NULL, /* ExplainCustomScan */
};
static void
BeginUnbatch(CustomScanState *node, EState *estate, int eflags)
{
UnbatchState *vcs = (UnbatchState*) node;
CustomScan *cscan = (CustomScan *) node->ss.ps.plan;
TupleDesc tupdesc;
outerPlanState(vcs) = ExecInitNode(outerPlan(cscan), estate, eflags);
/* Convert Vtype in tupdesc to Ntype in unbatch Node */
{
node->ss.ps.ps_ResultTupleSlot->tts_tupleDescriptor = CreateTupleDescCopy(outerPlanState(vcs)->ps_ResultTupleSlot->tts_tupleDescriptor);
tupdesc = node->ss.ps.ps_ResultTupleSlot->tts_tupleDescriptor;
for (int i = 0; i < tupdesc->natts; i++)
{
Form_pg_attribute attr = tupdesc->attrs[i];
Oid typid = GetNtype(attr->atttypid);
if (typid != InvalidOid)
attr->atttypid = typid;
}
ExecSetSlotDescriptor(node->ss.ps.ps_ResultTupleSlot, tupdesc);
}
vcs->ps_ResultVTupleSlot = VExecInitExtraTupleSlot(estate);
vcs->ps_ResultVTupleSlot->tts_tupleDescriptor = CreateTupleDescCopy(outerPlanState(vcs)->ps_ResultTupleSlot->tts_tupleDescriptor);
}
static TupleTableSlot*
FetchRowFromBatch(UnbatchState *ubs)
{
VectorTupleSlot *vslot;
TupleTableSlot *slot;
int iter;
int natts;
int i;
slot = ubs->css.ss.ps.ps_ResultTupleSlot;
vslot = (VectorTupleSlot *)ubs->ps_ResultVTupleSlot;
iter = ubs->iter;
while(iter < BATCHSIZE && vslot->skip[iter])
iter++;
/* we have checked that natts is greater than zero */
if (iter == BATCHSIZE)
return NULL;
ExecClearTuple(slot);
natts = slot->tts_tupleDescriptor->natts;
for(i = 0; i < natts; i++)
{
slot->tts_values[i] = ((vtype*)(vslot->tts.tts_values[i]))->values[iter];
slot->tts_isnull[i] = false;
}
ubs->iter = ++iter;
return ExecStoreVirtualTuple(slot);
}
/*
*
*/
static TupleTableSlot *
ExecUnbatch(CustomScanState *node)
{
UnbatchState *ubs;
TupleTableSlot *slot;
ubs = (UnbatchState*) node;
/* find a non skip tuple and return to client */
while(true)
{
/*
* iter = 0 indicate we finish unbatching the vector slot
* and need to read next vector slot
*/
slot = FetchRowFromBatch(ubs);
if(slot)
break;
/* finish current batch, read next batch */
if (!ReadNextVectorSlot(ubs))
return NULL;
}
return slot;
}
static bool
ReadNextVectorSlot(UnbatchState *ubs)
{
TupleTableSlot *slot;
slot = ExecProcNode(ubs->css.ss.ps.lefttree);
if(TupIsNull(slot))
return false;
/* Make sure the tuple is fully deconstructed */
slot_getallattrs(slot);
ubs->ps_ResultVTupleSlot = slot;
ubs->iter = 0;
return true;
}
/*
*
*/
static void
EndUnbatch(CustomScanState *node)
{
PlanState *outerPlan;
outerPlan = outerPlanState(node);
ExecEndNode(outerPlan);
}
static Node *
CreateUnbatchState(CustomScan *custom_plan)
{
UnbatchState *vss = palloc0(sizeof(UnbatchState));
NodeSetTag(vss, T_CustomScanState);
vss->css.methods = &unbatch_exec_methods;
return (Node *) &vss->css;
}
/*
* Add unbatch Node at top to make batch to tuple
*/
Plan *
AddUnbatchNodeAtTop(Plan *node)
{
CustomScan *convert = makeNode(CustomScan);
convert->methods = &unbatch_methods;
convert->scan.plan.lefttree = node;
convert->scan.plan.righttree = NULL;
return &convert->scan.plan;
}
/*
* Initialize vectorscan CustomScan node.
*/
void
InitUnbatch(void)
{
RegisterCustomScanMethods(&unbatch_methods);
}