-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.cpp
614 lines (480 loc) · 13.8 KB
/
Global.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
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include "Global.h"
#include "Expression.h"
#include "HW4-expr.h"
//extern Executor executor;
// set<string> tablesInTree;
char* toChars( std::string s ) {
char *c = new char[s.size() + 1];
c[s.size()] = '\0';
memcpy( c, s.c_str(), s.size() );
}
std::vector<std::string> tokenize_string( std::string s, const char * delimiter )
{
char * ss = toChars( s );
char * token;
std::vector<std::string> to_return;
token = strtok( ss, delimiter );
while ( token )
{
to_return.push_back( std::string( token ) );
token = strtok( NULL, delimiter );
}
return to_return;
}
bool compare_without_star( std::string s1, std::string s2 )
{
std::string ss1;
std::string ss2;
if ( s1[0] == '*' )
ss1 = s1.substr( 1, s1.size() );
else
ss1 = s1;
if ( s2[0] == '*' )
ss2 = s2.substr( 1, s2.size() );
else
ss2 = s2;
return ss1.compare( ss2 ) == 0;
}
std::vector<std::string> intercept_cols( std::vector<std::string> vs1, std::vector<std::string> vs2 )
{
std::vector<std::string> to_return;
for( int i = 0; i < vs1.size(); i++ )
{
for( int j = 0; j < vs2.size(); j++ )
{
if ( compare_without_star( vs1[i], vs2[j] ))
{
to_return.push_back( vs1[i] );
}
}
}
return to_return;
}
int get_col_pos(std::vector<std::string> header, std::string col ){
int i = 0;
if( col[0] == '*' || col[0] == '&' )
col = col.substr( 1, col.size() );
for ( std::vector<std::string>::iterator itr = header.begin() ; itr != header.end(); itr++ )
{
if ( compare_without_star( *itr, col ) ){
return i;
}
i++;
}
return -1;
}
std::string join_headers( std::string header1, std::string header2 )
{
std::vector<std::string> vs1 = tokenize_string( header1, "\t" );
std::vector<std::string> vs2 = tokenize_string( header2, "\t" );
std::vector<std::string> intercept = intercept_cols( vs1, vs2 );
std::string header;
for ( int i = 0; i < vs1.size(); i++ )
{
int j = 0;
for ( j = 0; j < intercept.size(); j++ )
{
if ( compare_without_star( vs1[i], intercept[j] ))
break;
}
if ( j == intercept.size() )
{
if ( vs1[i][0] == '*' )
header.append( vs1[i].substr( 1, vs1[i].size() ) );
else
header.append( vs1[i] );
header.push_back( '\t' );
} // current col_name is not in intercept,
}
for ( int i = 0; i < intercept.size(); i++)
{
if ( intercept[i][0] == '*' )
header.append( intercept[i].substr( 1, vs1[i].size() ) );
else
header.append( intercept[i] );
header.push_back( '\t' );
}
for ( int i = 0; i < vs2.size(); i++ )
{
int j = 0;
for ( j = 0; j < intercept.size(); j++ )
{
if ( compare_without_star( vs2[i], intercept[j] ))
break;
}
if ( j == intercept.size() )
{
if ( vs2[i][0] == '*' )
header.append( vs2[i].substr( 1, vs2[i].size() ) );
else
header.append( vs2[i] );
header.push_back( '\t' );
} // j reach the end, current std::string is not in intercept
}
return header;
}
bool is_number(std::string s){
//scan();
std::string::const_iterator it = s.begin();
while ( it != s.end() && isdigit( *it ) )
it++;
return !s.empty() && it == s.end();
}
int get_col_pos( std::string col, std::string header ){
std::vector<std::string> vh = tokenize_string( header, "\t" );
int i = 0;
if( col[0] == '*' || col[0] == '&' )
col = col.substr( 1, col.size() );
for ( std::vector<std::string>::iterator itr = vh.begin() ; itr != vh.end(); itr++ )
{
//std::cout << *itr << " " << col << std::endl;
if ( compare_without_star(*itr, col ) ){
return i;
}
i++;
}
return -1;
}
int arithmetic( expression * ep, std::string header, std::string row )
{
if ( !ep )
{
std::cout << "error in Table::arithmetic()" << std::endl;
return 0;
}
std::vector<std::string> cols = tokenize_string( header, "\t" );
expression* node[2];
node[0] = ep->values[0].ep;
node[1] = ep->values[1].ep;
int v_lhs = 0;
int v_rhs = 0;
int ops[2];
// tokenize row
std::vector<std::string> row_v;
char *s = toChars( row );
char *token;
token = strtok( s, "\t" );
while ( token )
{
row_v.push_back( std::string( token ) );
token = strtok ( NULL, "\t" );
}
if ( row_v.size() != cols.size() )
return false;
// see if nodes are number or std::string,
// if it is std::string, we need to find the actual value in database
for ( int i = 0; i < 2; i++ )
{
if ( node[i]->func != OP_COLNAME && node[i]->func != OP_NUMBER )
return arithmetic( node[i], header, row );
else if ( node[i]->func == OP_NUMBER )
{
ops[i] = node[i]->values[0].num;
}
else if ( node[i]->func == OP_COLNAME )
{
int pos = get_col_pos( node[i]->values[0].data, header );
if ( pos < 0 )
{
std::cout << "column does not exist!" << std::endl;
return false;
}
ops[i] = atoi( row_v[pos].c_str() );
}
}
// start comparison
switch( ep->func ){
case OP_PLUS: return ops[0] + ops[1]; break;
case OP_BMINUS: return ops[0] - ops[1]; break;
case OP_TIMES: return ops[0] * ops[1]; break;
case OP_DIVIDE: return ops[0] / ops[1]; break;
case OP_UMINUS: return -ops[0]; break;
default :
std::cout << "OPERATOR NOT SUPPORTED! " << std::endl;
return false;
break;
}
}
bool IN( std::string col, std::vector<std::string> vals, std::string header, std::string tuple )
{
for( int i = 0; i < vals.size(); i++ )
{
std::string cur_entry = get_entry( header, tuple, col );
//cout << header << " " << vals[i] << endl;
if( cur_entry.compare( vals[i] ) == 0 )
return true;
}
return false;
}
bool AND( expression * ep, std::string header, std::string tuple )
{
bool lhs = false;
bool rhs = false;
if ( ep->values[0].ep->func == OP_AND )
lhs = AND ( ep->values[0].ep, header, tuple );
else if ( ep->values[0].ep->func == OP_OR )
lhs = OR ( ep->values[0].ep, header, tuple );
else
lhs = comparison ( ep->values[0].ep, header, tuple );
if ( ep->values[1].ep->func == OP_AND )
rhs = AND ( ep->values[1].ep, header, tuple );
else if ( ep->values[1].ep->func == OP_OR )
rhs = OR ( ep->values[1].ep, header, tuple );
else
rhs = comparison ( ep->values[1].ep, header, tuple );
return lhs && rhs;
}
bool OR( expression * ep, std::string header, std::string tuple )
{
bool lhs = false;
bool rhs = false;
if ( ep->values[0].ep->func == OP_AND )
lhs = AND ( ep->values[0].ep, header, tuple );
else if ( ep->values[0].ep->func == OP_OR )
lhs = OR ( ep->values[0].ep, header, tuple );
else
lhs = comparison ( ep->values[0].ep, header, tuple );
if ( ep->values[1].ep->func == OP_AND )
rhs = AND ( ep->values[1].ep, header, tuple );
else if ( ep->values[1].ep->func == OP_OR )
rhs = OR ( ep->values[1].ep, header, tuple );
else
rhs = comparison ( ep->values[1].ep, header, tuple );
return lhs || rhs;
}
bool comparison( expression * ep, std::string header, std::string tuple )
{
if ( !ep )
return false;
//if ( ep->func )
if( ep->values[0].ep->func == OP_STRING ||
ep->values[1].ep->func == OP_STRING )
{
//cout << "here" << std::endl;
return string_comparison( ep, header, tuple );
}
else
{
return num_comparison( ep, header, tuple );
}
}
bool string_comparison( expression * ep, std::string header, std::string row )
{
//cout << "here " << std::endl;
if ( !ep )
return false;
std::vector<std::string> cols = tokenize_string( header, "\t" );
//cout << "what?" << std::endl;
expression* node[2];
node[0] = ep->values[0].ep;
node[1] = ep->values[1].ep;
std::string ops[2];
std::vector<std::string> row_v;
char *s = toChars( row );
char *token;
token = strtok( s, "\t" );
while ( token )
{
row_v.push_back( std::string( token ) );
token = strtok ( NULL, "\t" );
}
if ( row_v.size() != cols.size() )
return false;
// see if nodes are number or std::string,
// if it is std::string, we need to find the actual value in database
for ( int i = 0; i < 2; i++ )
{
if ( node[i]->func == OP_STRING )
{
ops[i] = std::string( node[i]->values[0].data );
//cout << "!" << ops[i] << std::endl;
}
else if ( node[i]->func == OP_COLNAME )
{
//cout << node[i]->values[i].name << std::endl;
int pos = get_col_pos( node[i]->values[i].name, header );
if ( pos < 0 )
{
std::cout << "column does not exist!" << std::endl;
return false;
}
ops[i] = std::string( row_v[pos] );
}
}
switch( ep->func ){
case OP_EQUAL: return (ops[0].compare( ops[1] ) == 0 ); break;
case OP_NOTEQ: return (ops[0].compare( ops[1] ) != 0 ); break;
case OP_LEQ: return (ops[0].compare( ops[1] ) <= 0 ); break;
case OP_GEQ: return (ops[0].compare( ops[1] ) >= 0 ); break;
case OP_LT: return (ops[0].compare( ops[1] ) < 0 ); break;
case OP_GT: return (ops[0].compare( ops[1] ) > 0 ); break;
default :
std::cout << "OPERATOR ON std::string NOT SUPPORTED! " << std::endl;
return false;
break;
}
}
bool num_comparison( expression * ep, std::string header, std::string row )
{
if ( !ep )
return false;
std::vector<std::string> cols = tokenize_string( header, "\t" );
expression* node[2];
node[0] = ep->values[0].ep;
node[1] = ep->values[1].ep;
int v_lhs = 0;
int v_rhs = 0;
int ops[2];
// tokenize row
std::vector<std::string> row_v;
char *s = toChars( row );
char *token;
token = strtok( s, "\t" );
while ( token )
{
row_v.push_back( std::string( token ) );
token = strtok ( NULL, "\t" );
}
if ( row_v.size() != cols.size() )
return false;
// see if nodes are number or std::string,
// if it is std::string, we need to find the actual value in database
for ( int i = 0; i < 2; i++ )
{
if ( node[i]->func != OP_COLNAME && node[i]->func != OP_NUMBER )
{
ops[i] = arithmetic( node[i], header, row );
}
else if ( node[i]->func == OP_NUMBER )
{
ops[i] = node[i]->values[0].num;
}
else if ( node[i]->func == OP_COLNAME )
{
int pos = get_col_pos( std::string( node[i]->values[0].name ), header );
if ( pos < 0 )
{
std::cout << "column does not exist!" << std::endl;
return false;
}
ops[i] = atoi( row_v[pos].c_str() );
}
}
// start comparison
switch( ep->func ){
case OP_EQUAL: return ops[0] == ops[1]; break;
case OP_NOTEQ: return ops[0] != ops[1]; break;
case OP_LEQ: return ops[0] <= ops[1]; break;
case OP_GEQ: return ops[0] >= ops[1]; break;
case OP_LT: return ops[0] < ops[1]; break;
case OP_GT: return ops[0] > ops[1]; break;
default :
std::cout << "OPERATOR NOT SUPPORTED! " << std::endl;
return false;
break;
}
}
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
std::string construct_tbl_path( std::string table_name )
{
std::string path( "./data/" );
path.append( table_name );
path.append( ".tbl" );
return path;
}
std::string construct_sta_path( std::string table_name )
{
std::string path( "./data/" );
path.append( table_name );
path.append( ".sta" );
return path;
}
std::string get_entry( std::string tuple, int pos )
{
char * _tuple = toChars( tuple );
// tokenize
char * token = strtok ( _tuple, "\t" );
for ( int i = 0; i < pos; i ++ )
token = strtok( NULL, "\t" );
return string( token );
}
std::string get_entry( std::string header, std::string tuple, std::string col )
{
int pos = get_col_pos( col, header );
std::string to_return;
if ( pos == -1 )
{
return to_return;
}
char * _tuple = toChars( tuple );
// tokenize
char * token = strtok ( _tuple, "\t" );
for ( int i = 0; i < pos; i ++ )
token = strtok( NULL, "\t" );
return string( token );
}
bool parsetreeDistinct(expression *ep){
if(ep->func == OP_PROJECTION &&
ep->values[1].ep->func == OP_DISTINCT){
ep->values[1].ep = ep->values[1].ep->values[0].ep;
return true;
}
if(ep->func == OP_PROJECTION &&
ep->values[1].ep->func == OP_RLIST){
return parsetreeDistinct(ep->values[1].ep);
}
if(ep->func == OP_RLIST &&
ep->values[1].ep != nullptr &&
ep->values[1].ep->func == OP_DISTINCT){
ep->values[1].ep = ep->values[1].ep->values[0].ep;
return true;
}
if(ep->func == OP_RLIST &&
ep->values[1].ep != nullptr &&
ep->values[1].ep->func == OP_RLIST){
return parsetreeDistinct(ep->values[1].ep);
}
return false;
}
bool isDistinct(expr e){
expression *ep = (expression *)e;
return parsetreeDistinct( ep );
}
typedef std::unordered_map<std::string, std::vector<std::string>* > HashMap;
HashMap get_hash( MyItr * itr, std::vector<std::string> keys )
{
HashMap hash_map;
itr->open();
std::string header = itr->first();
// load everything to hashTable from this.tuples
// construct Row for each tuple
std::string cur_string = itr->get_next();
while ( !itr->is_last() )
{
std::string key;
for ( unsigned int i = 0; i < keys.size(); i++ )
key.append( get_entry( header, cur_string, keys[i] ) );
HashMap::iterator map_itr = hash_map.find( key );
if ( map_itr == hash_map.end() )
{
std::vector<std::string> * new_list = new std::vector<std::string>();
new_list->push_back( header );
new_list->push_back( cur_string );
hash_map[key] = new_list;
}
else
{
map_itr->second->push_back( cur_string );
}
cur_string = itr->get_next();
}
return hash_map;
}