-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrel_algebra.cpp
executable file
·1315 lines (1265 loc) · 41.6 KB
/
rel_algebra.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Relational Algebra Implementation in C++
// Ishank Arora, 14074009
// Third Year CSE, IDD
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stack>
#include <vector>
#include <map>
#include <iomanip>
#include <ctype.h>
#include <stdexcept>
using namespace std;
typedef vector <vector<string> > vvs; // Declare aliases for the data structures used
typedef vector <string> vs;
int success; // Global variable to store if the query is successfully executed or not
vs table_list {"students", "courses", "enrollment", "houses", "ip_addresses"}; // List of tables in the database
struct relation // Structure to hold the table name, list of attributes, records and numerical mapping of the attributes
{
string table_name;
vvs records;
map <string, int> att_map;
vs att_list;
};
relation user_query(string query);
relation project(string query, relation data); // Functions corresponding to the different operations
relation rename(string query, relation data);
relation select_util(string query, relation data);
relation select(string query, relation data);
relation union1(relation t1, relation t2);
relation intersection(relation t1, relation t2);
relation cartesian(relation t1, relation t2);
relation set_diff(relation t1, relation t2);
relation join(relation t1, relation t2);
relation division(relation t1, relation t2);
relation sel_max(string query, relation data);
relation sel_min(string query, relation data);
relation sel_avg(string query, relation data);
relation sel_sum(string query, relation data);
relation sel_count(string query, relation data);
// Converts a string to integer, returns -1 if the string cannot be converted
int to_int(string s)
{
int dec;
try
{
dec = stoi(s);
}
catch(std::invalid_argument& e)
{
return -1;
}
return dec;
}
// Strips trailing whitespaces from a string
string strip(string s)
{
while(s[0]==' ')
s.erase(s.begin());
while(s[s.size()-1]==' ')
s.erase(s.size()-1);
return s;
}
// Removes pairs of brackets enclosing a string
string strip_b(string s)
{
while(s[0]=='(' && s[s.size()-1]==')')
{
s.erase(s.size()-1);
s.erase(s.begin());
}
return s;
}
// Splits a string into a vector of strings about the given delimiter
vs split(string s, char delim)
{
vs cols;
stringstream ss(s);
string cell;
while (getline(ss, cell, delim))
{
cell=strip(cell);
cols.push_back(cell);
}
return cols;
}
// The Table class has the contents of the table as private members, so these cannot be accessed directly by the user
// The user can call the member function 'parse_query' with a specific query and obtain the results of its execution
class Table
{
relation data;
int att_count, row_count; // stores the attribute and row count of the table
public:
Table(string t_name) // Constructor initializes row and attribute count to 0 and then calls read_data
{
data.table_name=t_name;
att_count=0;
row_count=0;
read_data("relations/" + t_name + ".csv");
}
void read_data(string file_name) // Reads the attributes and records of the given table from csv file
{
ifstream f1(file_name);
if(!f1.good())
{
success=0;
return;
}
string line, cell;
getline(f1, line);
data.att_list = split(line, ',');
for(; att_count<data.att_list.size(); att_count++)
data.att_map[data.att_list[att_count]]=att_count;
while(getline(f1, line))
{
data.records.push_back(split(line, ','));
row_count++;
}
}
relation parse_query(string query) // Parses the query entered by user and calls the corresponding functions
{
stack <pair <char, string> > proc; // Stack to store the type of operation and the corresponding arguments
while(query!=data.table_name)
{
int s1=query.find("[");
int k=1, s2=s1;
while(k>0)
{
s2++;
if(query[s2]=='[')
k++;
else if(query[s2]==']')
k--;
}
string args=query.substr(s1+1, s2-s1-1); // Arguments are defined inside square brackets []
proc.push(make_pair(toupper(query[0]), args));
s1=query.find("(", s2+1);
s2=query.size()-1;
while(query[s2]!=')')
s2--;
query=query.substr(s1+1, s2-s1-1); // After pushing one operation to the stack, the query is reduced to its input
query=strip(query);
query=strip_b(query);
}
relation out=data;
while(!proc.empty())
{
pair <char, string> x=proc.top(); // Pop the operations from the stack
proc.pop();
//cout<<x.first<<"\t"<<x.second<<endl;
// Depending on the first letter of the operation pushed in the stack, different functions are called
if(x.first=='P')
out=project(x.second, out);
else if(x.first=='S')
out=select_util(x.second, out);
else if(x.first=='R')
out=rename(x.second, out);
else if(x.first=='X')
out=sel_max(x.second, out);
else if(x.first=='N')
out=sel_min(x.second, out);
else if(x.first=='A')
out=sel_avg(x.second, out);
else if(x.first=='T')
out=sel_sum(x.second, out);
else if(x.first=='O')
out=sel_count(x.second, out);
else if(x.first=='U')
{
relation temp = user_query(x.second);
if(success)
out = union1(temp, out);
}
else if(x.first=='I')
{
relation temp = user_query(x.second);
if(success)
out = intersection(temp, out);
}
else if(x.first=='C')
{
relation temp = user_query(x.second);
if(success)
out = cartesian(temp, out);
}
else if(x.first=='D')
{
relation temp = user_query(x.second);
if(success)
out = set_diff(temp, out);
}
else if(x.first=='J')
{
relation temp = user_query(x.second);
if(success)
out = join(temp, out);
}
else if(x.first=='V')
{
relation temp = user_query(x.second);
if(success)
out = division(temp, out);
}
else
{
cout<<"ERROR: Invalid query\n\n"; // Undefined operation
success=0;
}
}
return out;
}
};
// Converts an infix expression involving predicates alongwith AND(^) and OR(|) symbols to postfix
vs ToPostfix(string query)
{
stack<string> S;
vs postfix;
int i, j;
for(i=0; i<query.size(); i++)
{
if(query[i]==' ')
continue;
else if(query[i]=='^' || query[i]=='|')
{
while(!S.empty() && S.top() != "(") // Pops out elements until it encounters an opening parantheses
{
postfix.push_back(S.top());
S.pop();
}
if(query[i]=='^')
S.push("^");
else
S.push("|");
}
else if(query[i]=='(')
S.push("(");
else if(query[i]==')')
{
while(!S.empty() && S.top() != "(")
{
postfix.push_back(S.top());
S.pop();
}
S.pop();
}
else // Finds the predicate until a logical operator or parantheses are found
{
j=i;
while(query[i]!='^' && query[i]!='|' && query[i]!='(' && query[i]!=')' && i<query.size())
i++;
postfix.push_back(query.substr(j, i-j));
i--;
}
}
while(!S.empty())
{
postfix.push_back(S.top());
S.pop();
}
return postfix;
}
// Removes duplicate records from the table
relation remove_dup(relation data)
{
relation out;
out.table_name=data.table_name;
out.att_list=data.att_list;
out.att_map=data.att_map;
int flag;
for(int j=0; j<data.records.size(); j++)
{
flag=0;
for(int k=0; k<j; k++)
if(data.records[j]==data.records[k])
{
flag=1;
break;
}
if(!flag)
out.records.push_back(data.records[j]);
}
return out;
}
// Prints the table with proper formatting
void print_table(relation out)
{
cout<<"\n ";
vector <int> sz(out.att_list.size(), 0);
int j, k, p=0;
for(j=0; j<out.att_list.size(); j++)
{
sz[j] = out.att_list[j].size();
for(k=0; k<out.records.size(); k++)
{
if(out.records[k][j].size()>sz[j])
sz[j]=out.records[k][j].size();
}
p+=sz[j];
}
for(j=0; j<out.att_list.size(); j++)
{
cout << left << setw(sz[j]+1) << setfill(' ') <<out.att_list[j]<<" | ";
}
cout<<"\n";
for(j=0; j<6*out.att_list.size()+p; j++)
cout<<"-";
cout<<"\n ";
for(j=0; j<out.records.size(); j++)
{
for(k=0; k<out.records[j].size(); k++)
cout << left << setw(sz[k]+1) << setfill(' ') <<out.records[j][k]<<" | ";
cout<<"\n ";
}
cout<<"\n";
}
// Utility function to implement multiple predicates in the select operation
relation select_util(string query, relation data)
{
vs cols = ToPostfix(query); // Converts given query to postfix
vector <relation> r;
int i, j, k, flag;
for(i=0; i<cols.size(); i++)
{
if(cols[i]!="^" && cols[i]!="|")
r.push_back(select(strip(cols[i]), data)); // First applies select operation to each of the predicates
else
r.push_back(data);
}
stack<relation> Stk;
for(i=0; i<cols.size(); i++)
{
if(cols[i]=="^" || cols[i]=="|")
{
relation op1 = Stk.top();
Stk.pop();
relation op2 = Stk.top();
Stk.pop();
relation out;
out.att_map=op1.att_map;
out.att_list=op1.att_list;
out.table_name=op1.table_name;
if(cols[i]=="^") // If ^ operator is found, include records common to both relations
{
for(j=0; j<op1.records.size(); j++)
{
flag=0;
for(k=0; k<op2.records.size(); k++)
{
if(op1.records[j]==op2.records[k])
{
flag=1;
break;
}
}
if(flag)
out.records.push_back(op1.records[j]);
}
}
else // If | operator is found, take all the records from both tables and remove duplicates at the end
{
out=op1;
out.records.insert(out.records.end(), op2.records.begin(), op2.records.end());
}
Stk.push(out); // Pushes the result of the operation on the two relations to the stack
}
else
{
Stk.push(r[i]);
}
}
return remove_dup(Stk.top());
}
// Selects the records from a relation based on a given predicate
relation select(string query, relation data)
{
relation out;
int i, isn=0;
if(query.find(">=")!=string::npos) // Greater than or equal to operator
{
query.erase(query.begin()+query.find(">="));
vs cols=split(query, '=');
if(cols[1][0]=='\'' && cols[1][cols[1].size()-1]=='\'') // Checks if the value to be compared against is a constant or not
{
cols[1].erase(cols[1].begin());
cols[1].erase(cols[1].size()-1);
if(data.att_map.count(cols[0])==0) // Displays error if column not present in the mapping
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
if(to_int(cols[1])!=-1)
isn=1;
int c1=data.att_map[cols[0]];
out.att_map=data.att_map;
out.att_list=data.att_list;
for(i=0; i<data.records.size(); i++) // Selects records which obey the predicate
{
if(isn)
{
if(to_int(data.records[i][c1])>=to_int(cols[1]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]>=cols[1])
out.records.push_back(data.records[i]);
}
}
else // If second value is not a constant, it must be another column
{
if(data.att_map.count(cols[0])==0 || data.att_map.count(cols[1])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
int c1=data.att_map[cols[0]], c2=data.att_map[cols[1]];
out.att_map=data.att_map;
out.att_list=data.att_list;
if(to_int(data.records[0][c1])!=-1)
isn=1;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])>=to_int(data.records[i][c2]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]>=data.records[i][c2])
out.records.push_back(data.records[i]);
}
}
}
else if(query.find("<=")!=string::npos) // Less than or equal to operator
{
query.erase(query.begin()+query.find(">="));
vs cols=split(query, '=');
if(cols[1][0]=='\'' && cols[1][cols[1].size()-1]=='\'')
{
cols[1].erase(cols[1].begin());
cols[1].erase(cols[1].size()-1);
if(data.att_map.count(cols[0])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
if(to_int(cols[1])!=-1)
isn=1;
int c1=data.att_map[cols[0]];
out.att_map=data.att_map;
out.att_list=data.att_list;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])<=to_int(cols[1]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]<=cols[1])
out.records.push_back(data.records[i]);
}
}
else
{
if(data.att_map.count(cols[0])==0 || data.att_map.count(cols[1])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
int c1=data.att_map[cols[0]], c2=data.att_map[cols[1]];
out.att_map=data.att_map;
out.att_list=data.att_list;
if(to_int(data.records[0][c1])!=-1)
isn=1;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])<=to_int(data.records[i][c2]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]<=data.records[i][c2])
out.records.push_back(data.records[i]);
}
}
}
else if(query.find("=")!=string::npos) // Equal to operator
{
vs cols=split(query, '=');
if(cols[1][0]=='\'' && cols[1][cols[1].size()-1]=='\'')
{
cols[1].erase(cols[1].begin());
cols[1].erase(cols[1].size()-1);
if(data.att_map.count(cols[0])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
if(to_int(cols[1])!=-1)
isn=1;
int c1=data.att_map[cols[0]];
out.att_map=data.att_map;
out.att_list=data.att_list;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])==to_int(cols[1]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]==cols[1])
out.records.push_back(data.records[i]);
}
}
else
{
if(data.att_map.count(cols[0])==0 || data.att_map.count(cols[1])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
int c1=data.att_map[cols[0]], c2=data.att_map[cols[1]];
out.att_map=data.att_map;
out.att_list=data.att_list;
if(to_int(data.records[0][c1])!=-1)
isn=1;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])==to_int(data.records[i][c2]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]==data.records[i][c2])
out.records.push_back(data.records[i]);
}
}
}
else if(query.find("!")!=string::npos) // Not equal to operator
{
vs cols=split(query, '!');
if(cols[1][0]=='\'' && cols[1][cols[1].size()-1]=='\'')
{
cols[1].erase(cols[1].begin());
cols[1].erase(cols[1].size()-1);
if(data.att_map.count(cols[0])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
if(to_int(cols[1])!=-1)
isn=1;
int c1=data.att_map[cols[0]];
out.att_map=data.att_map;
out.att_list=data.att_list;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])!=to_int(cols[1]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]!=cols[1])
out.records.push_back(data.records[i]);
}
}
else
{
if(data.att_map.count(cols[0])==0 || data.att_map.count(cols[1])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
int c1=data.att_map[cols[0]], c2=data.att_map[cols[1]];
out.att_map=data.att_map;
out.att_list=data.att_list;
if(to_int(data.records[0][c1])!=-1)
isn=1;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])!=to_int(data.records[i][c2]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]!=data.records[i][c2])
out.records.push_back(data.records[i]);
}
}
}
else if(query.find(">")!=string::npos) // Greater than operator
{
vs cols=split(query, '>');
if(cols[1][0]=='\'' && cols[1][cols[1].size()-1]=='\'')
{
cols[1].erase(cols[1].begin());
cols[1].erase(cols[1].size()-1);
if(data.att_map.count(cols[0])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
if(to_int(cols[1])!=-1)
isn=1;
int c1=data.att_map[cols[0]];
out.att_map=data.att_map;
out.att_list=data.att_list;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])>to_int(cols[1]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]>cols[1])
out.records.push_back(data.records[i]);
}
}
else
{
if(data.att_map.count(cols[0])==0 || data.att_map.count(cols[1])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
int c1=data.att_map[cols[0]], c2=data.att_map[cols[1]];
out.att_map=data.att_map;
out.att_list=data.att_list;
if(to_int(data.records[0][c1])!=-1)
isn=1;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])>to_int(data.records[i][c2]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]>data.records[i][c2])
out.records.push_back(data.records[i]);
}
}
}
else if(query.find("<")!=string::npos) // Less than or equal to operator
{
vs cols=split(query, '<');
if(cols[1][0]=='\'' && cols[1][cols[1].size()-1]=='\'')
{
cols[1].erase(cols[1].begin());
cols[1].erase(cols[1].size()-1);
if(data.att_map.count(cols[0])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
if(to_int(cols[1])!=-1)
isn=1;
int c1=data.att_map[cols[0]];
out.att_map=data.att_map;
out.att_list=data.att_list;
for(i=0; i<data.records.size(); i++)
{
if(isn)
{
if(to_int(data.records[i][c1])<to_int(cols[1]))
out.records.push_back(data.records[i]);
}
else if(data.records[i][c1]<cols[1])
out.records.push_back(data.records[i]);
}
}
else
{
if(data.att_map.count(cols[0])==0 || data.att_map.count(cols[1])==0)
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
int c1=data.att_map[cols[0]], c2=data.att_map[cols[1]];
out.att_map=data.att_map;
out.att_list=data.att_list;
if(to_int(data.records[0][c1])!=-1)
isn=1;
for(i=0; i<data.records.size(); i++)
{
if(isn)
if(to_int(data.records[i][c1])<to_int(data.records[i][c2]))
out.records.push_back(data.records[i]);
else if(data.records[i][c1]<data.records[i][c2])
out.records.push_back(data.records[i]);
}
}
}
else // Displays error if none of the operators are present
{
cout<<"ERROR: Not enough arguments\n\n";
success=0;
return out;
}
return remove_dup(out);
}
// Takes projection of a given relation
relation project(string query, relation data)
{
relation out;
vs cols=split(query, ',');
for(int i=0; i<cols.size(); i++)
{
if(data.att_map.count(cols[i])==0) // If a column is not present in relation, displays the error
{
cout<<"ERROR: Column does not exist\n\n";
success=0;
return out;
}
out.att_map[cols[i]]=i;
out.att_list.push_back(cols[i]);
}
out.table_name=data.table_name;
for(int i=0; i<data.records.size(); i++)
{
vs temp;
for(int j=0; j<cols.size(); j++) // Inserts only the required attributes of the records
temp.push_back(data.records[i][data.att_map[cols[j]]]);
out.records.push_back(temp);
}
return remove_dup(out);
}
// Renames the relation, as well as the columns of the relation
relation rename(string query, relation data)
{
int s1=query.find("("), s2=query.find(")");
relation out;
if((s1!=string::npos && s2==string::npos) || (s1==string::npos && s2!=string::npos))
{
cout<<"ERROR: Invalid query\n\n";
success=0;
}
else if(s1==string::npos && s2==string::npos) // Column names not provided, so renames the table only
{
out=data;
out.table_name = strip(query);
}
else
{
int i=0;
while(query[i]==' ')
i++;
if(i==s1)
{
success=0;
cout<<"ERROR: Name for table not entered\n\n"; // Displays the error if the new table name is not entered with the column names
return out;
}
out.table_name = strip(query.substr(0, s1));
query=query.substr(s1+1, s2-s1-1);
vs cols=split(query, ',');
for(i=0; i<cols.size(); i++)
if(cols[i]=="")
cols.erase(cols.begin()+i);
// Displays error if the number of new column names does not match with the number of original column names
if(cols.size()!=data.att_list.size())
{
cout<<"ERROR: Number of columns do not match\n\n";
success=0;
}
else
{
out.records=data.records;
out.att_list=cols;
for(i=0; i<cols.size(); i++)
out.att_map[cols[i]]=i;
}
}
return remove_dup(out);
}
// Takes union of two relations
relation union1(relation t1, relation t2)
{
relation out;
if(t1.att_list!=t2.att_list) // The attribute list of the relations must be the same
{
cout<<"ERROR: The tables are not union compatible\n\n";
success=0;
return out;
}
out.table_name=t1.table_name+" U "+t2.table_name;
out.att_list=t1.att_list;
out.att_map=t1.att_map;
out.records=t1.records;
out.records.insert(out.records.end(), t2.records.begin(), t2.records.end()); // Concatenates the records
return remove_dup(out); // Removes duplicates
}
// Takes intersection of two relations
relation intersection(relation t1, relation t2)
{
relation out;
if(t1.att_list!=t2.att_list) // The attribute list of the relations must be the same
{
cout<<"ERROR: The tables are not union compatible\n\n";
success=0;
return out;
}
out.table_name=t1.table_name+" I "+t2.table_name;
out.att_list=t1.att_list;
out.att_map=t1.att_map;
int flag;
for(int i=0; i<t1.records.size(); i++)
{
flag=0;
for(int j=0; j<t2.records.size(); j++)
{
if(t1.records[i]==t2.records[j])
{
flag=1;
break;
}
}
if(flag) // Inserts a record only if it exists in both relations
out.records.push_back(t1.records[i]);
}
return remove_dup(out);
}
// Takes set difference of two relations
relation set_diff(relation t1, relation t2)
{
relation out;
if(t1.att_list!=t2.att_list) // The attribute list of the relations must be the same
{
cout<<"ERROR: The tables are not union compatible\n\n";
success=0;
return out;
}
out.table_name=t1.table_name+" - "+t2.table_name;
out.att_list=t1.att_list;
out.att_map=t1.att_map;
int flag;
for(int i=0; i<t1.records.size(); i++)
{
flag=0;
for(int j=0; j<t2.records.size(); j++)
{
if(t1.records[i]==t2.records[j])
{
flag=1;
break;
}
}
if(!flag) // Inserts a record only if it exists in the first relation but not the second
out.records.push_back(t1.records[i]);
}
return remove_dup(out);
}
// Takes cartesian product of two relations
relation cartesian(relation t1, relation t2)
{
relation out;
out.table_name=t1.table_name+" X "+t2.table_name;
int dup, i, j;
for(i=0; i<t1.att_list.size(); i++)
{
dup=0;
for(j=0; j<t2.att_list.size(); j++)
{
if(t1.att_list[i]==t2.att_list[j]) // Checks if columns of same names exist in the two relations
{
dup=1;
break;
}
}
if(dup)
{
if(t1.table_name==t2.table_name) // If column names in the two relations are same, the names of the relation should be different
{
cout<<"ERROR: Tables with same column names should have different table names\n\n";
success=0;
return out;
}
out.att_list.push_back(t1.table_name+"."+t1.att_list[i]); // Makes the column names as table_name.column_name
}
else
out.att_list.push_back(t1.att_list[i]);
}
for(i=0; i<t2.att_list.size(); i++) // Performs the checks for attributes in the second relation
{
dup=0;
for(j=0; j<t1.att_list.size(); j++)
{
if(t2.att_list[i]==t1.att_list[j])
{
dup=1;
break;
}
}
if(dup)
out.att_list.push_back(t2.table_name+"."+t2.att_list[i]);
else
out.att_list.push_back(t2.att_list[i]);
}
for(i=0; i<out.att_list.size(); i++)
out.att_map[out.att_list[i]] = i;
for(i=0; i<t1.records.size(); i++)
{
for(j=0; j<t2.records.size(); j++)
{
vs temp = t1.records[i];
temp.insert(temp.end(), t2.records[j].begin(), t2.records[j].end()); // Concatenates the records of the two relations
out.records.push_back(temp);
}
}
return remove_dup(out);
}
// Takes join of the two relations
relation join(relation t1, relation t2)
{
relation out;
out.table_name=t1.table_name+" J "+t2.table_name;
out.att_list=t1.att_list;
out.att_list.insert(out.att_list.end(), t2.att_list.begin(), t2.att_list.end());
vector <pair<int, int> > comm;
int i, j, k, flag;
for(i=0; i<t1.att_list.size(); i++)
{
if(t2.att_map.count(t1.att_list[i])!=0) // Stores the mapping of columns with the same names in the two relations
comm.push_back(make_pair(i, t2.att_map[t1.att_list[i]]));
}
for(i=0; i<t1.records.size(); i++)
{
for(j=0; j<t2.records.size(); j++)
{
flag=1;
for(k=0; k<comm.size(); k++)
{
// Checks that the records have same value for all the common attributes
if(t1.records[i][comm[k].first]!=t2.records[j][comm[k].second])
{
flag=0;
break;
}
}
if(flag)
{
vs temp = t1.records[i];
temp.insert(temp.end(), t2.records[j].begin(), t2.records[j].end());
out.records.push_back(temp);
}
}
}
for(i=0; i<out.att_list.size(); i++)
{
for(j=i+1; j<out.att_list.size(); j++)