-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.C
264 lines (225 loc) · 7.77 KB
/
join.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
#include "catalog.h"
#include "query.h"
#include "sort.h"
#include "joinHT.h"
#include "stdio.h"
#include "stdlib.h"
extern JoinType JoinMethod;
const int matchRec(const Record & outerRec,
const Record & innerRec,
const AttrDesc & attrDesc1,
const AttrDesc & attrDesc2);
/*
* Joins two relations.
*
* Returns:
* OK on success
* an error code otherwise
*/
// implementation of nested loops join goes here
const Status QU_NL_Join(const string & result,
const int projCnt,
const attrInfo projNames[],
const attrInfo *attr1,
const Operator op,
const attrInfo *attr2)
{
Status status;
int resultTupCnt = 0;
if (attr1->attrType != attr2->attrType ||
attr1->attrLen != attr2->attrLen)
{
return ATTRTYPEMISMATCH;
}
// go through the projection list and look up each in the
// attr cat to get an AttrDesc structure (for offset, length, etc)
AttrDesc attrDescArray[projCnt];
for (int i = 0; i < projCnt; i++)
{
Status status = attrCat->getInfo(projNames[i].relName,
projNames[i].attrName,
attrDescArray[i]);
if (status != OK)
{
return status;
}
}
// get AttrDesc structure for the first join attribute
AttrDesc attrDesc1;
status = attrCat->getInfo(attr1->relName,
attr1->attrName,
attrDesc1);
if (status != OK)
{
return status;
}
// get AttrDesc structure for the first join attribute
AttrDesc attrDesc2;
status = attrCat->getInfo(attr2->relName,
attr2->attrName,
attrDesc2);
if (status != OK)
{
return status;
}
// get output record length from attrdesc structures
int reclen = 0;
for (int i = 0; i < projCnt; i++)
{
reclen += attrDescArray[i].attrLen;
}
// open the result table
InsertFileScan resultRel(result, status);
if (status != OK) { return status; }
char outputData[reclen];
Record outputRec;
outputRec.data = (void *) outputData;
outputRec.length = reclen;
// start scan on outer table
HeapFileScan outerScan(string(attrDesc1.relName), status);
if (status != OK) { return status; }
status = outerScan.startScan(0,
0,
STRING,
NULL,
EQ);
if (status != OK) { return status; }
// scan outer table
RID outerRID;
Record outerRec;
Operator myop;
switch(op) {
case EQ: myop=EQ; break;
case GT: myop=LT; break;
case GTE: myop=LTE; break;
case LT: myop=GT; break;
case LTE: myop=GTE; break;
case NE: myop=NE; break;
}
while (outerScan.scanNext(outerRID) == OK)
{
status = outerScan.getRecord(outerRec);
ASSERT(status == OK);
// scan inner table
HeapFileScan innerScan(string(attrDesc2.relName), status);
if (status != OK) { return status; }
status = innerScan.startScan(attrDesc2.attrOffset,
attrDesc2.attrLen,
(Datatype) attrDesc2.attrType,
((char *)outerRec.data) + attrDesc1.attrOffset,
myop);
if (status != OK) { return status; }
RID innerRID;
while (innerScan.scanNext(innerRID) == OK)
{
Record innerRec;
status = innerScan.getRecord(innerRec);
ASSERT(status == OK);
// we have a match, copy data into the output record
int outputOffset = 0;
for (int i = 0; i < projCnt; i++)
{
// copy the data out of the proper input file (inner vs. outer)
if (0 == strcmp(attrDescArray[i].relName, attrDesc1.relName))
{
memcpy(outputData + outputOffset,
(char *)outerRec.data + attrDescArray[i].attrOffset,
attrDescArray[i].attrLen);
}
else // get data from the inner record
{
memcpy(outputData + outputOffset,
(char *)innerRec.data + attrDescArray[i].attrOffset,
attrDescArray[i].attrLen);
}
outputOffset += attrDescArray[i].attrLen;
} // end copy attrs
// add the new record to the output relation
RID outRID;
status = resultRel.insertRecord(outputRec, outRID);
ASSERT(status == OK);
resultTupCnt++;
} // end scan inner
} // end scan outer
printf("tuple nested join produced %d result tuples \n", resultTupCnt);
return OK;
}
// implementation of sort merge join goes here
const Status QU_SM_Join(const string & result,
const int projCnt,
const attrInfo projNames[],
const attrInfo *attr1,
const Operator op,
const attrInfo *attr2)
{
Status status;
int resultTupCnt = 0;
if (attr1->attrType != attr2->attrType ||
attr1->attrLen != attr2->attrLen)
{
return ATTRTYPEMISMATCH;
}
printf("sm join produced %d result tuples \n", resultTupCnt);
return OK;
}
// This is really not a hash join implementation. It is actually a block nested
// loops join that uses hashing on each block of outer tuples read.
// It assumes that blocks of the outer table are read M pages at a time
const Status QU_Hash_Join(const string & result,
const int projCnt,
const attrInfo projNames[],
const attrInfo *attr1,
const Operator op,
const attrInfo *attr2)
{
Status status;
int resultTupCnt = 0;
if (attr1->attrType != attr2->attrType ||
attr1->attrLen != attr2->attrLen)
{
return ATTRTYPEMISMATCH;
}
printf("blockNL Hash join produced %d result tuples \n", resultTupCnt);
return OK;
}
const Status QU_Join(const string & result,
const int projCnt,
const attrInfo projNames[],
const attrInfo *attr1,
const Operator op,
const attrInfo *attr2)
{
if ((JoinMethod == NLJoin) || ((JoinMethod == HashJoin) && (op != EQ)))
{
return QU_NL_Join (result, projCnt, projNames, attr1, op, attr2);
}
else
if (JoinMethod == SMJoin)
{
return QU_SM_Join (result, projCnt, projNames, attr1, op, attr2);
}
else return QU_Hash_Join (result, projCnt, projNames, attr1, op, attr2);
}
const int matchRec(const Record & outerRec,
const Record & innerRec,
const AttrDesc & attrDesc1,
const AttrDesc & attrDesc2)
{
int tmpInt1, tmpInt2;
float tmpFloat1, tmpFloat2;
switch(attrDesc1.attrType)
{
case INTEGER:
memcpy(&tmpInt1, (char *)outerRec.data + attrDesc1.attrOffset, sizeof(int));
memcpy(&tmpInt2, (char *)innerRec.data + attrDesc2.attrOffset, sizeof(int));
return tmpInt1 - tmpInt2;
case FLOAT:
memcpy(&tmpFloat1, (char *)outerRec.data + attrDesc1.attrOffset, sizeof(float));
memcpy(&tmpFloat2, (char *)innerRec.data + attrDesc2.attrOffset, sizeof(float));
return int(tmpFloat1 - tmpFloat2);
case STRING:
return strcmp((char *)outerRec.data + attrDesc1.attrOffset,
(char *)innerRec.data + attrDesc2.attrOffset);
}
return 0;
}