/
Operation.h
1398 lines (1233 loc) · 46.8 KB
/
Operation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/
/************************************************************************
*
* @file Operation.h
*
* Defines the Operation of a relational algebra query.
*
***********************************************************************/
#pragma once
#include "AggregateOp.h"
#include "ram/Condition.h"
#include "ram/Expression.h"
#include "ram/Node.h"
#include "ram/Relation.h"
#include "ram/Utils.h"
#include "utility/ContainerUtil.h"
#include "utility/MiscUtil.h"
#include "utility/StreamUtil.h"
#include <cassert>
#include <cstddef>
#include <iosfwd>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
namespace souffle {
/** Pattern type for lower/upper bound */
using RamPattern =
std::pair<std::vector<std::unique_ptr<RamExpression>>, std::vector<std::unique_ptr<RamExpression>>>;
/**
* @class RamOperation
* @brief Abstract class for a relational algebra operation
*/
class RamOperation : public RamNode {
public:
RamOperation* clone() const override = 0;
protected:
void print(std::ostream& os) const override {
print(os, 0);
}
/** @brief Pretty print with indentation */
virtual void print(std::ostream& os, int tabpos) const = 0;
/** @brief Pretty print jump-bed */
static void print(const RamOperation* operation, std::ostream& os, int tabpos) {
operation->print(os, tabpos);
}
friend class RamQuery;
};
/**
* @class RamAbstractParallel
* @brief Abstract class for parallel operation
*/
class RamAbstractParallel {};
/**
* @class RamNestedOperation
* @brief Abstract class for a nesting operations in a loop-nest
*
* In the following example, the nested operation
* of "IF C1" is "IF C2":
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* IF C1
* IF C2
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* TODO (b-scholz): profile text is awkward; perhaps there is a better way of
* storing profile information for RAM operations since
* it is not always used for all RAM operations.
*/
class RamNestedOperation : public RamOperation {
public:
RamNestedOperation(std::unique_ptr<RamOperation> nested, std::string profileText = "")
: nestedOperation(std::move(nested)), profileText(std::move(profileText)) {
assert(nullptr != nestedOperation);
}
RamNestedOperation* clone() const override = 0;
/** @brief Get nested operation */
RamOperation& getOperation() const {
return *nestedOperation;
}
/** @brief Get profile text */
const std::string& getProfileText() const {
return profileText;
}
std::vector<const RamNode*> getChildNodes() const override {
return {nestedOperation.get()};
}
void apply(const RamNodeMapper& map) override {
nestedOperation = map(std::move(nestedOperation));
}
protected:
void print(std::ostream& os, int tabpos) const override {
RamOperation::print(nestedOperation.get(), os, tabpos);
}
bool equal(const RamNode& node) const override {
const auto& other = static_cast<const RamNestedOperation&>(node);
return equal_ptr(nestedOperation, other.nestedOperation) && profileText == other.profileText;
}
/** Nested operation */
std::unique_ptr<RamOperation> nestedOperation;
/** Text used by the profiler */
const std::string profileText;
};
/**
* @class RamTupleOperation
* @brief Abstract class for relation searches and lookups
*/
class RamTupleOperation : public RamNestedOperation {
public:
RamTupleOperation(int ident, std::unique_ptr<RamOperation> nested, std::string profileText = "")
: RamNestedOperation(std::move(nested), std::move(profileText)), identifier(ident) {}
RamTupleOperation* clone() const override = 0;
/** @brief Get identifier */
int getTupleId() const {
return identifier;
}
/** @brief Set identifier */
void setTupleId(int id) {
identifier = id;
}
std::vector<const RamNode*> getChildNodes() const override {
return RamNestedOperation::getChildNodes();
}
protected:
bool equal(const RamNode& node) const override {
const auto& other = static_cast<const RamTupleOperation&>(node);
return RamNestedOperation::equal(other) && identifier == other.identifier;
}
/**
* Identifier for the tuple, corresponding to
* its position in the loop nest
*/
int identifier;
};
/**
* @class RamRelationOperation
* @brief Abstract class for operations on relations
*
* One such operation is a for loop
*/
class RamRelationOperation : public RamTupleOperation {
public:
RamRelationOperation(std::unique_ptr<RamRelationReference> relRef, int ident,
std::unique_ptr<RamOperation> nested, std::string profileText = "")
: RamTupleOperation(ident, std::move(nested), std::move(profileText)),
relationRef(std::move(relRef)) {
assert(relationRef != nullptr && "relation reference is a null-pointer");
}
RamRelationOperation* clone() const override = 0;
/** @brief Get search relation */
const RamRelation& getRelation() const {
return *relationRef->get();
}
void apply(const RamNodeMapper& map) override {
RamTupleOperation::apply(map);
relationRef = map(std::move(relationRef));
}
std::vector<const RamNode*> getChildNodes() const override {
auto res = RamTupleOperation::getChildNodes();
res.push_back(relationRef.get());
return res;
}
protected:
bool equal(const RamNode& node) const override {
const auto& other = static_cast<const RamRelationOperation&>(node);
return RamTupleOperation::equal(other) && equal_ptr(relationRef, other.relationRef);
}
/** Search relation */
std::unique_ptr<RamRelationReference> relationRef;
};
/**
* @class RamScan
* @brief Iterate all tuples of a relation
*
* The following example iterates over all tuples
* in the set A:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* FOR t0 IN A
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamScan : public RamRelationOperation {
public:
RamScan(std::unique_ptr<RamRelationReference> rel, int ident, std::unique_ptr<RamOperation> nested,
std::string profileText = "")
: RamRelationOperation(std::move(rel), ident, std::move(nested), std::move(profileText)) {}
RamScan* clone() const override {
return new RamScan(
souffle::clone(relationRef), getTupleId(), souffle::clone(&getOperation()), getProfileText());
}
protected:
void print(std::ostream& os, int tabpos) const override {
os << times(" ", tabpos);
os << "FOR t" << getTupleId();
os << " IN " << getRelation().getName() << std::endl;
RamRelationOperation::print(os, tabpos + 1);
}
};
/**
* @class RamParallelScan
* @brief Iterate all tuples of a relation in parallel
*
* An example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* PARALLEL FOR t0 IN A
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamParallelScan : public RamScan, public RamAbstractParallel {
public:
RamParallelScan(std::unique_ptr<RamRelationReference> rel, int ident,
std::unique_ptr<RamOperation> nested, std::string profileText = "")
: RamScan(std::move(rel), ident, std::move(nested), profileText) {}
RamParallelScan* clone() const override {
return new RamParallelScan(
souffle::clone(relationRef), getTupleId(), souffle::clone(&getOperation()), getProfileText());
}
protected:
void print(std::ostream& os, int tabpos) const override {
os << times(" ", tabpos);
os << "PARALLEL FOR t" << getTupleId();
os << " IN " << getRelation().getName() << std::endl;
RamRelationOperation::print(os, tabpos + 1);
}
};
/**
* @class Relation Scan with Index
* @brief An abstract class for performing indexed operations
*/
class RamIndexOperation : public RamRelationOperation {
public:
RamIndexOperation(std::unique_ptr<RamRelationReference> r, int ident, RamPattern queryPattern,
std::unique_ptr<RamOperation> nested, std::string profileText = "")
: RamRelationOperation(std::move(r), ident, std::move(nested), std::move(profileText)),
queryPattern(std::move(queryPattern)) {
assert(getRangePattern().first.size() == getRelation().getArity() &&
getRangePattern().second.size() == getRelation().getArity());
for (const auto& pattern : queryPattern.first) {
assert(pattern != nullptr && "pattern is a null-pointer");
}
for (const auto& pattern : queryPattern.second) {
assert(pattern != nullptr && "pattern is a null-pointer");
}
}
/**
* @brief Get range pattern
* @return A pair of std::vector of pointers to RamExpression objects
* These vectors represent the lower and upper bounds for each attribute in the tuple
* <expr1> <= Tuple[level, element] <= <expr2>
* We have that at an index for an attribute, its bounds are <expr1> and <expr2> respectively
* */
std::pair<std::vector<RamExpression*>, std::vector<RamExpression*>> getRangePattern() const {
return std::make_pair(toPtrVector(queryPattern.first), toPtrVector(queryPattern.second));
}
std::vector<const RamNode*> getChildNodes() const override {
auto res = RamRelationOperation::getChildNodes();
for (auto& pattern : queryPattern.first) {
res.push_back(pattern.get());
}
for (auto& pattern : queryPattern.second) {
res.push_back(pattern.get());
}
return res;
}
void apply(const RamNodeMapper& map) override {
RamRelationOperation::apply(map);
for (auto& pattern : queryPattern.first) {
pattern = map(std::move(pattern));
}
for (auto& pattern : queryPattern.second) {
pattern = map(std::move(pattern));
}
}
RamIndexOperation* clone() const override {
RamPattern resQueryPattern;
for (const auto& i : queryPattern.first) {
resQueryPattern.first.emplace_back(i->clone());
}
for (const auto& i : queryPattern.second) {
resQueryPattern.second.emplace_back(i->clone());
}
return new RamIndexOperation(souffle::clone(relationRef), getTupleId(), std::move(resQueryPattern),
souffle::clone(&getOperation()), getProfileText());
}
/** @brief Helper method for printing */
void printIndex(std::ostream& os) const {
// const auto& attrib = getRelation().getAttributeNames();
bool first = true;
for (unsigned int i = 0; i < getRelation().getArity(); ++i) {
// TODO: print proper upper lower/bound
// early exit if no upper/lower bounds are defined
if (isRamUndefValue(queryPattern.first[i].get()) &&
isRamUndefValue(queryPattern.second[i].get())) {
continue;
}
if (first) {
os << " ON INDEX ";
first = false;
} else {
os << " AND ";
}
// both bounds defined and equal => equality
if (!isRamUndefValue(queryPattern.first[i].get()) &&
!isRamUndefValue(queryPattern.second[i].get())) {
// print equality when lower bound = upper bound
if (*(queryPattern.first[i]) == *(queryPattern.second[i])) {
os << "t" << getTupleId() << ".";
os << i << " = ";
os << *(queryPattern.first[i]);
continue;
}
}
// at least one bound defined => inequality
if (!isRamUndefValue(queryPattern.first[i].get()) ||
!isRamUndefValue(queryPattern.second[i].get())) {
if (!isRamUndefValue(queryPattern.first[i].get())) {
os << *(queryPattern.first[i]) << " <= ";
}
os << "t" << getTupleId() << ".";
os << i;
if (!isRamUndefValue(queryPattern.second[i].get())) {
os << " <= " << *(queryPattern.second[i]);
}
continue;
}
}
}
protected:
bool equal(const RamNode& node) const override {
const auto& other = static_cast<const RamIndexOperation&>(node);
return RamRelationOperation::equal(other) &&
equal_targets(queryPattern.first, other.queryPattern.first) &&
equal_targets(queryPattern.second, other.queryPattern.second);
}
/** Values of index per column of table (if indexable) */
RamPattern queryPattern;
};
/**
* @class RamIndexScan
* @brief Search for tuples of a relation matching a criteria
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* FOR t1 IN X ON INDEX t1.c = t0.0
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamIndexScan : public RamIndexOperation {
public:
RamIndexScan(std::unique_ptr<RamRelationReference> r, int ident, RamPattern queryPattern,
std::unique_ptr<RamOperation> nested, std::string profileText = "")
: RamIndexOperation(std::move(r), ident, std::move(queryPattern), std::move(nested),
std::move(profileText)) {}
RamIndexScan* clone() const override {
RamPattern resQueryPattern;
for (const auto& i : queryPattern.first) {
resQueryPattern.first.emplace_back(i->clone());
}
for (const auto& i : queryPattern.second) {
resQueryPattern.second.emplace_back(i->clone());
}
return new RamIndexScan(souffle::clone(relationRef), getTupleId(), std::move(resQueryPattern),
souffle::clone(&getOperation()), getProfileText());
}
protected:
void print(std::ostream& os, int tabpos) const override {
const RamRelation& rel = getRelation();
os << times(" ", tabpos);
os << "FOR t" << getTupleId() << " IN ";
os << rel.getName();
printIndex(os);
os << std::endl;
RamIndexOperation::print(os, tabpos + 1);
}
};
/**
* @class RamParallelIndexScan
* @brief Search for tuples of a relation matching a criteria
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* PARALLEL FOR t1 IN X ON INDEX t1.c = t0.0
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamParallelIndexScan : public RamIndexScan, public RamAbstractParallel {
public:
RamParallelIndexScan(std::unique_ptr<RamRelationReference> rel, int ident, RamPattern queryPattern,
std::unique_ptr<RamOperation> nested, std::string profileText = "")
: RamIndexScan(std::move(rel), ident, std::move(queryPattern), std::move(nested), profileText) {}
RamParallelIndexScan* clone() const override {
RamPattern resQueryPattern;
for (const auto& i : queryPattern.first) {
resQueryPattern.first.emplace_back(i->clone());
}
for (const auto& i : queryPattern.second) {
resQueryPattern.second.emplace_back(i->clone());
}
return new RamParallelIndexScan(souffle::clone(relationRef), getTupleId(), std::move(resQueryPattern),
souffle::clone(&getOperation()), getProfileText());
}
protected:
void print(std::ostream& os, int tabpos) const override {
const RamRelation& rel = getRelation();
os << times(" ", tabpos);
os << "PARALLEL FOR t" << getTupleId() << " IN ";
os << rel.getName();
printIndex(os);
os << std::endl;
RamIndexOperation::print(os, tabpos + 1);
}
};
/**
* @class RamAbstractChoice
* @brief Abstract class for a choice operation
*
* Finding a single tuple, if it exists, such that a condition holds.
*/
class RamAbstractChoice {
public:
RamAbstractChoice(std::unique_ptr<RamCondition> cond) : condition(std::move(cond)) {
assert(condition != nullptr && "Condition is a null-pointer");
}
/** @brief Getter for the condition */
const RamCondition& getCondition() const {
assert(condition != nullptr && "condition of choice is a null-pointer");
return *condition;
}
void apply(const RamNodeMapper& map) {
condition = map(std::move(condition));
}
std::vector<const RamNode*> getChildNodes() const {
return {condition.get()};
}
protected:
bool equal(const RamNode& node) const {
const auto& other = dynamic_cast<const RamAbstractChoice&>(node);
return equal_ptr(condition, other.condition);
}
/** Condition for which a tuple in the relation may hold */
std::unique_ptr<RamCondition> condition;
};
/**
* @class RamChoice
* @brief Find a tuple in a relation such that a given condition holds.
*
* Only one tuple is returned (if one exists), even
* if multiple tuples satisfying the condition exist.
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* CHOICE t1 IN A WHERE (t1.x, t1.y) NOT IN A
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamChoice : public RamRelationOperation, public RamAbstractChoice {
public:
RamChoice(std::unique_ptr<RamRelationReference> rel, size_t ident, std::unique_ptr<RamCondition> cond,
std::unique_ptr<RamOperation> nested, std::string profileText = "")
: RamRelationOperation(std::move(rel), ident, std::move(nested), std::move(profileText)),
RamAbstractChoice(std::move(cond)) {}
void apply(const RamNodeMapper& map) override {
RamRelationOperation::apply(map);
RamAbstractChoice::apply(map);
}
RamChoice* clone() const override {
return new RamChoice(souffle::clone(relationRef), getTupleId(), souffle::clone(condition),
souffle::clone(&getOperation()), getProfileText());
}
std::vector<const RamNode*> getChildNodes() const override {
return {nestedOperation.get(), relationRef.get(), RamAbstractChoice::getChildNodes().at(0)};
}
protected:
void print(std::ostream& os, int tabpos) const override {
os << times(" ", tabpos);
os << "CHOICE t" << getTupleId();
os << " IN " << getRelation().getName();
os << " WHERE " << getCondition();
os << std::endl;
RamRelationOperation::print(os, tabpos + 1);
}
bool equal(const RamNode& node) const override {
const auto& other = static_cast<const RamChoice&>(node);
return RamRelationOperation::equal(other) && RamAbstractChoice::equal(other);
}
};
/**
* @class ParallelRamChoice
* @brief Find a tuple in a relation such that a given condition holds in parallel.
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* PARALLEL CHOICE t1 IN A WHERE (t1.x, t1.y) NOT IN A
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamParallelChoice : public RamChoice, public RamAbstractParallel {
public:
RamParallelChoice(std::unique_ptr<RamRelationReference> rel, size_t ident,
std::unique_ptr<RamCondition> cond, std::unique_ptr<RamOperation> nested,
std::string profileText = "")
: RamChoice(std::move(rel), ident, std::move(cond), std::move(nested), profileText) {}
RamParallelChoice* clone() const override {
return new RamParallelChoice(souffle::clone(relationRef), getTupleId(), souffle::clone(condition),
souffle::clone(&getOperation()), getProfileText());
}
protected:
void print(std::ostream& os, int tabpos) const override {
os << times(" ", tabpos);
os << "PARALLEL CHOICE t" << getTupleId();
os << " IN " << getRelation().getName();
os << " WHERE " << getCondition();
os << std::endl;
RamRelationOperation::print(os, tabpos + 1);
}
};
/**
* @class RamIndexChoice
* @brief Use an index to find a tuple in a relation such that a given condition holds.
*
* Only one tuple is returned (if one exists), even
* if multiple tuples satisfying the condition exist.
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* CHOICE A AS t1 ON INDEX t1.x=10 AND t1.y = 20
* WHERE (t1.x, t1.y) NOT IN A
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamIndexChoice : public RamIndexOperation, public RamAbstractChoice {
public:
RamIndexChoice(std::unique_ptr<RamRelationReference> r, int ident, std::unique_ptr<RamCondition> cond,
RamPattern queryPattern, std::unique_ptr<RamOperation> nested, std::string profileText = "")
: RamIndexOperation(std::move(r), ident, std::move(queryPattern), std::move(nested),
std::move(profileText)),
RamAbstractChoice(std::move(cond)) {
assert(getRangePattern().first.size() == getRelation().getArity());
assert(getRangePattern().second.size() == getRelation().getArity());
}
void apply(const RamNodeMapper& map) override {
RamRelationOperation::apply(map);
for (auto& pattern : queryPattern.first) {
pattern = map(std::move(pattern));
}
for (auto& pattern : queryPattern.second) {
pattern = map(std::move(pattern));
}
RamAbstractChoice::apply(map);
}
std::vector<const RamNode*> getChildNodes() const override {
auto res = RamIndexOperation::getChildNodes();
res.push_back(RamAbstractChoice::getChildNodes().at(0));
return res;
}
RamIndexChoice* clone() const override {
RamPattern resQueryPattern;
for (const auto& i : queryPattern.first) {
resQueryPattern.first.emplace_back(i->clone());
}
for (const auto& i : queryPattern.second) {
resQueryPattern.second.emplace_back(i->clone());
}
auto* res = new RamIndexChoice(souffle::clone(relationRef), getTupleId(), souffle::clone(condition),
std::move(resQueryPattern), souffle::clone(&getOperation()), getProfileText());
return res;
}
protected:
void print(std::ostream& os, int tabpos) const override {
const RamRelation& rel = getRelation();
os << times(" ", tabpos);
os << "CHOICE " << rel.getName() << " AS t" << getTupleId();
printIndex(os);
os << " WHERE " << getCondition();
os << std::endl;
RamIndexOperation::print(os, tabpos + 1);
}
bool equal(const RamNode& node) const override {
const auto& other = static_cast<const RamIndexChoice&>(node);
return RamIndexOperation::equal(other) && RamAbstractChoice::equal(other);
}
};
/**
* @class RamParallelIndexChoice
* @brief Use an index to find a tuple in a relation such that a given condition holds in parallel.
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* QUERY
* ...
* PARALLEL CHOICE A AS t1 ON INDEX t1.x=10 AND t1.y = 20
* WHERE (t1.x, t1.y) NOT IN A
* ...
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamParallelIndexChoice : public RamIndexChoice, public RamAbstractParallel {
public:
RamParallelIndexChoice(std::unique_ptr<RamRelationReference> r, int ident,
std::unique_ptr<RamCondition> cond, RamPattern queryPattern, std::unique_ptr<RamOperation> nested,
std::string profileText = "")
: RamIndexChoice(std::move(r), ident, std::move(cond), std::move(queryPattern), std::move(nested),
profileText) {}
RamParallelIndexChoice* clone() const override {
RamPattern resQueryPattern;
for (const auto& i : queryPattern.first) {
resQueryPattern.first.emplace_back(i->clone());
}
for (const auto& i : queryPattern.second) {
resQueryPattern.second.emplace_back(i->clone());
}
auto* res = new RamParallelIndexChoice(souffle::clone(relationRef), getTupleId(),
souffle::clone(condition), std::move(resQueryPattern), souffle::clone(&getOperation()),
getProfileText());
return res;
}
protected:
void print(std::ostream& os, int tabpos) const override {
const RamRelation& rel = getRelation();
os << times(" ", tabpos);
os << "PARALLEL CHOICE " << rel.getName() << " AS t" << getTupleId();
printIndex(os);
os << " WHERE " << getCondition();
os << std::endl;
RamIndexOperation::print(os, tabpos + 1);
}
};
/**
* @class RamAbstractAggregate
* @brief Abstract class for aggregation
*
* A particular function (e.g. MIN) is applied given a
* that a condition holds
*/
class RamAbstractAggregate {
public:
RamAbstractAggregate(
AggregateOp fun, std::unique_ptr<RamExpression> expr, std::unique_ptr<RamCondition> cond)
: function(fun), expression(std::move(expr)), condition(std::move(cond)) {
assert(condition != nullptr && "Condition is a null-pointer");
assert(expression != nullptr && "Expression is a null-pointer");
}
virtual ~RamAbstractAggregate() = default;
/** @brief Get condition */
const RamCondition& getCondition() const {
assert(condition != nullptr && "Condition of aggregate is a null-pointer");
return *condition;
}
/** @brief Get aggregation function */
AggregateOp getFunction() const {
return function;
}
/** @brief Get target expression */
const RamExpression& getExpression() const {
assert(expression != nullptr && "Expression of aggregate is a null-pointer");
return *expression;
}
std::vector<const RamNode*> getChildNodes() const {
return {expression.get(), condition.get()};
}
protected:
void print(std::ostream& os, int /* tabpos */) const {
switch (function) {
case AggregateOp::MIN:
case AggregateOp::FMIN:
case AggregateOp::UMIN: os << "min "; break;
case AggregateOp::MAX:
case AggregateOp::UMAX:
case AggregateOp::FMAX: os << "max "; break;
case AggregateOp::SUM:
case AggregateOp::FSUM:
case AggregateOp::USUM: os << "sum "; break;
case AggregateOp::COUNT: os << "count "; break;
case AggregateOp::MEAN: os << "mean "; break;
}
if (function != AggregateOp::COUNT) {
os << *expression << " ";
}
}
protected:
bool equal(const RamNode& node) const {
const auto& other = dynamic_cast<const RamAbstractAggregate&>(node);
return function == other.function && equal_ptr(expression, other.expression) &&
equal_ptr(condition, other.condition);
}
/** Aggregation function */
AggregateOp function;
/** Aggregation expression */
std::unique_ptr<RamExpression> expression;
/** Aggregation tuple condition */
std::unique_ptr<RamCondition> condition;
};
/**
* @class RamAggregate
* @brief Aggregation function applied on some relation
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* t0.0 = COUNT FOR ALL t0 IN A
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Applies the function COUNT to determine the number
* of elements in A.
*/
class RamAggregate : public RamRelationOperation, public RamAbstractAggregate {
public:
RamAggregate(std::unique_ptr<RamOperation> nested, AggregateOp fun,
std::unique_ptr<RamRelationReference> relRef, std::unique_ptr<RamExpression> expression,
std::unique_ptr<RamCondition> condition, int ident)
: RamRelationOperation(std::move(relRef), ident, std::move(nested)),
RamAbstractAggregate(fun, std::move(expression), std::move(condition)) {}
std::vector<const RamNode*> getChildNodes() const override {
auto res = RamRelationOperation::getChildNodes();
auto children = RamAbstractAggregate::getChildNodes();
res.insert(res.end(), children.begin(), children.end());
return res;
}
RamAggregate* clone() const override {
return new RamAggregate(souffle::clone(&getOperation()), function, souffle::clone(relationRef),
souffle::clone(expression), souffle::clone(condition), getTupleId());
}
void apply(const RamNodeMapper& map) override {
RamRelationOperation::apply(map);
condition = map(std::move(condition));
expression = map(std::move(expression));
}
protected:
void print(std::ostream& os, int tabpos) const override {
os << times(" ", tabpos);
os << "t" << getTupleId() << ".0=";
RamAbstractAggregate::print(os, tabpos);
os << "FOR ALL t" << getTupleId() << " ∈ " << getRelation().getName();
if (!isRamTrue(condition.get())) {
os << " WHERE " << getCondition();
}
os << std::endl;
RamRelationOperation::print(os, tabpos + 1);
}
bool equal(const RamNode& node) const override {
const auto& other = static_cast<const RamAggregate&>(node);
return RamRelationOperation::equal(other) && RamAbstractAggregate::equal(node);
}
};
/**
* @class RamParallelAggregate
* @brief Parallel Aggregation function applied on some relation
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* PARALLEL t0.0 = COUNT FOR ALL t0 IN A
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Applies the function PARALLEL COUNT to determine the number
* of elements in A.
*/
class RamParallelAggregate : public RamAggregate, public RamAbstractParallel {
public:
RamParallelAggregate(std::unique_ptr<RamOperation> nested, AggregateOp fun,
std::unique_ptr<RamRelationReference> relRef, std::unique_ptr<RamExpression> expression,
std::unique_ptr<RamCondition> condition, int ident)
: RamAggregate(std::move(nested), fun, std::move(relRef), std::move(expression),
std::move(condition), ident) {}
RamParallelAggregate* clone() const override {
return new RamParallelAggregate(souffle::clone(&getOperation()), function,
souffle::clone(relationRef), souffle::clone(expression), souffle::clone(condition),
identifier);
}
protected:
void print(std::ostream& os, int tabpos) const override {
os << times(" ", tabpos);
os << "PARALLEL t" << getTupleId() << ".0=";
RamAbstractAggregate::print(os, tabpos);
os << "FOR ALL t" << getTupleId() << " ∈ " << getRelation().getName();
if (!isRamTrue(condition.get())) {
os << " WHERE " << getCondition();
}
os << std::endl;
RamRelationOperation::print(os, tabpos + 1);
}
};
/**
* @class RamIndexAggregate
* @brief Indexed aggregation on a relation. The index allows us to iterate over a restricted range
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* t0.0=sum t0.1 SEARCH t0 ∈ S ON INDEX t0.0 = number(1)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamIndexAggregate : public RamIndexOperation, public RamAbstractAggregate {
public:
RamIndexAggregate(std::unique_ptr<RamOperation> nested, AggregateOp fun,
std::unique_ptr<RamRelationReference> relRef, std::unique_ptr<RamExpression> expression,
std::unique_ptr<RamCondition> condition, RamPattern queryPattern, int ident)
: RamIndexOperation(std::move(relRef), ident, std::move(queryPattern), std::move(nested)),
RamAbstractAggregate(fun, std::move(expression), std::move(condition)) {}
std::vector<const RamNode*> getChildNodes() const override {
auto res = RamIndexOperation::getChildNodes();
auto children = RamAbstractAggregate::getChildNodes();
res.insert(res.end(), children.begin(), children.end());
return res;
}
RamIndexAggregate* clone() const override {
RamPattern pattern;
for (const auto& i : queryPattern.first) {
pattern.first.emplace_back(i->clone());
}
for (const auto& i : queryPattern.second) {
pattern.second.emplace_back(i->clone());
}
return new RamIndexAggregate(souffle::clone(&getOperation()), function, souffle::clone(relationRef),
souffle::clone(expression), souffle::clone(condition), std::move(pattern), getTupleId());
}
void apply(const RamNodeMapper& map) override {
RamIndexOperation::apply(map);
condition = map(std::move(condition));
expression = map(std::move(expression));
}
protected:
void print(std::ostream& os, int tabpos) const override {
os << times(" ", tabpos);
os << "t" << getTupleId() << ".0=";
RamAbstractAggregate::print(os, tabpos);
os << "SEARCH t" << getTupleId() << " ∈ " << getRelation().getName();
printIndex(os);
if (!isRamTrue(condition.get())) {
os << " WHERE " << getCondition();
}
os << std::endl;
RamIndexOperation::print(os, tabpos + 1);
}
bool equal(const RamNode& node) const override {
const auto& other = static_cast<const RamIndexAggregate&>(node);
return RamIndexOperation::equal(other) && RamAbstractAggregate::equal(other);
}
};
/**
* @class RamParallelIndexAggregate
* @brief Aggregate over values of a relation using an index in parallel
*
* For example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* t0.0=sum t0.1 SEARCH t0 ∈ S ON INDEX t0.0 = number(1)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class RamParallelIndexAggregate : public RamIndexAggregate, public RamAbstractParallel {
public:
RamParallelIndexAggregate(std::unique_ptr<RamOperation> nested, AggregateOp fun,
std::unique_ptr<RamRelationReference> relRef, std::unique_ptr<RamExpression> expression,
std::unique_ptr<RamCondition> condition, RamPattern queryPattern, int ident)
: RamIndexAggregate(std::move(nested), fun, std::move(relRef), std::move(expression),
std::move(condition), std::move(queryPattern), ident) {}
RamParallelIndexAggregate* clone() const override {
RamPattern pattern;
for (const auto& i : queryPattern.first) {
pattern.first.emplace_back(i->clone());
}
for (const auto& i : queryPattern.second) {
pattern.second.emplace_back(i->clone());
}
return new RamParallelIndexAggregate(souffle::clone(&getOperation()), function,
souffle::clone(relationRef), souffle::clone(expression), souffle::clone(condition),
std::move(pattern), getTupleId());
}
protected:
void print(std::ostream& os, int tabpos) const override {
os << times(" ", tabpos);
os << "PARALLEL t" << getTupleId() << ".0=";