-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.cpp
108 lines (96 loc) · 3.65 KB
/
join.cpp
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
#include "catalog.h"
#include "query.h"
#include "sort.h"
#include "index.h"
#include <cmath>
#include <cstring>
#include <stdio.h>
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define DOUBLEERROR 1e-07
/*
* Joins two relations
*
* Returns:
* OK on success
* an error code otherwise
*/
Status Operators::Join(const string& result, // Name of the output relation
const int projCnt, // Number of attributes in the projection
const attrInfo projNames[], // List of projection attributes
const attrInfo* attr1, // Left attr in the join predicate
const Operator op, // Predicate operator
const attrInfo* attr2) // Right attr in the join predicate
{
Status res;
AttrDesc attr1Desc;
AttrDesc attr2Desc;
res = attrCat->getInfo(attr1->relName, attr1->attrName, attr1Desc);
if(res != OK) return res;
res = attrCat->getInfo(attr2->relName, attr2->attrName, attr2Desc);
if(res != OK) return res;
AttrDesc descProjNames[projCnt];
// Calc output record len
int size=0;
for (int x=0;x<projCnt;x++) {
size += projNames[x].attrLen;
res = attrCat->getInfo(projNames[x].relName, projNames[x].attrName, descProjNames[x]);
if(res != OK) return res;
}
//Order or precedence: INL, SMJ, SNL
if(op == EQ && (attr1Desc.indexed || attr2Desc.indexed)) // If at least one is indexed and EQ op
{
if(attr2Desc.indexed)
{
//attr2 is indexed so must be right side
res = Operators::INL(result, projCnt, descProjNames, attr1Desc, op, attr2Desc, size);
if(res != OK) return res;
}
else
{
//attr1 is indexed so must be right side
res = Operators::INL(result, projCnt, descProjNames, attr2Desc, op, attr1Desc, size);
if(res != OK) return res;
}
}
else if(op == EQ) // If neither is indexed, but is EQ op
{
res = Operators::SMJ(result, projCnt, descProjNames, attr2Desc, op, attr1Desc, size);
if(res != OK) return res;
}
else
{
res = Operators::SNL(result, projCnt, descProjNames, attr1Desc, op, attr2Desc, size);
if(res != OK) return res;
}
return OK;
}
// Function to compare two record based on the predicate. Returns 0 if the two attributes
// are equal, a negative number if the left (attrDesc1) attribute is less that the right
// attribute, otherwise this function returns a positive number.
int Operators::matchRec(const Record& outerRec, // Left record
const Record& innerRec, // Right record
const AttrDesc& attrDesc1, // Left attribute in the predicate
const AttrDesc& attrDesc2) // Right attribute in the predicate
{
int tmpInt1, tmpInt2;
double tmpFloat1, tmpFloat2, floatDiff;
// Compare the attribute values using memcpy to avoid byte alignment issues
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 DOUBLE:
memcpy(&tmpFloat1, (char *) outerRec.data + attrDesc1.attrOffset, sizeof(double));
memcpy(&tmpFloat2, (char *) innerRec.data + attrDesc2.attrOffset, sizeof(double));
floatDiff = tmpFloat1 - tmpFloat2;
return (fabs(floatDiff) < DOUBLEERROR) ? 0 : floatDiff;
case STRING:
return strncmp(
(char *) outerRec.data + attrDesc1.attrOffset,
(char *) innerRec.data + attrDesc2.attrOffset,
MAX(attrDesc1.attrLen, attrDesc2.attrLen));
}
return 0;
}