-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_query.cpp
More file actions
834 lines (701 loc) · 22.6 KB
/
Copy pathnested_query.cpp
File metadata and controls
834 lines (701 loc) · 22.6 KB
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
#include <bits/stdc++.h>
#include "postfix.h"
using namespace std;
// to store table
typedef vector<vector<string> > vvs;
// to store table and it's name
typedef pair<vector<vector<string> >, string> pvvs;
/*
return an object of pvvs
which stores table in the form of vvs and
table name stored in table_name
*/
pvvs table_to_vvs(string table_name) {
// open file
fstream ob;
ob.open(table_name + ".csv", ios::in);
// throw error if file does not exist
if(! ob.is_open()) {
cerr<<"Table doesn't exist";
throw -1;
}
// line by line read the file and store it in vvs
string line, field;
vvs result;
while(getline(ob, line)) {
vector<string> row;
istringstream s(line);
while(getline(s, field, ',')) {
row.push_back(field);
}
result.push_back(row);
}
// close connection
ob.close();
return make_pair(result, table_name);
}
/*
remove duplicate rows from input table
*/
vvs unique_rows(vvs table) {
vvs result;
result.push_back(table[0]);
set<vector<string> > unique_rows;
for(int i = 1; i < table.size();i++) {
unique_rows.insert(table[i]);
}
for(auto it = unique_rows.begin(); it != unique_rows.end();it++) {
result.push_back(*it);
}
return result;
}
/*
remove spaces from query string
*/
string remove_spaces(string query) {
string result;
for(int i = 0;i < query.size();i++) {
if(query[i] != ' ') result += query[i];
}
return result;
}
/*
parameters
ctable: a table
query: must be of the form "cname1,cname2,cname3,cname4,cname5"
*/
pvvs project(pvvs ctable, string query) {
vvs table = ctable.first;
// map column name to column index
map<string, int> colname_to_number;
for(int i = 0;i < table[0].size(); i++) {
colname_to_number[table[0][i]] = i;
}
vector<int> column_index;
string column; int i = 0;
// iterate through each column mentioned in the query and store its column index
// as per colname_to_number in the vector column_index
while(i < query.size()) {
if(query[i] == ',') {
// column to be projected must be present in the table
if(colname_to_number.find(column) == colname_to_number.end()) {
cerr<<"Column "<<column<<" is not present";
throw -1;
}
column_index.push_back(colname_to_number[column]);
column.clear();
} else {
column += query[i];
}
i++;
}
// column to be projected must be present in the table
if(colname_to_number.find(column) == colname_to_number.end()) {
cerr<<"Column "<<column<<" is not present";
throw -1;
}
column_index.push_back(colname_to_number[column]);
vvs result;
// for each row of table extract values for all the index
// stored in column_index
for(int i = 0;i < table.size();i++) {
vector<string> row;
for(int j = 0;j < column_index.size();j++) {
row.push_back(table[i][column_index[j]]);
}
result.push_back(row);
}
return make_pair(unique_rows(result), "");
}
/*
display table on the console
each row in new line
columns in a row separated by space
*/
void display_vvs(vvs table) {
for(int i=0;i<table.size();i++) {
for(int j=0;j<table[0].size();j++) {
cout<<table[i][j]<<" ";
} cout<<"\n";
}
}
/*
select rows from a given table
which satisfy the conditions stored
in the query
query: e.g. "A>B|(C<D&E=F)|age*5<30"
*/
pvvs select(pvvs ctable, string query) {
vvs table = ctable.first;
set<char> valid_operator;
// operator supported
valid_operator.insert('>');
valid_operator.insert('=');
valid_operator.insert('&');
valid_operator.insert('+');
valid_operator.insert('-');
valid_operator.insert('*');
valid_operator.insert(')');
valid_operator.insert('(');
valid_operator.insert('!');
valid_operator.insert('|');
valid_operator.insert('<');
char sym = 'A'; // alphabet mapped to string
map<char,string> alpha_to_string;
// query : credits * 5 < 17 & (deptname = "Computer" | deptname = "Biology")
// new_query : A * B < C & (D = E | F = G)
string new_query;
string valid;
for(int i = 0;i < query.size();i++) {
if(valid_operator.find(query[i]) == valid_operator.end()) {
valid += query[i];
}
else if(valid.size() > 0) {
alpha_to_string.insert(make_pair(sym,valid));
new_query += sym;
sym++;
valid.clear();
new_query += query[i];
}
else {
new_query += query[i];
}
}
if(valid.size() > 0) {
alpha_to_string.insert(make_pair(sym, valid));
new_query += sym;
sym++;
}
int qsize = new_query.size(), j;
char sym_temp = sym;
// store postfix expression
char postfix[qsize + 1];
for(j = 0;j < qsize; j++) {
postfix[j] = new_query[j];
} postfix[j] = '\0';
infixToPostfix(postfix);
// evaluate the postfix expression
string postfix_exp;
for(int i = 0;postfix[i] != '\0';i++){
postfix_exp += postfix[i];
}
map<string, int> colname_to_number;
for(int i = 0;i < table[0].size(); i++) {
colname_to_number[table[0][i]] = i;
}
vvs result;
result.push_back(table[0]);
for(int i = 1;i < table.size();i++) {
vector<string> row = table[i];
// time to solve postfix expression
stack<char> solve_postfix;
solve_postfix.push(postfix_exp[0]);
int k = 1;
while(not (solve_postfix.empty()) and k < postfix_exp.size()) {
char ch = postfix_exp[k++];
if(valid_operator.count(ch) == 0) {
solve_postfix.push(ch);
} else {
// unary operator
if(ch == '!') {
char zerone = solve_postfix.top();
solve_postfix.pop();
bool value = !(zerone - 48);
solve_postfix.push('0' + value);
} else { // binary operator
char x = solve_postfix.top();
solve_postfix.pop();
char y = solve_postfix.top();
solve_postfix.pop();
string val1; val1 += x;
string val2; val2 += y;
if(alpha_to_string.find(x) != alpha_to_string.end()) {
val1 = alpha_to_string[x];
}
if(alpha_to_string.find(y) != alpha_to_string.end()) {
val2 = alpha_to_string[y];
}
if(colname_to_number.find(val1) != colname_to_number.end()) {
val1 = row[colname_to_number[val1]];
}
if(colname_to_number.find(val2) != colname_to_number.end()) {
val2 = row[colname_to_number[val2]];
}
if(val1[0] == '"'){
val1 = val1.substr(1, val1.size()-2);
}
if(val2[0] == '"'){
val2 = val2.substr(1, val2.size()-2);
}
if(ch == '&') {
solve_postfix.push('0' + ((val1[0]-48) & (val2[0]-48)));
}
if(ch == '|') {
solve_postfix.push('0' + ((val1[0]-48) | (val2[0]-48)));
}
if(ch == '=') {
if(val1 == val2) solve_postfix.push('1');
else solve_postfix.push('0');
}
if(ch == '>') {
int u = atoi(val1.c_str());
int v = atoi(val2.c_str());
bool ans = v > u;
solve_postfix.push(ans + '0');
}
if(ch == '<') {
int u = atoi(val1.c_str());
int v = atoi(val2.c_str());
bool ans = v < u;
solve_postfix.push(ans + '0');
}
if(ch == '+') {
int u = atoi(val1.c_str());
int v = atoi(val2.c_str());
int ans = v + u;
alpha_to_string.insert(make_pair(sym, to_string(ans)));
solve_postfix.push(sym);
sym++;
}
if(ch == '*') {
int u = atoi(val1.c_str());
int v = atoi(val2.c_str());
int ans = v * u;
alpha_to_string.insert(make_pair(sym, to_string(ans)));
solve_postfix.push(sym);
sym++;
}
if(ch == '-') {
int u = atoi(val1.c_str());
int v = atoi(val2.c_str());
int ans = u - v;
alpha_to_string.insert(make_pair(sym, to_string(ans)));
solve_postfix.push(sym);
sym++;
}
}
}
}
if(solve_postfix.top() == '1') {
result.push_back(row);
}
for(char i = sym_temp;i < sym;i++){
alpha_to_string.erase(i);
}
sym = sym_temp;
}
return make_pair(result, "");
}
/*
combined rows of input tables
if they satisfy two constraints:
1. Number of columns in both table must be same
2. Corresponding column name must be same
*/
pvvs _union(pvvs ctable1, pvvs ctable2) {
vvs table1 = ctable1.first;
vvs table2 = ctable2.first;
if(table1[0].size() != table2[0].size()) {
cerr<<"Number of columns must be same";
throw -1;
}
for(int i = 0;i < table1[0].size();i++) {
if(table1[0][i] != table2[0][i]) {
cerr<<"Column "<<table1[0][i]<<" and "<<table2[0][i]<<" are different";
throw -1;
}
}
// 1. create a table composed of rows from both tables
// 2. remove duplicate rows
set<vector<string> > unique_rows;
for(int i = 1;i < table1.size();i++) {
unique_rows.insert(table1[i]);
}
for(int i = 1;i < table2.size();i++) {
unique_rows.insert(table2[i]);
}
vvs result;
result.push_back(table1[0]);
for(auto it = unique_rows.begin(); it != unique_rows.end();it++) {
result.push_back(*it);
}
return make_pair(result, "");
}
/*
remove rows of ctable1 which are present in ctable2
Two constraints must be satisfied
1. Number of columns in both table must be same
2. Corresponding column name must be same
*/
pvvs difference(pvvs ctable1, pvvs ctable2) {
vvs table1 = ctable1.first;
vvs table2 = ctable2.first;
if(table1[0].size() != table2[0].size()) {
cerr<<"Number of columns must be same";
throw -1;
}
for(int i = 0;i < table1[0].size();i++) {
if(table1[0][i] != table2[0][i]) {
cerr<<"Column "<<table1[0][i]<<" and "<<table2[0][i]<<" are different";
throw -1;
}
}
set<vector<string> > rows;
for(int i = 1;i < table2.size();i++) {
rows.insert(table2[i]);
}
vvs result;
result.push_back(table1[0]);
for(int i = 1;i < table1.size();i++) {
if(rows.find(table1[i]) == rows.end()) {
result.push_back(table1[i]);
}
}
return make_pair(result, "");
}
/*
query: table_newname, cname1, cname2, cname3, ...
task: change table name and columns name of the input table
constraints: first entry in the query is new name of the table,
rest entries may or may not be present,
if present number of entries except first one
must be equal to number of columns in the table
*/
pvvs rename(pvvs ctable, string query) {
string column; int i = 0;
vector<string> new_names;
while(i < query.size()) {
if(query[i] == ',') {
new_names.push_back(column);
column.clear();
} else {
column += query[i];
}
i++;
} new_names.push_back(column);
for(int i = 0; i < new_names.size();i++) {
if(new_names[i] == "") {
cerr<<"New table name or column name cannot be empty";
throw -1;
}
}
if(new_names.size() == 1) {
ctable.second = new_names[0];
return ctable;
}
if(ctable.first[0].size() != new_names.size()-1) {
cerr<<"Number of columns in the query != Number of columns in the table";
throw -1;
}
ctable.second = new_names[0];
for(int i=1;i<new_names.size();i++) {
ctable.first[0][i-1] = new_names[i];
}
return ctable;
}
/*
arguments: 2 tables
either column names of both tables are different
or their names are different
*/
pvvs cross_product(pvvs ctable1, pvvs ctable2) {
vvs table1 = ctable1.first; string tname1 = ctable1.second;
vvs table2 = ctable2.first; string tname2 = ctable2.second;
// check for same column_name
set<string> colnames;
for(int i = 0;i < table2[0].size();i++) {
colnames.insert(table2[0][i]);
}
vector<string> new_colnames;
for(int i = 0;i < table1[0].size();i++) {
if(colnames.find(table1[0][i]) != colnames.end()) {
if(tname1 == tname2 || tname1 == "" || tname2 == "") {
cerr<<"Cannot perform crossproduct, same column_name exist in both tables";
cerr<<"\nHint: Use rename operation before cross product";
throw -1;
}
break;
}
}
for(int i = 0;i < table1[0].size();i++) {
if(colnames.find(table1[0][i]) != colnames.end()) {
new_colnames.push_back(tname1 + '.' + table1[0][i]);
} else {
new_colnames.push_back(table1[0][i]);
}
}
colnames.clear();
for(int i = 0;i < table1[0].size();i++) {
colnames.insert(table1[0][i]);
}
for(int i = 0;i < table2[0].size();i++) {
if(colnames.find(table2[0][i]) != colnames.end()) {
new_colnames.push_back(tname2 + '.' + table2[0][i]);
} else {
new_colnames.push_back(table2[0][i]);
}
}
vvs result;
result.push_back(new_colnames);
for(int i = 1;i < table1.size();i++) {
for(int j = 1; j < table2.size();j++) {
vector<string> combine_rows;
combine_rows.reserve(table1[i].size() + table2[j].size());
combine_rows.insert(combine_rows.end(), table1[i].begin(), table1[i].end());
combine_rows.insert(combine_rows.end(), table2[j].begin(), table2[j].end());
result.push_back(combine_rows);
}
}
return make_pair(result, "");
}
pvvs general_query(string query) {
query = remove_spaces(query);
// project operation
if(query.size() > 3 and query[0] == '#') {
if(query[1] != '(') {
cerr<<"Invalid syntax";
throw -1;
}
stack<char> valid_paranth;
valid_paranth.push('(');
int i = 2;
while(i < query.size()) {
if(query[i] == '(') {
valid_paranth.push('(');
}
if(query[i] == ')') {
if(valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
valid_paranth.pop();
}
if(valid_paranth.empty()) break;
i++;
}
if(not valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table = general_query(query.substr(2, i-2));
if(! (i+2 <= query.size()-2)) {
cerr<<"Invalid syntax";
throw -1;
}
if(not(query[i+1] == '(' and query[query.size()-1] == ')')) {
cerr<<"Invalid syntax";
throw -1;
}
return project(temp_table, query.substr(i+2, query.size()-i-3));
}
// select operation
if(query.size() > 3 and query[0] == '$') {
if(query[1] != '(') {
cerr<<"Invalid syntax";
throw -1;
}
stack<char> valid_paranth;
valid_paranth.push('(');
int i = 2;
while(i < query.size()) {
if(query[i] == '(') {
valid_paranth.push('(');
}
if(query[i] == ')') {
if(valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
valid_paranth.pop();
}
if(valid_paranth.empty()) break;
i++;
}
if(not valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table = general_query(query.substr(2, i-2));
if(! (i+2 <= query.size()-2)) {
cerr<<"Invalid syntax";
throw -1;
}
if(not(query[i+1] == '(' and query[query.size()-1] == ')')) {
cerr<<"Invalid syntax";
throw -1;
}
return select(temp_table, query.substr(i+2, query.size()-i-3));
}
// union operation
if(query.size() > 3 and query[0] == 'U') {
if(query[1] != '(') {
cerr<<"Invalid syntax";
throw -1;
}
stack<char> valid_paranth;
valid_paranth.push('(');
int i = 2;
while(i < query.size()) {
if(query[i] == '(') {
valid_paranth.push('(');
}
if(query[i] == ')') {
if(valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
valid_paranth.pop();
}
if(valid_paranth.empty()) break;
i++;
}
if(not valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table1 = general_query(query.substr(2, i-2));
if(! (i+2 <= query.size()-2)) {
cerr<<"Invalid syntax";
throw -1;
}
if(not(query[i+1] == '(' and query[query.size()-1] == ')')) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table2 = general_query(query.substr(i+2, query.size()-i-3));
return _union(temp_table1, temp_table2);
}
// difference operation
if(query.size() > 3 and query[0] == 'D') {
if(query[1] != '(') {
cerr<<"Invalid syntax";
throw -1;
}
stack<char> valid_paranth;
valid_paranth.push('(');
int i = 2;
while(i < query.size()) {
if(query[i] == '(') {
valid_paranth.push('(');
}
if(query[i] == ')') {
if(valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
valid_paranth.pop();
}
if(valid_paranth.empty()) break;
i++;
}
if(not valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table1 = general_query(query.substr(2, i-2));
if(! (i+2 <= query.size()-2)) {
cerr<<"Invalid syntax";
throw -1;
}
if(not(query[i+1] == '(' and query[query.size()-1] == ')')) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table2 = general_query(query.substr(i+2, query.size()-i-3));
return difference(temp_table1, temp_table2);
}
// rename operation
if(query.size() > 3 and query[0] == 'R') {
if(query[1] != '(') {
cerr<<"Invalid syntax";
throw -1;
}
stack<char> valid_paranth;
valid_paranth.push('(');
int i = 2;
while(i < query.size()) {
if(query[i] == '(') {
valid_paranth.push('(');
}
if(query[i] == ')') {
if(valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
valid_paranth.pop();
}
if(valid_paranth.empty()) break;
i++;
}
if(not valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table = general_query(query.substr(2, i-2));
if(! (i+2 <= query.size()-2)) {
cerr<<"Invalid syntax";
throw -1;
}
if(not(query[i+1] == '(' and query[query.size()-1] == ')')) {
cerr<<"Invalid syntax";
throw -1;
}
return rename(temp_table, query.substr(i+2, query.size()-i-3));
}
// cartesian product
if(query.size() > 3 and query[0] == 'C') {
if(query[1] != '(') {
cerr<<"Invalid syntax";
throw -1;
}
stack<char> valid_paranth;
valid_paranth.push('(');
int i = 2;
while(i < query.size()) {
if(query[i] == '(') {
valid_paranth.push('(');
}
if(query[i] == ')') {
if(valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
valid_paranth.pop();
}
if(valid_paranth.empty()) break;
i++;
}
if(not valid_paranth.empty()) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table1 = general_query(query.substr(2, i-2));
if(! (i+2 <= query.size()-2)) {
cerr<<"Invalid syntax";
throw -1;
}
if(not(query[i+1] == '(' and query[query.size()-1] == ')')) {
cerr<<"Invalid syntax";
throw -1;
}
pvvs temp_table2 = general_query(query.substr(i+2, query.size()-i-3));
return cross_product(temp_table1, temp_table2);
}
return table_to_vvs(query);
}
int main() {
string query;
//freopen("queries","r",stdin);
//freopen("output.out","w",stdout);
while(true) {
getline(cin, query);
try {
pvvs table = general_query(query);
display_vvs(table.first);
cout<<"\n$"<<table.second<<"$"<<endl;
} catch (int e) {
cout<<"\n$$\n";
}
}
return 0;
}