-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparison.cc
More file actions
747 lines (617 loc) · 20.5 KB
/
Copy pathComparison.cc
File metadata and controls
747 lines (617 loc) · 20.5 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
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include "Swap.h"
#include "Config.h"
#include "Schema.h"
#include "ParseTree.h"
#include "Record.h"
#include "Comparison.h"
using namespace std;
Comparison::Comparison(const Comparison& copy_me) :
operand1(copy_me.operand1), whichAtt1(copy_me.whichAtt1),
operand2(copy_me.operand2), whichAtt2(copy_me.whichAtt2),
attType(copy_me.attType), op(copy_me.op) {}
Comparison& Comparison::operator=(const Comparison& copy_me) {
// handle self-assignment first
if (this == ©_me) return *this;
operand1 = copy_me.operand1; whichAtt1 = copy_me.whichAtt1;
operand2 = copy_me.operand2; whichAtt2 = copy_me.whichAtt2;
attType = copy_me.attType; op = copy_me.op;
return *this;
}
void Comparison::Swap(Comparison& _swap) {
SWAP(operand1, _swap.operand1);
SWAP(operand2, _swap.operand2);
SWAP(whichAtt1, _swap.whichAtt1);
SWAP(whichAtt2, _swap.whichAtt2);
SWAP(attType, _swap.attType);
SWAP(op, _swap.op);
}
bool Comparison :: Run (Record& left, Record& right) {
char* val1 = NULL;
char* val2 = NULL;
char* left_bits = left.GetBits();
char* lit_bits = right.GetBits();
// first get a pointer to the first value to compare
if (operand1 == Left) val1 = left_bits + ((int *) left_bits)[whichAtt1 + 1];
else val1 = lit_bits + ((int *) lit_bits)[whichAtt1 + 1];
// next get a pointer to the second value to compare
if (operand2 == Left) val2 = left_bits + ((int *) left_bits)[whichAtt2 + 1];
else val2 = lit_bits + ((int *) lit_bits)[whichAtt2 + 1];
int val1Int, val2Int, tempResult;
double val1Double, val2Double;
// now check the type and the comparison operation
switch (attType) {
// first case: we are dealing with integers
case Integer: {
val1Int = *((int *) val1);
val2Int = *((int *) val2);
// and check the operation type in order to actually do the comparison
switch (op) {
case LessThan: return (val1Int < val2Int);
case GreaterThan: return (val1Int > val2Int);
default: return (val1Int == val2Int);
}
}
// second case: dealing with doubles
case Float: {
val1Double = *((double *) val1);
val2Double = *((double *) val2);
// and check the operation type in order to actually do the comparison
switch (op) {
case LessThan: return (val1Double < val2Double);
case GreaterThan: return (val1Double > val2Double);
default: return (val1Double == val2Double);
}
}
// final case: dealing with strings
default: {
// so check the operation type in order to actually do the comparison
tempResult = strcmp (val1, val2);
switch (op) {
case LessThan: return (tempResult < 0);
case GreaterThan: return (tempResult > 0);
default: return (tempResult == 0);
}
}
}
}
ostream& operator<<(ostream& _os, Comparison& _c) {
if (_c.operand1 == Left) _os << "Left[" << _c.whichAtt1 << "]";
else if (_c.operand1 == Right) _os << "Right[" << _c.whichAtt1 << "]";
else _os << "Const";
if (_c.op == LessThan) _os << " < ";
else if (_c.op == GreaterThan) _os << " > ";
else _os << " = ";
if (_c.operand2 == Left) _os << "Left[" << _c.whichAtt2 << "]";
else if (_c.operand2 == Right) _os << "Right[" << _c.whichAtt2 << "]";
else _os << "Const";
if (_c.attType == Integer) _os << " (Integer)";
else if (_c.attType == Float) _os << " (Float)";
else _os << " (String)";
return _os;
}
OrderMaker :: OrderMaker() : numAtts(0) {}
OrderMaker :: OrderMaker(const OrderMaker& _o) : numAtts(_o.numAtts) {
memcpy(whichAtts, _o.whichAtts, _o.numAtts*sizeof(int));
memcpy(whichTypes, _o.whichTypes, _o.numAtts*sizeof(Type));
}
OrderMaker& OrderMaker::operator=(const OrderMaker& _o) {
// handle self-assignment first
if (this == &_o) return *this;
memcpy(whichAtts, _o.whichAtts, _o.numAtts*sizeof(int));
memcpy(whichTypes, _o.whichTypes, _o.numAtts*sizeof(Type));
return *this;
}
void OrderMaker::Swap(OrderMaker& _swap) {
int auxWhichAtts[MAX_ANDS];
Type auxWhichTypes[MAX_ANDS];
memcpy(auxWhichAtts, whichAtts, numAtts*sizeof(int));
memcpy(auxWhichTypes, whichTypes, numAtts*sizeof(Type));
memcpy(whichAtts, _swap.whichAtts, _swap.numAtts*sizeof(int));
memcpy(whichTypes, _swap.whichTypes, _swap.numAtts*sizeof(Type));
memcpy(_swap.whichAtts, auxWhichAtts, numAtts*sizeof(int));
memcpy(_swap.whichTypes, auxWhichTypes, numAtts*sizeof(Type));
SWAP(numAtts, _swap.numAtts);
}
OrderMaker :: OrderMaker(Schema& schema) : numAtts(0) {
unsigned int n = schema.GetNumAtts();
vector<Attribute> atts = schema.GetAtts();
//first add the Integer attributes
for (int i = 0; i < n; i++) {
if (atts[i].type == Integer) {
whichAtts[numAtts] = i;
whichTypes[numAtts] = Integer;
numAtts++;
}
}
// now add in the doubles
for (int i = 0; i < n; i++) {
if (atts[i].type == Float) {
whichAtts[numAtts] = i;
whichTypes[numAtts] = Float;
numAtts++;
}
}
// and finally the strings
for (int i = 0; i < n; i++) {
if (atts[i].type == String) {
whichAtts[numAtts] = i;
whichTypes[numAtts] = String;
numAtts++;
}
}
}
OrderMaker :: OrderMaker(Schema& schema, int* _atts, int _atts_no) {
numAtts = _atts_no;
vector<Attribute> atts = schema.GetAtts();
//only the specified attributes are inserted into the sorting order
for (int i = 0; i < numAtts; i++) {
whichAtts[i] = _atts[i];
whichTypes[i] = atts[_atts[i]].type;
}
}
void OrderMaker::ANDMerge(OrderMaker& om1, OrderMaker& om2) {
*this = om1;
//copy the attributes from om2 that do not appear in om1
for (int i = 0; i < om2.numAtts; i++) {
bool found = false;
for (int j = 0; j < numAtts; j++) {
if (whichAtts[j] == om2.whichAtts[i]) {
//attribute already appears in om1; discard it
found = true;
break;
}
}
//attribute is not appearing in om1
if (found == false) {
whichAtts[numAtts] = om2.whichAtts[i];
whichTypes[numAtts] = om2.whichTypes[i];
numAtts += 1;
}
}
}
int OrderMaker :: Run(Record& left, Record& right) {
char *val1, *val2;
char* left_bits = left.GetBits();
char* right_bits = right.GetBits();
for (int i = 0; i < numAtts; i++) {
val1 = left_bits + ((int *) left_bits)[whichAtts[i] + 1];
val2 = right_bits + ((int *) right_bits)[whichAtts[i] + 1];
// these are used to store the two operands, depending on their type
int val1Int, val2Int;
double val1Double, val2Double;
// now check the type and do the comparison
switch (whichTypes[i]) {
case Integer: {
// cast the two bit strings to integers
val1Int = *((int *) val1);
val2Int = *((int *) val2);
// and do the comparison
if (val1Int < val2Int) return -1;
else if (val1Int > val2Int) return 1;
break;
}
case Float: {
// cast the two bit strings to doubles
val1Double = *((double *) val1);
val2Double = *((double *) val2);
// and do the comparison
if (val1Double < val2Double) return -1;
else if (val1Double > val2Double) return 1;
break;
}
default: {
int sc = strcmp (val1, val2);
if (sc != 0) return sc;
break;
}
}
}
return 0;
}
int OrderMaker :: Run (Record& left, Record& right, OrderMaker& orderRight) {
char *val1, *val2;
char* left_bits = left.GetBits();
char* right_bits = right.GetBits();
for (int i = 0; i < numAtts; i++) {
val1 = left_bits + ((int *) left_bits)[whichAtts[i] + 1];
val2 = right_bits + ((int *) right_bits)[orderRight.whichAtts[i] + 1];
// these are used to store the two operands, depending on their type
int val1Int, val2Int;
double val1Double, val2Double;
// now check the type and do the comparison
switch (whichTypes[i]) {
case Integer: {
// cast the two bit strings to integers
val1Int = *((int *) val1);
val2Int = *((int *) val2);
// and do the comparison
if (val1Int < val2Int) return -1;
else if (val1Int > val2Int) return 1;
break;
}
case Float: {
// cast the two bit strings to doubles
val1Double = *((double *) val1);
val2Double = *((double *) val2);
// and do the comparison
if (val1Double < val2Double) return -1;
else if (val1Double > val2Double) return 1;
break;
}
default: {
int sc = strcmp (val1, val2);
if (sc != 0) return sc;
break;
}
}
}
return 0;
}
ostream& operator<<(ostream& _os, OrderMaker& _o) {
_os << "{";
for (int i = 0; i < _o.numAtts; i++) {
_os << _o.whichAtts[i] << ":";
if (_o.whichTypes[i] == Integer) _os << "Integer";
else if (_o.whichTypes[i] == Float) _os << "Float";
else _os << "String";
if (i < _o.numAtts-1) _os << ", ";
}
_os << "}";
return _os;
}
CNF::CNF() : numAnds(0) {}
CNF::CNF(const CNF& _copy) : numAnds(_copy.numAnds) {
memcpy(andList, _copy.andList, _copy.numAnds*sizeof(Comparison));
}
CNF& CNF::operator=(const CNF& _o) {
// handle self-assignment first
if (this == &_o) return *this;
memcpy(andList, _o.andList, _o.numAnds*sizeof(Comparison));
numAnds = _o.numAnds;
return *this;
}
void CNF::Swap(CNF& _swap) {
for (int i = 0; i < MAX_ANDS; i++)
andList[i].Swap(_swap.andList[i]);
SWAP(numAnds, _swap.numAnds);
}
int CNF :: GetSortOrders (OrderMaker& left, OrderMaker& right) {
// initialize the size of the OrderMakers
left.numAtts = 0;
right.numAtts = 0;
// loop through all of the conditions in the CNF and find those
// that are acceptable for use in a sort ordering:
// - one attribute comes from one side and the other from another side
for (int i = 0; i < numAnds; i++) {
// verify that it operates over attributes from both tables
if (!((andList[i].operand1 == Left && andList[i].operand2 == Right) ||
(andList[i].operand2 == Left && andList[i].operand1 == Right))) {
continue;
}
// since we are here, we have found a join attribute!!!
// so all we need to do is add the new comparison info into the
// relevant structures
if (andList[i].operand1 == Left) {
left.whichAtts[left.numAtts] = andList[i].whichAtt1;
left.whichTypes[left.numAtts] = andList[i].attType;
}
else if (andList[i].operand1 == Right) {
right.whichAtts[right.numAtts] = andList[i].whichAtt1;
right.whichTypes[right.numAtts] = andList[i].attType;
}
if (andList[i].operand2 == Left) {
left.whichAtts[left.numAtts] = andList[i].whichAtt2;
left.whichTypes[left.numAtts] = andList[i].attType;
}
else if (andList[i].operand2 == Right) {
right.whichAtts[right.numAtts] = andList[i].whichAtt2;
right.whichTypes[right.numAtts] = andList[i].attType;
}
// note that we have found two new attributes
left.numAtts++;
right.numAtts++;
}
return left.numAtts;
}
int CNF::ExtractCNF (AndList& parseTree, Schema& schema, Record& literal) {
// build a new CNF with conditions based on the schema
// literal will contain all the constants in the conditions
// previous values stored in CNF are lost
numAnds = 0;
// this tells us the size of the literal record
int numFieldsInLiteral = 0;
int recSize = 0;
int* attStart = new int[MAX_ANDS];
char* recBits = new char[PAGE_SIZE];
char* recPos = recBits;
// now we go through and build the comparison structure
AndList* currCond = &parseTree;
while (currCond != NULL) {
// check if at least one operand in condition is an attribute from schema
if (ConditionOnSchema(*currCond, schema) == false) {
// NO; go further along in the list
currCond = currCond->rightAnd;
continue;
}
// these store the types of the two values that are found
Type typeLeft;
Type typeRight;
if (currCond->left->left->code == NAME) {
// first thing is to deal with the left operand
// so we check to see if it is an attribute name, and if so,
// we look it up in the schema
// see if we can find this attribute in the schema
string s(currCond->left->left->value);
int leftIdx = schema.Index(s);
if (leftIdx != -1) {
andList[numAnds].operand1 = Left;
andList[numAnds].whichAtt1 = leftIdx;
typeLeft = schema.FindType(s);
}
}
else if (currCond->left->left->code == STRING) {
// the next thing is to see if we have a string; if so, add it to the
// literal record that stores all of the comparison values
andList[numAnds].operand1 = Literal;
andList[numAnds].whichAtt1 = numFieldsInLiteral;
typeLeft = String;
// add to record literal
attStart[numFieldsInLiteral] = recSize;
int cLen = strlen(currCond->left->left->value);
memcpy(recPos, currCond->left->left->value, cLen);
if (cLen % sizeof (int) != 0) {
cLen += sizeof (int) - (cLen % sizeof (int));
} else {
cLen += sizeof (int);
}
recSize += cLen;
recPos += cLen;
numFieldsInLiteral += 1;
}
else if (currCond->left->left->code == INTEGER) {
// see if it is an integer
andList[numAnds].operand1 = Literal;
andList[numAnds].whichAtt1 = numFieldsInLiteral;
typeLeft = Integer;
// add to record literal
attStart[numFieldsInLiteral] = recSize;
int cLen = sizeof(int);
*((int *) recPos) = atoi (currCond->left->left->value);
recSize += cLen;
recPos += cLen;
numFieldsInLiteral += 1;
}
else if (currCond->left->left->code == FLOAT) {
// see if it is a double
andList[numAnds].operand1 = Literal;
andList[numAnds].whichAtt1 = numFieldsInLiteral;
typeLeft = Float;
// add to record literal
attStart[numFieldsInLiteral] = recSize;
int cLen = sizeof(double);
*((double *) recPos) = atof (currCond->left->left->value);
recSize += cLen;
recPos += cLen;
numFieldsInLiteral += 1;
}
else {
// catch-all case
cerr << "ERROR: Unknown type for " << currCond->left->left->value << "!" << endl;
return -1;
}
// now that we have dealt with the left operand, we need to deal with the
// right operand
if (currCond->left->right->code == NAME) {
// first thing is to deal with the left operand
// so we check to see if it is an attribute name, and if so,
// we look it up in the schema
// see if we can find this attribute in the schema
string s(currCond->left->right->value);
int leftIdx = schema.Index(s);
if (leftIdx != -1) {
andList[numAnds].operand2 = Left;
andList[numAnds].whichAtt2 = leftIdx;
typeRight = schema.FindType(s);
}
}
else if (currCond->left->right->code == STRING) {
// the next thing is to see if we have a string; if so, add it to the
// literal record that stores all of the comparison values
andList[numAnds].operand2 = Literal;
andList[numAnds].whichAtt2 = numFieldsInLiteral;
typeRight = String;
// add to record literal
attStart[numFieldsInLiteral] = recSize;
int cLen = strlen(currCond->left->right->value);
memcpy(recPos, currCond->left->right->value, cLen);
if (cLen % sizeof (int) != 0) {
cLen += sizeof (int) - (cLen % sizeof (int));
} else {
cLen += sizeof (int);
}
recSize += cLen;
recPos += cLen;
numFieldsInLiteral += 1;
}
else if (currCond->left->right->code == INTEGER) {
// see if it is an integer
andList[numAnds].operand2 = Literal;
andList[numAnds].whichAtt2 = numFieldsInLiteral;
typeRight = Integer;
// add to record literal
attStart[numFieldsInLiteral] = recSize;
int cLen = sizeof(int);
*((int *) recPos) = atoi (currCond->left->right->value);
recSize += cLen;
recPos += cLen;
numFieldsInLiteral += 1;
}
else if (currCond->left->right->code == FLOAT) {
// see if it is a double
andList[numAnds].operand2 = Literal;
andList[numAnds].whichAtt2 = numFieldsInLiteral;
typeRight = Float;
// add to record literal
attStart[numFieldsInLiteral] = recSize;
int cLen = sizeof(double);
*((double *) recPos) = atof (currCond->left->right->value);
recSize += cLen;
recPos += cLen;
numFieldsInLiteral += 1;
}
else {
// catch-all case
cerr << "ERROR: Unknown type for " << currCond->left->right->value << "!" << endl;
return -1;
}
// now we check to make sure that there was not a type mismatch
if (typeLeft != typeRight) {
cerr << "ERROR: Type mismatch for " << currCond->left->left->value
<< " AND " << currCond->left->right->value << "!" << endl;
return -1;
}
// set up the type info for this comparison
andList[numAnds].attType = typeLeft;
// and finally set up the comparison operator for this comparison
if (currCond->left->code == LESS_THAN) andList[numAnds].op = LessThan;
else if (currCond->left->code == GREATER_THAN) andList[numAnds].op = GreaterThan;
else if (currCond->left->code == EQUALS) andList[numAnds].op = Equals;
else {
cerr << "ERROR: Unknown comparison operator for " << currCond->left->left->value
<< " AND " << currCond->left->right->value << "!" << endl;
return -1;
}
numAnds += 1;
currCond = currCond->rightAnd;
}
// create the literal record
char* recComplete = new char[(numFieldsInLiteral+1)*sizeof(int)+recSize];
((int*) recComplete)[0] = (numFieldsInLiteral+1)*sizeof(int)+recSize;
for (int i = 0; i < numFieldsInLiteral; i++)
((int*) recComplete)[i+1] = (numFieldsInLiteral+1)*sizeof(int)+attStart[i];
memcpy(recComplete+(numFieldsInLiteral+1)*sizeof(int), recBits, recSize);
literal.Consume(recComplete);
return 0;
}
int CNF::ExtractCNF (AndList& parseTree, Schema& leftSchema, Schema& rightSchema) {
// build a new CNF with conditions based on the schemas
// one attribute has to come from one schema and the other from another
// previous values stored in CNF are lost
numAnds = 0;
// now we go through and build the comparison structure
AndList* currCond = &parseTree;
while (currCond != NULL) {
// check if at least one operand in condition is an attribute from schema
if (ConditionOnSchemas(*currCond, leftSchema, rightSchema) == false) {
// NO; go further along in the list
currCond = currCond->rightAnd;
continue;
}
// these store the types of the two values that are found
Type typeLeft;
Type typeRight;
string s(currCond->left->left->value);
int leftIdx = leftSchema.Index(s);
if (leftIdx != -1) {
andList[numAnds].operand1 = Left;
andList[numAnds].whichAtt1 = leftIdx;
typeLeft = leftSchema.FindType(s);
}
else {
int rightIdx = rightSchema.Index(s);
andList[numAnds].operand1 = Right;
andList[numAnds].whichAtt1 = rightIdx;
typeRight = rightSchema.FindType(s);
}
s = currCond->left->right->value;
leftIdx = leftSchema.Index(s);
if (leftIdx != -1) {
andList[numAnds].operand2 = Left;
andList[numAnds].whichAtt2 = leftIdx;
typeLeft = leftSchema.FindType(s);
}
else {
int rightIdx = rightSchema.Index(s);
andList[numAnds].operand2 = Right;
andList[numAnds].whichAtt2 = rightIdx;
typeRight = rightSchema.FindType(s);
}
// now we check to make sure that there was not a type mismatch
if (typeLeft != typeRight) {
cerr << "ERROR: Type mismatch for " << currCond->left->left->value
<< " AND " << currCond->left->right->value << "!" << endl;
return -1;
}
// set up the type info for this comparison
andList[numAnds].attType = typeLeft;
// and finally set up the comparison operator for this comparison
if (currCond->left->code == LESS_THAN) andList[numAnds].op = LessThan;
else if (currCond->left->code == GREATER_THAN) andList[numAnds].op = GreaterThan;
else if (currCond->left->code == EQUALS) andList[numAnds].op = Equals;
else {
cerr << "ERROR: Unknown comparison operator for " << currCond->left->left->value
<< " AND " << currCond->left->right->value << "!" << endl;
return -1;
}
numAnds += 1;
currCond = currCond->rightAnd;
}
return 0;
}
bool CNF :: Run (Record& left, Record& right) {
for (int i = 0; i < numAnds; i++) {
bool result = andList[i].Run(left, right);
if (result == false) return false;
}
return true;
}
ostream& operator<<(ostream& _os, CNF& _o) {
for (int i = 0; i < _o.numAnds; i++) {
if (i == 0) _os << "("; else _os << " (";
_os << _o.andList[i];
if (i < _o.numAnds-1) _os << ") AND"; else _os << ")";
}
return _os;
}
bool ConditionOnSchema(AndList& _cond, Schema& _schema) {
bool isGood = false;
if (_cond.left->left->code == NAME) {
string s(_cond.left->left->value);
int leftIdx = _schema.Index(s);
if (leftIdx != -1) isGood = true;
else return false;
}
if (_cond.left->right->code == NAME) {
string s(_cond.left->right->value);
int rightIdx = _schema.Index(s);
if (rightIdx != -1) isGood = true;
else return false;
}
return isGood;
}
bool ConditionOnSchemas(AndList& _cond, Schema& _schemaL, Schema& _schemaR) {
int sL = 0, sR = 0;
if (_cond.left->left->code == NAME) {
string s(_cond.left->left->value);
int idx = _schemaL.Index(s);
if (idx != -1) sL += 1;
idx = _schemaR.Index(s);
if (idx != -1) sR += 1;
}
if (sL+sR != 1) return false;
if (_cond.left->right->code == NAME) {
string s(_cond.left->right->value);
int idx = _schemaL.Index(s);
if (idx != -1) sL += 1;
idx = _schemaR.Index(s);
if (idx != -1) sR += 1;
}
if ((sL == 1) && (sR == 1)) return true;
else return false;
}