-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryPlanUtils.h
186 lines (163 loc) · 5.32 KB
/
QueryPlanUtils.h
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
#include <iostream>
#include <string.h>
#include "Function.h"
#include "ParseFunc.h"
#include "ParseTree.h"
#include <string>
using namespace std;
void printFuncOperator(struct FuncOperator *finalFunction) {
cout << "\n Code: " << finalFunction->code;
if (finalFunction->leftOperand != NULL) {
cout << "\n Left Operand Code: " << (finalFunction->leftOperand->code);
cout << "\n Left Operand Value: " << string(finalFunction->leftOperand->value);
}
if (finalFunction->leftOperator != NULL) {
cout << "\n Left Operator: ";
printFuncOperator(finalFunction->leftOperator);
}
if (finalFunction->right != NULL) {
cout << "\n Right Operator: ";
printFuncOperator(finalFunction->right);
}
cout << endl;
}
void printTableList(struct TableList *tables) {
while (tables != NULL) {
cout << "\n Table Name: " << string(tables->tableName);
cout << "\n Table Alias: " << string(tables->aliasAs);
tables = tables->next;
}
cout << endl;
}
void printNameList(struct NameList *nl) {
while (nl != NULL) {
cout << "\n Name: " << string(nl->name);
nl = nl->next;
}
cout << endl;
}
void printOrList(struct OrList *ol) {
while (ol != NULL) {
cout << "\n Comparision Op Code: " << ol->left->code;
cout << "\n Comparision Operand Left: " << ol->left->left->code;
cout << "\n Comparision Operand Left: " << string(ol->left->left->value);
cout << "\n Comparision Operand Right: " << ol->left->right->code;
cout << "\n Comparision Operand Right: " << string(ol->left->right->value);
ol = ol->rightOr;
}
}
void printAndList(struct AndList *al) {
while (al != NULL) {
printOrList(al->left);
al = al->rightAnd;
}
cout << endl;
}
char *removeDot(char *str) {
string temp = string(str);
string attName;
if (temp.find(".") != string::npos) {
attName = temp.substr(temp.find(".") + 1, string::npos);
str = new char[attName.length() + 1];
strcpy(str, attName.c_str());
}
return str;
}
void removeDot(struct Operand *op) {
string temp = string(op->value);
string attName;
if (temp.find(".") != string::npos) {
attName = temp.substr(temp.find(".") + 1, string::npos);
delete[] op->value;
op->value = new char[attName.length() + 1];
strcpy(op->value, attName.c_str());
}
}
void cleanFuncOperator(struct FuncOperator *finalFunction) {
if (finalFunction->leftOperand != NULL) {
finalFunction->leftOperand->value = removeDot(finalFunction->leftOperand->value);
}
if (finalFunction->leftOperator != NULL) {
cleanFuncOperator(finalFunction->leftOperator);
}
if (finalFunction->right != NULL) {
cleanFuncOperator(finalFunction->right);
}
}
void cleanNameList(struct NameList *nl) {
while (nl != NULL) {
nl->name = removeDot(nl->name);
nl = nl->next;
}
cout << endl;
}
void searchAtt(char *attname, Schema *s, Attribute *copyToMe) {
// remove the dot before searching
attname = removeDot(attname);
// search for the current attname in the schema
Attribute *atts = s->GetAtts();
for (int i = 0; i < s->GetNumAtts(); i++) {
if (strcmp(attname, (atts + i)->name) == 0) {
copyToMe->myType = (atts + i)->myType;
copyToMe->name = strdup((atts + i)->name);
return;
}
}
return;
}
// remove the ____._____ from the AndList
void removeMapping(struct OrList *orlist) {
string attName;
while (orlist != NULL) {
if (orlist->left->left->code == NAME) {
removeDot(orlist->left->left);
}
if (orlist->left->right->code == NAME) {
removeDot(orlist->left->right);
}
orlist = orlist->rightOr;
}
}
/*******************************************************************************
* Helper function to print output schema of each node
******************************************************************************/
void printOutputSchema(Schema *s) {
cout << " Output Schema: " << s->schemaName << endl;
for (int i = 0; i < s->GetNumAtts(); i++) {
Attribute *att = s->GetAtts();
cout << " " << att[i].name << ": ";
// cout << " Att" << (i+1) << ": ";
switch (att[i].myType) {
case 0:
cout << "Int" << endl;
break;
case 1:
cout << "Double" << endl;
break;
case 2:
cout << "String" << endl;
break;
}
}
}
void getAttsFromFunc(struct FuncOperator *finalFunction, struct NameList *atts, struct NameList *head) {
if (finalFunction->leftOperand != NULL) {
struct NameList *temp;
if (head == NULL) {
head = new NameList;
head->name = strdup(finalFunction->leftOperand->value);
atts = head;
} else {
temp = new NameList;
temp->name = strdup(finalFunction->leftOperand->value);
atts->next = temp;
atts = temp;
}
}
if (finalFunction->leftOperator != NULL) {
getAttsFromFunc(finalFunction->leftOperator, atts, head);
}
if (finalFunction->right != NULL) {
getAttsFromFunc(finalFunction->right, atts, head);
}
}