-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
2682 lines (2494 loc) · 118 KB
/
Copy pathsolution.cpp
File metadata and controls
2682 lines (2494 loc) · 118 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
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
#include "solution.h"
#include <fstream>
#include <functional>
#include <algorithm>
#include <numeric>
#include <assert.h>
#include <chrono>
using namespace std::chrono;
void Solution::difference(std::vector<join_result>& a, std::vector<join_result>& b) {
std::set<std::vector<int>> a_set;
std::set<std::vector<int>> b_set;
for (auto record : a) {
std::sort(record.id.begin(), record.id.end());
a_set.insert(record.id);
}
for (auto record : b) {
std::sort(record.id.begin(), record.id.end());
b_set.insert(record.id);
}
std::cout << "size difference: " << a_set.size() << '/' << b_set.size() << std::endl;
// assert (a_set.size() <= b_set.size());
std::set<std::vector<int>> diff;
std::set_difference(b_set.begin(), b_set.end(), a_set.begin(), a_set.end(), std::inserter(diff, diff.end()));
for (auto item : diff)
print_vector(item);
}
std::vector<join_result> Solution::deduplicate(std::vector<join_result>& answer) {
std::set<std::vector<int>> unique;
std::vector<join_result> unique_answer;
for (auto record : answer) {
auto it = unique.insert(record.id);
if (it.second)
unique_answer.push_back(record);
}
return unique_answer;
}
std::vector<join_result> Solution::remove_invalid(std::vector<join_result>& answer) {
std::set<std::vector<int>> unique_path;
std::vector<join_result> final_answer;
std::cout << "previous answer size: " << answer.size() << std::endl;
for (const auto& record : answer) {
std::set<int> path(record.id.begin(), record.id.end());
if (path.size() == record.id.size())
if (unique_path.insert(record.id).second)
final_answer.emplace_back(record);
}
return final_answer;
}
bool Solution::is_equal_joined(join_result& a, join_result& b) {
for (int i = 0; i < a.attrs.size(); ++i) {
if (a.attrs[i] != b.attrs[i])
return false;
}
return true;
}
bool Solution::is_equal_joined(join_result& a, join_result& b, std::vector<int>& join_attrs) {
for (auto idx : join_attrs) {
if (a.attrs[idx] != b.attrs[idx])
return false;
}
return true;
}
bool Solution::is_durable(join_result& a, join_result& b, int durability) {
int left = MAX(a.t_start, b.t_start);
int right = MIN(a.t_end, b.t_end);
if (right - left + 1 >= durability)
return true;
else
return false;
}
bool Solution::greater(join_result& a, join_result& b, std::vector<int>& join_attrs) {
for (auto i : join_attrs) {
if (a.attrs[i] == b.attrs[i])
continue;
else
return a.attrs[i] >= b.attrs[i];
}
return true;
}
void Solution::sort_by_join_attrs(std::vector<join_result>& v, std::vector<int>& join_attrs) {
std::sort(v.begin(), v.end(), [&](join_result const& r1, join_result const& r2) -> bool {
for (auto idx : join_attrs) {
if (r1.attrs[idx] == r2.attrs[idx])
continue;
else
return r1.attrs[idx] < r2.attrs[idx];
}
if (r1.t_start == r2.t_start)
return r1.t_end < r2.t_end;
else
return r1.t_start < r2.t_start;
}
);
}
std::map<int, JoinTreeNode*> Solution::create_join_tree(const std::string filename) {
std::map<int, JoinTreeNode*> tree;
std::ifstream fin(filename);
if (!fin)
std::cout << "FILE ERROR" << std::endl;
std::cout << "load join tree structure from " << filename << std::endl;
std::string line;
CSVRow row;
int root_id = 0, node_id = 0, offspring = 0;
while(fin >> row) {
root_id = std::stoi(row[0]);
offspring = std::stoi(row[1]);
tree[root_id] = new JoinTreeNode(root_id);
for (int i = 2; i < 2 + offspring; ++i) {
node_id = std::stoi(row[i]);
if (tree.find(node_id) == tree.end())
tree[node_id] = new JoinTreeNode(node_id);
tree[node_id]->parent = tree[root_id];
tree[root_id]->children.push_back(tree[node_id]);
}
for (int i = 2 + offspring; i < row.size(); ++i)
tree[root_id]->join_attrs.push_back(std::stoi(row[i]));
tree[root_id]->attrs_num = tree[root_id]->join_attrs.size();
}
fin.close();
return tree;
}
std::map<int, YannakakisJoinTreeNode*> Solution::create_yannakakis_join_tree(const std::string filename) {
std::map<int, YannakakisJoinTreeNode*> tree;
std::ifstream fin(filename);
if (!fin)
std::cout << "FILE ERROR" << std::endl;
std::cout << "load join tree structure from " << filename << std::endl;
std::string line;
CSVRow row;
int root_id = 0, node_id = 0, offspring = 0;
while(fin >> row) {
root_id = std::stoi(row[0]);
offspring = std::stoi(row[1]);
tree[root_id] = new YannakakisJoinTreeNode(root_id);
for (int i = 2; i < 2 + offspring; ++i) {
node_id = std::stoi(row[i]);
if (tree.find(node_id) == tree.end())
tree[node_id] = new YannakakisJoinTreeNode(node_id);
tree[node_id]->parent = tree[root_id];
tree[root_id]->children.push_back(tree[node_id]);
}
for (int i = 2 + offspring; i < row.size(); ++i)
tree[root_id]->join_attrs.push_back(std::stoi(row[i]));
tree[root_id]->attrs_num = tree[root_id]->join_attrs.size();
}
fin.close();
return tree;
}
void Solution::delete_tree(std::map<int, JoinTreeNode*>& tree, int root_id) {
for (int i=0; i<tree[root_id]->children.size(); ++i) {
delete_tree(tree, tree[root_id]->children[i]->node_id);
}
delete tree[root_id];
return;
}
void Solution::delete_tree(std::map<int, YannakakisJoinTreeNode*>& tree, int root_id) {
for (int i=0; i<tree[root_id]->children.size(); ++i) {
delete_tree(tree, tree[root_id]->children[i]->node_id);
}
delete tree[root_id];
return;
}
void Solution::tree_viewer(std::map<int, JoinTreeNode*>& tree, int root_id) {
// std::cout << root_id << " (" << tree[root_id]->attrs_num << ')' << std::endl;
std::cout << root_id << " (" ;
for (auto v : tree[root_id]->join_attrs)
std::cout << v << ',';
std::cout << ')' << std::endl;
if (tree[root_id]->children.empty())
return;
for (int i=0; i<tree[root_id]->children.size(); ++i)
tree_viewer(tree, tree[root_id]->children[i]->node_id);
}
void Solution::tree_viewer(std::map<int, YannakakisJoinTreeNode*>& tree, int root_id) {
std::cout << root_id << " (" ;
for (auto v : tree[root_id]->join_attrs)
std::cout << v << ',';
std::cout << ')' << std::endl;
if (tree[root_id]->children.empty())
return;
for (int i=0; i<tree[root_id]->children.size(); ++i)
tree_viewer(tree, tree[root_id]->children[i]->node_id);
}
void Solution::print_tree(std::map<int, YannakakisJoinTreeNode*>& tree) {
for (auto& item : tree) {
YannakakisJoinTreeNode* node = item.second;
std::cout << "table " << node->node_id << " : " << std::endl;
// for (auto& item : node->origin_table) {
// for (int idx : item.second)
// std::cout << idx << " ";
// }
// std::cout << std::endl;
for (auto& item : node->non_dangling_join_values) {
std::cout << "join key: ";
print_vector(item);
}
}
}
void Solution::initialize_tree(std::map<int, YannakakisJoinTreeNode*>& tree) {
// wipe out data
for (auto& item : tree) {
YannakakisJoinTreeNode* node = item.second;
node->non_dangling_join_values.clear();
}
}
void Solution::remove_expired_record(std::map<int, YannakakisJoinTreeNode*>& tree, join_result& record) {
int node_id = record.table_id;
int idx = record.idx;
std::vector<int> key = record.attrs;
// remove the record
tree[node_id]->origin_table[key].erase(idx);
if (tree[node_id]->origin_table[key].empty()) {
tree[node_id]->origin_table.erase(key);
// remove the projected key from its parent and children
// clean upward
if (tree[node_id]->parent != nullptr) {
std::vector<int> common_attrs = get_intersection(tree[node_id]->join_attrs, tree[node_id]->parent->join_attrs);
std::vector<int> new_key = std::vector<int>(this->total_num_attrs, UNDEF);
for (int idx : common_attrs)
new_key[idx] = key[idx];
tree[node_id]->projection_up[new_key].erase(key);
if (tree[node_id]->projection_up[new_key].empty())
tree[node_id]->projection_up.erase(new_key);
}
// clean downward
// for (auto child : tree[node_id]->children) {
for (int i=0; i<tree[node_id]->children.size(); ++i) {
std::vector<int> common_attrs = get_intersection(tree[node_id]->join_attrs, tree[node_id]->children[i]->join_attrs);
std::vector<int> new_key = std::vector<int>(this->total_num_attrs, UNDEF);
for (int idx : common_attrs)
new_key[idx] = key[idx];
tree[node_id]->children[i]->projection_down[new_key].erase(key);
if (tree[node_id]->children[i]->projection_down[new_key].empty())
tree[node_id]->children[i]->projection_down.erase(new_key);
}
}
}
void Solution::add_active_record(std::map<int, YannakakisJoinTreeNode*>& tree, join_result& record) {
int node_id = record.table_id;
std::vector<int> key = record.attrs;
// insert the record
tree[node_id]->origin_table[key].insert(record.idx);
// join attrs project down
for (auto child : tree[node_id]->children) {
std::vector<int> common_attrs = get_intersection(tree[node_id]->join_attrs, child->join_attrs);
std::vector<int> new_key = std::vector<int>(this->total_num_attrs, UNDEF);
for (int idx : common_attrs)
new_key[idx] = key[idx];
child->projection_down[new_key].insert(key);
}
// join attrs project up
if (tree[node_id]->parent != nullptr) {
// std::cout << "node id: " << node_id << " parent id: " << tree[node_id]->parent->node_id << std::endl;
std::vector<int> common_attrs = get_intersection(tree[node_id]->join_attrs, tree[node_id]->parent->join_attrs);
std::vector<int> new_key = std::vector<int>(this->total_num_attrs, UNDEF);
for (int idx : common_attrs)
new_key[idx] = key[idx];
tree[node_id]->projection_up[new_key].insert(key);
}
}
void Solution::prepare_join_values(YannakakisJoinTreeNode* node) {
if (!node->non_dangling_join_values.empty())
return;
for (auto& item : node->origin_table)
node->non_dangling_join_values.insert(item.first);
}
bool Solution::empty_join_tree(std::map<int, YannakakisJoinTreeNode*>& tree) {
for (auto& item : tree) {
YannakakisJoinTreeNode* node = item.second;
if (node->non_dangling_join_values.empty())
return true;
}
return false;
}
void Solution::get_total_active_records(std::map<int, YannakakisJoinTreeNode*>& tree) {
int total_records = 0;
int total_keys = 0;
for (auto& item : tree) {
YannakakisJoinTreeNode* node = item.second;
total_keys += node->origin_table.size();
for (auto& entry : node->origin_table) {
total_records += entry.second.size();
}
}
std::cout << "# of keys: " << total_keys << std::endl;
std::cout << "# of active records: " << total_records << std::endl;
}
void Solution::print_tree(std::map<int, JoinTreeNode*>& tree, int root_id) {
std::cout << root_id << " (" ;
for (auto v : tree[root_id]->join_attrs)
std::cout << v << ',';
std::cout << ')';
for (const auto& item : tree[root_id]->max_t) {
print_vector(item.first);
std::cout << "->" << std::get<0>(item.second) << std::endl;
}
std::cout << std::endl;
if (tree[root_id]->children.empty())
return;
for (int i=0; i<tree[root_id]->children.size(); ++i)
print_tree(tree, tree[root_id]->children[i]->node_id);
}
void Solution::join_output(join_result& target, std::vector<join_result>& buffer, std::vector<int>& union_attrs,
std::vector<join_result>& answer, int durability) {
for (int i=0; i<buffer.size(); ++i) {
join_result record;
record.id = target.id;
for (auto& v : buffer[i].id)
record.id.push_back(v);
record.attrs = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : union_attrs) {
record.attrs[idx] = MAX(record.attrs[idx], target.attrs[idx]);
record.attrs[idx] = MAX(record.attrs[idx], buffer[i].attrs[idx]);
}
record.t_start = MAX(target.t_start, buffer[i].t_start);
record.t_end = MIN(target.t_end, buffer[i].t_end);
record.attr_id = union_attrs;
if (record.t_end - record.t_start + 1 >= durability)
answer.push_back(record);
}
}
void Solution::enumerate_join_output(std::map<int, std::vector<int>>& candidates, int table_id) {
if (table_id >= candidates.size()) {
return;
}
for (int id : candidates[table_id]) {
enumerate_join_output(candidates, table_id+1);
}
return;
}
void Solution::join_output(std::map<int, std::vector<int>>& candidates, std::map<int, std::vector<join_result>>& join_tables,
int table_id, std::vector<int>& temp_answer, std::vector<join_result>& answer) {
if (temp_answer.size() == candidates.size()) {
join_result result;
result.id = temp_answer;
answer.emplace_back(result);
return;
}
for (int id : candidates[table_id]) {
temp_answer.push_back(join_tables[table_id][id].id[0]);
join_output(candidates, join_tables, table_id+1, temp_answer, answer);
temp_answer.pop_back();
}
}
void Solution::join_output(std::map<int, std::vector<int>>& candidates, std::vector<int>& join_order, std::map<int, std::vector<int>>& join_attrs,
std::map<int, std::vector<join_result>>& join_tables, std::vector<join_result>& answer) {
int table_1 = join_order[0], table_2 = join_order[1];
std::vector<int> common_attrs, union_attrs;
common_attrs = get_intersection(join_attrs[table_1], join_attrs[table_2]);
union_attrs = get_union(join_attrs[table_1], join_attrs[table_2]);
std::vector<join_result> left, right;
for (auto id : candidates[table_1])
left.emplace_back(join_tables[table_1][id]);
for (auto id : candidates[table_2])
right.emplace_back(join_tables[table_2][id]);
std::vector<join_result> partial = naive_durable_join(left, right, common_attrs, union_attrs, 0);
for (int i=2; i<join_order.size(); ++i) {
table_2 = join_order[i];
common_attrs = get_intersection(union_attrs, join_attrs[table_2]);
union_attrs = get_union(union_attrs, join_attrs[table_2]);
right.clear();
for (auto id : candidates[table_2])
right.emplace_back(join_tables[table_2][id]);
partial = naive_durable_join(partial, right, common_attrs, union_attrs, 0);
}
for (auto item : partial)
answer.emplace_back(item);
}
int Solution::join_output_size(std::map<int, std::vector<int>>& candidates) {
int product = 1;
for (const auto& item : candidates) {
product *= item.second.size();
}
return product;
}
int Solution::intersection(join_result& a, join_result& b) {
int start = MAX(a.t_start, b.t_start);
int end = MIN(a.t_end, b.t_end);
return end - start + 1;
}
void Solution::FS_join_output(std::vector<join_result>& left, std::vector<join_result>& right, std::vector<int>& union_attrs,
std::vector<join_result>& answer,int durability) {
// std::cout << "LFET:" << std::endl;
// for (auto& item : left)
// item.print();
// std::cout << "RIGHT:" << std::endl;
// for (auto& item : right)
// item.print();
int left_ptr = 0, right_ptr = 0;
int left_start = -1, left_end = -1;
int right_start = -1, right_end = -1;
// records in left and right are already sorted based on their interval startpoints
while (left_ptr < left.size() && right_ptr < right.size()) {
left_start = left[left_ptr].t_start;
right_start = right[right_ptr].t_start;
// process interval join based on left
if (left_start <= right_start) {
left_end = left[left_ptr].t_end;
for (int i = right_ptr; i < right.size(); ++i) {
if (right[i].t_start <= left_end) {
if (intersection(left[left_ptr], right[i]) >= durability) {
join_result record;
for (auto& v : left[left_ptr].id)
record.id.push_back(v);
for (auto& v : right[i].id)
record.id.push_back(v);
record.attrs = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : union_attrs) {
record.attrs[idx] = MAX(record.attrs[idx], left[left_ptr].attrs[idx]);
record.attrs[idx] = MAX(left[left_ptr].attrs[idx], right[i].attrs[idx]);
}
record.attr_id = union_attrs;
record.t_start = MAX(left[left_ptr].t_start, right[i].t_start);
record.t_end = MIN(left[left_ptr].t_end, right[i].t_end);
answer.push_back(record);
// std::cout << "ANSWER:" << std::endl;
// record.print();
}
}
else {
break;
}
}
left_ptr++;
}
// process interval join based on right
else {
right_end = right[right_ptr].t_end;
for (int i = left_ptr; i < left.size(); ++i) {
if (left[i].t_start <= right_end) {
if (intersection(left[i], right[right_ptr]) >= durability) {
join_result record;
for (auto& v : left[i].id)
record.id.push_back(v);
for (auto& v : right[right_ptr].id)
record.id.push_back(v);
record.attrs = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : union_attrs) {
record.attrs[idx] = MAX(record.attrs[idx], left[i].attrs[idx]);
record.attrs[idx] = MAX(left[i].attrs[idx], right[right_ptr].attrs[idx]);
}
record.attr_id = union_attrs;
record.t_start = MAX(left[i].t_start, right[right_ptr].t_start);
record.t_end = MIN(left[i].t_end, right[right_ptr].t_end);
answer.push_back(record);
// std::cout << "ANSWER:" << std::endl;
// record.print();
}
}
else {
break;
}
}
right_ptr++;
}
}
}
bool Solution::check(std::map<int, JoinTreeNode*>& tree, join_result& record, int durability) {
int node_id = record.table_id;
JoinTreeNode *n = tree[node_id];
std::vector<int> key;
while (n != nullptr && n->parent != nullptr) {
key = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : n->parent->join_attrs)
key[idx] = record.attrs[idx];
if (n->parent->max_t.find(key) == n->parent->max_t.end())
return false;
if (std::get<0>(n->parent->max_t[key]) > record.t_end + 1 - durability)
return false;
n = n->parent;
}
return true;
}
std::vector<join_result> Solution::naive_durable_join(std::vector<join_result>& left_table, std::vector<join_result>& right_table,
std::vector<int>& join_attrs, std::vector<int>& union_attrs, int durability) {
std::vector<join_result> answer;
for (int i=0; i<left_table.size(); ++i) {
for (int j=0; j<right_table.size(); ++j) {
if (is_equal_joined(left_table[i], right_table[j], join_attrs)
&& is_durable(left_table[i], right_table[j], durability)) {
join_result item;
for (auto& v : left_table[i].id)
item.id.push_back(v);
for (auto& v : right_table[j].id)
item.id.push_back(v);
item.attrs = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : union_attrs) {
item.attrs[idx] = MAX(item.attrs[idx], left_table[i].attrs[idx]);
item.attrs[idx] = MAX(item.attrs[idx], right_table[j].attrs[idx]);
}
item.t_start = MAX(left_table[i].t_start, right_table[j].t_start);
item.t_end = MIN(left_table[i].t_end, right_table[j].t_end);
item.attr_id = union_attrs;
answer.push_back(item);
//std::cout << i << '/' << left_table.size() << ' ' << j << '/' << right_table.size() << std::endl;
}
// left_table[i].print();
// right_table[j+1].print();
}
}
return answer;
}
std::vector<join_result> Solution::pairwise_durable_join(std::vector<join_result>& left_table, std::vector<join_result>& right_table,
std::vector<int>& join_attrs, std::vector<int>& union_attrs, int durability) {
std::vector<join_result> answer;
std::vector<join_result> active_records;
int left_ptr = 0, right_ptr = 0;
while (left_ptr < left_table.size() && right_ptr < right_table.size()) {
if (is_equal_joined(left_table[left_ptr], right_table[right_ptr], join_attrs)) {
active_records.push_back(right_table[right_ptr]);
right_ptr++;
}
else {
// clean up active_records and output join result
if (!active_records.empty()) {
join_output(left_table[left_ptr], active_records, union_attrs, answer, durability);
int advance = 1;
for (; left_ptr + advance < left_table.size(); ++advance) {
if (is_equal_joined(left_table[left_ptr], left_table[left_ptr + advance], join_attrs)) {
join_output(left_table[left_ptr + advance], active_records, union_attrs, answer, durability);
}
else
break;
}
left_ptr += advance;
active_records.clear();
if (left_ptr >= left_table.size())
break;
}
// navigate for next round of join
else {
// left greater than right
if (greater(left_table[left_ptr], right_table[right_ptr], join_attrs)) {
right_ptr++;
}
// left less than right
else {
left_ptr++;
}
}
}
}
// handle leftover
if (!active_records.empty()) {
if (left_ptr < left_table.size()) {
for (int advance=0; left_ptr + advance < left_table.size(); ++advance) {
if (is_equal_joined(left_table[left_ptr], left_table[left_ptr + advance], join_attrs)) {
join_output(left_table[left_ptr + advance], active_records, union_attrs, answer, durability);
}
}
active_records.clear();
}
}
return answer;
}
std::vector<join_result> Solution::pairwise_durable_temporal_join(std::vector<join_result>& left_table, std::vector<join_result>& right_table, std::vector<int>& join_attrs, std::vector<int>& union_attrs, int durability) {
clock_t ts, te;
clock_t index_time = 0, query_time = 0;
std::vector<join_result> answer;
// build a set of interval trees for left table
std::map<std::vector<int>, intervalVector> left_table_intervals;
std::vector<int> key;
for (int i = 0; i < left_table.size(); ++i) {
key = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : join_attrs)
key[idx] = left_table[i].attrs[idx];
left_table_intervals[key].push_back(interval(left_table[i].t_start, left_table[i].t_end, i));
}
ts = clock();
std::map<std::vector<int>, intervalTree*> left_forest;
for (auto& item : left_table_intervals)
left_forest[item.first] = new intervalTree(std::move(item.second), 16, 1);
te = clock();
index_time += te - ts;
// build a set of interval tress for right table
std::map<std::vector<int>, intervalVector> right_table_intervals;
for (int i = 0; i < right_table.size(); ++i) {
key = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : join_attrs)
key[idx] = right_table[i].attrs[idx];
right_table_intervals[key].push_back(interval(right_table[i].t_start, right_table[i].t_end, i));
}
ts = clock();
std::map<std::vector<int>, intervalTree*> right_forest;
for (auto& item : right_table_intervals)
right_forest[item.first] = new intervalTree(std::move(item.second), 16, 1);
te = clock();
index_time += te - ts;
// tempoal join
auto left_it = left_forest.begin();
auto right_it = right_forest.begin();
std::vector<int> left_key, right_key;
while (left_it != left_forest.end() && right_it != right_forest.end()) {
left_key = left_it->first;
right_key = right_it->first;
int direction = compare(left_key, right_key, join_attrs);
if (direction == 0) {
// do interval tree search
std::vector<join_result> partial_ans;
if (left_table_intervals[left_key].size() <= right_table_intervals[right_key].size()) {
partial_ans = interval_join(left_table_intervals[left_key], right_forest[right_key], durability);
}
else {
partial_ans = interval_join(right_table_intervals[right_key], left_forest[left_key], durability);
}
for (auto& item : partial_ans) {
// assert (item.id.size() == 2);
int left_idx = left_table_intervals[left_key].size() <= right_table_intervals[right_key].size() ? item.id[0] : item.id[1];
int right_idx = left_table_intervals[left_key].size() <= right_table_intervals[right_key].size() ? item.id[1] : item.id[0];
item.attrs = std::vector<int>(this->total_num_attrs, UNDEF);
for (int idx : union_attrs) {
//std::cout << left_table[item.id[0]].attrs.size() << ' ' << right_table[item.id[1]].attrs.size() << std::endl;
item.attrs[idx] = MAX(item.attrs[idx], left_table[left_idx].attrs[idx]);
item.attrs[idx] = MAX(item.attrs[idx], right_table[right_idx].attrs[idx]);
}
std::vector<int> ids;
for (int record_id : left_table[left_idx].id)
ids.push_back(record_id);
for (int record_id : right_table[right_idx].id)
ids.push_back(record_id);
item.id = ids;
item.attr_id = union_attrs;
// print_vector(item.id);
// print_vector(union_attrs);
answer.emplace_back(item);
}
//std::cout << answer.size() << std::endl;
right_it++;
left_it++;
}
else if (direction == -1) {
right_it++;
}
else {
left_it++;
}
}
return answer;
}
std::vector<join_result> Solution::pairwise_forward_scan_temporal_join(std::vector<join_result>& left_table, std::vector<join_result>& right_table,
std::vector<int>& join_attrs, std::vector<int>& union_attrs, int durability) {
std::vector<join_result> answer;
std::vector<join_result> left_records;
std::vector<join_result> right_records;
int left_ptr = 0, right_ptr = 0;
while (left_ptr < left_table.size() && right_ptr < right_table.size()) {
if (is_equal_joined(left_table[left_ptr], right_table[right_ptr], join_attrs)) {
right_records.push_back(right_table[right_ptr]);
right_ptr++;
}
else {
// clean up active_records and output join result
if (!right_records.empty()) {
left_records.push_back(left_table[left_ptr]);
int advance = 1;
for (; left_ptr + advance < left_table.size(); ++advance) {
if (is_equal_joined(left_table[left_ptr], left_table[left_ptr + advance], join_attrs)) {
left_records.push_back(left_table[left_ptr + advance]);
}
else {
break;
}
}
left_ptr += advance;
// Join Output : Forward scan on left & right records
FS_join_output(left_records, right_records, union_attrs, answer, durability);
left_records.clear();
right_records.clear();
if (left_ptr >= left_table.size())
break;
}
// navigate for next round of join
else {
// left greater than right
if (greater(left_table[left_ptr], right_table[right_ptr], join_attrs)) {
right_ptr++;
}
// left less than right
else {
left_ptr++;
}
}
}
}
// handle leftover
if (!right_records.empty()) {
if (left_ptr < left_table.size()) {
for (int advance=0; left_ptr + advance < left_table.size(); ++advance) {
if (is_equal_joined(left_table[left_ptr], left_table[left_ptr + advance], join_attrs)) {
left_records.push_back(left_table[left_ptr + advance]);
}
else {
break;
}
}
// Join Output : Forward scan on left & right records
FS_join_output(left_records, right_records, union_attrs, answer, durability);
right_records.clear();
left_records.clear();
}
}
return answer;
}
std::vector<join_result> Solution::multiway_durable_join_baseline(std::map<int, std::vector<join_result>>& join_tables,
std::vector<int>& join_order, std::map<int, std::vector<int>>& join_attrs, int durability, int flag, bool verbose) {
clock_t ts = clock();
// early termination if one of the table is empty
for (int id : join_order) {
if (join_tables[id].empty())
return std::vector<join_result>();
}
// A baseline solution by pairwise durable join
std::vector<int> common_attrs, union_attrs;
common_attrs = get_intersection(join_attrs[join_order[0]], join_attrs[join_order[1]]);
union_attrs = get_union(join_attrs[join_order[0]], join_attrs[join_order[1]]);
if (verbose) {
std::cout << "===round 1===" << std::endl;
std::cout << "join attrs:";
print_vector(common_attrs);
std::cout << "union attrs:";
print_vector(union_attrs);
}
// initial pair join
std::vector<join_result> partial_ans;
if (flag == 0) {
if (verbose)
std::cout << "*************using sort-merge join*************" << std::endl;
// sort two tables by common join attrs
sort_by_join_attrs(join_tables[join_order[0]], common_attrs);
sort_by_join_attrs(join_tables[join_order[1]], common_attrs);
partial_ans = pairwise_durable_join(join_tables[join_order[0]], join_tables[join_order[1]], common_attrs, union_attrs, durability);
// partial_ans = pairwise_durable_join(join_tables[join_order[0]], join_tables[join_order[1]], common_attrs, union_attrs, 0);
}
else if (flag == 1) {
if (verbose)
std::cout << "*************using temporal join*************" << std::endl;
partial_ans = pairwise_durable_temporal_join(join_tables[join_order[0]], join_tables[join_order[1]], common_attrs, union_attrs, durability);
}
else if (flag == -1) {
if (verbose)
std::cout << "*************using Forward Scan join*************" << std::endl;
// sort two tables by common join attrs
sort_by_join_attrs(join_tables[join_order[0]], common_attrs);
sort_by_join_attrs(join_tables[join_order[1]], common_attrs);
partial_ans = pairwise_forward_scan_temporal_join(join_tables[join_order[0]], join_tables[join_order[1]], common_attrs, union_attrs, durability);
}
else {
if (verbose)
std::cout << "*************using loop-based join*************" << std::endl;
partial_ans = naive_durable_join(join_tables[join_order[0]], join_tables[join_order[1]], common_attrs, union_attrs, durability);
// partial_ans = naive_durable_join(join_tables[join_order[0]], join_tables[join_order[1]], common_attrs, union_attrs, 0);
}
if (verbose)
std::cout << "intermediate result size: " << partial_ans.size() << std::endl;
// pairwise join for rest tables one-by-one
// (need projection on Intermediate join results if necessary)
for (int i=2; i<join_order.size(); ++i) {
if (partial_ans.empty())
return std::vector<join_result>();
common_attrs = get_intersection(partial_ans[0].attr_id, join_attrs[join_order[i]]);
union_attrs = get_union(partial_ans[0].attr_id, join_attrs[join_order[i]]);
if (verbose) {
std::cout << "===round " << i << "===" << std::endl;
std::cout << "join attrs:";
print_vector(common_attrs);
std::cout << "union attrs:";
print_vector(union_attrs);
}
if (flag == 0) {
sort_by_join_attrs(partial_ans, common_attrs);
sort_by_join_attrs(join_tables[join_order[i]], common_attrs);
partial_ans = pairwise_durable_join(partial_ans, join_tables[join_order[i]], common_attrs, union_attrs, durability);
// partial_ans = pairwise_durable_join(partial_ans, join_tables[join_order[i]], common_attrs, union_attrs, 0);
}
else if (flag == 1) {
partial_ans = pairwise_durable_temporal_join(partial_ans, join_tables[join_order[i]], common_attrs, union_attrs, durability);
}
else if (flag == -1) {
sort_by_join_attrs(partial_ans, common_attrs);
sort_by_join_attrs(join_tables[join_order[i]], common_attrs);
partial_ans = pairwise_forward_scan_temporal_join(partial_ans, join_tables[join_order[i]], common_attrs, union_attrs, durability);
}
else {
partial_ans = naive_durable_join(partial_ans, join_tables[join_order[i]], common_attrs, union_attrs, durability);
}
if (verbose)
std::cout << "intermediate result size: " << partial_ans.size() << std::endl;
}
std::vector<join_result> ans;
for (auto item : partial_ans) {
if (item.t_end - item.t_start + 1 >= durability)
ans.push_back(item);
}
if (verbose)
std::cout << "intermediate result size: " << ans.size() << std::endl;
// return results
clock_t te = clock();
if (verbose)
std::cout << "overall time usage: " << (double) (te - ts) / CLOCKS_PER_SEC << std::endl;
return ans;
}
queue_tuple Solution::get_max_from_children(JoinTreeNode* n, std::vector<int>& key) {
int max_value = 0, max_tid = 0, max_rid = 0;
int value, tid, rid;
for (int i=0; i < n->children.size(); ++i) {
// if any of its children has an empty min_heap
// then it shouldn't contribute to its parent, set as infinity
if (n->children[i]->min_t[key].empty())
return std::make_tuple(INT_MAX, -1, -1);
// retrieve min
auto it = n->children[i]->min_t[key].begin();
value = std::get<0>(*it);
tid = std::get<1>(*it);
rid = std::get<2>(*it);
if (value > max_value)
max_value = value, max_tid = tid, max_rid = rid;
}
queue_tuple result(max_value, max_tid, max_rid);
return result;
}
void Solution::enumerate(JoinTreeNode* root, join_result& record, std::map<int, std::vector<join_result>>& join_tables,
std::map<int, std::vector<int>>& candidates, int durability) {
std::vector<int> common_attrs = get_intersection(root->join_attrs, record.attr_id);
std::vector<int> key = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : common_attrs)
key[idx] = record.attrs[idx];
// if the current node is leaf node, output results
if (root->children.size() == 0) {
int table_id = root->node_id;
if (table_id == record.table_id) {
candidates[table_id].push_back(record.idx);
}
else {
// std::cout << root->node_id << ':';
// print_vector(record.attrs);
// std::cout << root->node_id << ':' << root->base_table.size() << std::endl;
for (int rid : root->base_table[key]) {
if (is_equal_joined(join_tables[table_id][rid], record, common_attrs)
&& is_durable(join_tables[table_id][rid], record, durability)) {
candidates[table_id].push_back(join_tables[table_id][rid].idx);
}
}
return;
}
}
else {
// if the common attrs are subset of the record's attrs
// recursively go to its children
if (is_subset(root->join_attrs, record.attr_id)) {
if (std::get<0>(root->max_t[key]) <= record.t_end + 1 - durability) {
for (int i=0; i < root->children.size(); ++i) {
enumerate(root->children[i], record, join_tables, candidates, durability);
}
}
else {
return;
}
}
// else need to get a union of records that join on projection of the common attrs
else {
// locate the position that the superkey matches with the key (which should be a prefix)
auto lower_pos = root->max_t.upper_bound(key);
int greater;
while (lower_pos != root->max_t.end()) {
greater = compare(lower_pos->first, key, common_attrs);
if (greater < 0)
break;
else {
// assert (greater == 0);
if (std::get<0>(lower_pos->second) <= record.t_end + 1 - durability) {
for (int i=0; i < root->children.size(); ++i) {
join_result temp = record;
temp.attrs = lower_pos->first;
temp.attr_id = root->join_attrs;
enumerate(root->children[i], temp, join_tables, candidates, durability);
}
}
lower_pos++;
}
}
// for (auto item : root->max_t) {
// int direction = compare(item.first, key, common_attrs);
// // super key != key, continue to scan
// if (direction == 1)
// continue;
// else if (direction == -1)
// continue;
// // equal! recursively enumerate
// else {
// assert (direction == 0);
// if (std::get<0>(item.second) <= record.t_end - durability) {
// for (int i=0; i < root->children.size(); ++i) {
// join_result temp = record;
// temp.attrs = item.first;
// temp.attr_id = root->join_attrs;
// enumerate(root->children[i], temp, join_tables, candidates, durability);
// }
// }
// }
// }
}
}
}
// TODO: Optimize!!
void Solution::purge(std::map<int, JoinTreeNode*>& tree, join_result& record) {
int table_id = record.table_id;
// int record_id = record.id[0];
int record_id = record.idx;
int t = record.t_start;
queue_tuple prev_min, prev_max;
queue_tuple tuple_key(t, table_id, record_id);
std::vector<int> key, projected_key;
// assert (tree[table_id]->base_table[record.attrs].find(record_id) != tree[table_id]->base_table[record.attrs].end());
// tree[table_id]->base_table[record.attrs].erase(record_id);
projected_key = std::vector<int>(this->total_num_attrs, UNDEF);
for (auto idx : tree[table_id]->parent->join_attrs) {