forked from yongwen/columbia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.cpp
2407 lines (2075 loc) · 65.9 KB
/
rules.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
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
/* rules.cpp : rules implementation
$Revision: 16 $
Implements classes in rules.h
Columbia Optimizer Framework
A Joint Research Project of Portland State University
and the Oregon Graduate Institute
Directed by Leonard Shapiro and David Maier
Supported by NSF Grants IRI-9610013 and IRI-9619977
*/
#include "stdafx.h"
#include "tasks.h"
#include "physop.h"
#include "cat.h"
#define NUMOFRULES 20 // Number of elements in the enum RULELABELS in rules.h
#define LINEWIDTH 256 // buffer length of one text line
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// use to turn some rules on/off in the optimizer
int RuleVector[NUMOFRULES];
//##ModelId=3B0C0868032A
RULE_SET::RULE_SET(CString filename) : RuleCount(NUMOFRULES)
{
//read the RuleVector value from filename
FILE *fp; // file handle
char TextLine[LINEWIDTH]; // text line buffer
char *p;
int rule_count=0;
if((fp = fopen(filename,"r"))==NULL)
OUTPUT_ERROR("can not open file 'ruleset'");
for(;;)
{
fgets(TextLine,LINEWIDTH,fp);
if (feof(fp)) break;
// skip the comment line
if(IsCommentOrBlankLine(TextLine)) continue;
p = SkipSpace(TextLine);
//if the vectors are filled before end of file, raise an error
if (rule_count >= NUMOFRULES)
{
OUTPUT_ERROR("Rule Set File error!");
}
parseString(p);
RuleVector[rule_count] = atoi(p);
rule_count++;
}
// if the file ends before the vectors are filled, set the rest to off
if (rule_count < NUMOFRULES)
{
for (; rule_count<NUMOFRULES; rule_count++)
RuleVector[rule_count] = 0;
}
fclose(fp);
rule_set = new RULE* [RuleCount];
// file-scan implements get
rule_set[R_GET_TO_FILE_SCAN] = new GET_TO_FILE_SCAN();
// filter implements select
rule_set[R_SELECT_TO_FILTER] = new SELECT_TO_FILTER();
// physical project implements project
rule_set[R_P_TO_PP] = new P_TO_PP();
// index loops join implements eqjoin
rule_set[R_EQ_TO_LOOPS_INDEX] = new EQ_TO_LOOPS_INDEX();
// sort merge implements eqjoin
rule_set[R_EQ_TO_MERGE] = new EQ_TO_MERGE();
// LOOPS JOIN implements EQJOIN
rule_set[R_EQ_TO_LOOPS] = new EQ_TO_LOOPS();
// sort enforcer rule
rule_set[R_SORT_RULE] = new SORT_RULE();
// Commute eqjoin
rule_set[R_EQJOIN_COMMUTE] = new EQJOIN_COMMUTE();
// Associativity of EQJOIN
rule_set[R_EQJOIN_LTOR] = new EQJOIN_LTOR();
// Associativity of EQJOIN
rule_set[R_EQJOIN_RTOL] = new EQJOIN_RTOL();
// Cesar's exchange rule
rule_set[R_EXCHANGE] = new EXCHANGE();
// hash_duplicates implements rm_duplicates
rule_set[R_RM_TO_HASH_DUPLICATES] = new RM_TO_HASH_DUPLICATES();
// hgroup_list implements agg_list
// agg_ops1 and agg_ops2 just hold a place
AGG_OP_ARRAY *agg_ops1 = new AGG_OP_ARRAY;
AGG_OP_ARRAY *agg_ops2 = new AGG_OP_ARRAY;
rule_set[R_AL_TO_HGL] = new AL_TO_HGL(agg_ops1, agg_ops2);
// p_func_op implements func_op
rule_set[R_FO_TO_PFO] = new FO_TO_PFO();
// agg_thru_eq push agg_list below eqjoin
// agg_ops3 and agg_ops4 just hold a place
AGG_OP_ARRAY *agg_ops3 = new AGG_OP_ARRAY;
AGG_OP_ARRAY *agg_ops4 = new AGG_OP_ARRAY;
rule_set[R_AGG_THRU_EQJOIN] = new AGG_THRU_EQJOIN(agg_ops3, agg_ops4);
// eq_to_bit EQJOIN to BIT_SEMIJOIN
rule_set[R_EQ_TO_BIT] = new EQ_TO_BIT();
// selectet_to_indexed filter
rule_set[R_SELECT_TO_INDEXED_FILTER] = new SELECT_TO_INDEXED_FILTER();
// project_through_select
rule_set[R_PROJECT_THRU_SELECT] = new PROJECT_THRU_SELECT();
// EQJOIN to HASH JOIN
rule_set[R_EQ_TO_HASH] = new EQ_TO_HASH();
// DUMMY to PDUMMY
rule_set[R_DUMMY_TO_PDUMMY] = new DUMMY_TO_PDUMMY();
}; // rule set
//##ModelId=3B0C0868033E
RULE_SET::~RULE_SET()
{
for(int i=0; i<RuleCount; i++) delete rule_set[i];
delete rule_set;
}
//##ModelId=3B0C08680348
CString RULE_SET::Dump()
{
CString os;
CString temp;
for(int i=0; i<RuleCount; i++)
{
temp.Format(" %d %s\r\n", RuleVector[i],rule_set[i]->GetName() );
os += temp;
}
return os;
}
#ifdef _DEBUG
#ifndef _TABLE_
//##ModelId=3B0C08680352
CString RULE_SET::DumpStats()
{
CString os;
CString temp;
temp.Format("%s", "Rule#\tTopMatch\t Bindings\tConditions\r\n");
os += temp;
for(int i=0; i<RuleCount; i++)
{
temp.Format("%d\t%d\t%d\t%d\t%s\r\n",
i, TopMatch[i], Bindings[i], Conditions[i], rule_set[i]->GetName() );
os += temp;
}
return os;
}
#endif
#endif
// ====================
//##ModelId=3B0EA6DA0280
BINDERY::BINDERY (GRP_ID group_no, EXPR * original)
:state(start), group_no(group_no), cur_expr(NULL),
original(original), input(NULL),
one_expr(FALSE) // try all expressions within this group
{
ASSERT (original);
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_BINDERY].New();
} // BINDERY::BINDERY
//##ModelId=3B0EA6DA0262
BINDERY::BINDERY (M_EXPR * expr, EXPR * original)
:state(start), cur_expr(expr), original(original), input(NULL),
one_expr(TRUE) // restricted to this log expr
{
group_no = expr -> GetGrpID();
ASSERT (original);
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_BINDERY].New();
} // BINDERY::BINDERY
//##ModelId=3B0EA6DA0294
BINDERY::~BINDERY ()
{
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_BINDERY].Delete();
if(input!=NULL)
{
for(int i=0; i<original->GetOp()->GetArity(); i++) delete input[i];
delete [] input;
}
}; // BINDERY::~BINDERY
//##ModelId=3B0EA6DA02A8
EXPR * BINDERY::extract_expr ()
{
EXPR * result;
// If original pattern is NULL something weird is happening.
assert(original);
OP * patt_op = original -> GetOp();
// Ensure that there has been a binding, so there is an
// expression to extract.
assert(state == valid_binding || state == finished ||
(patt_op -> is_leaf () && state == start) );
// create leaf marked with group index
if (patt_op -> is_leaf ())
{
result = new EXPR (
new LEAF_OP(((LEAF_OP *)patt_op)->GetIndex(), group_no));
} // create leaf marked with group index
else // general invocation of new EXPR
{
//Top operator in the new EXPR will be top operator in cur_expr.
//Get it. (Probably could use patt_op here.)
OP * op_arg = cur_expr->GetOp()->Clone();
//Need the arity of the top operator to construct inputs of new EXPR
int arity = op_arg -> GetArity();
// Inputs of new EXPR can be extracted from binderys stored in
// BINDERY::input. Put these in the array subexpr.
if(arity)
{
EXPR ** subexpr = new EXPR* [arity];
for (int input_no = 0; input_no < arity; input_no++ )
subexpr[input_no] = input[input_no]->extract_expr();
// Put everything together for the result.
result = new EXPR (op_arg, subexpr);
}
else
result = new EXPR (op_arg);
} // general invocation of new EXPR
return result;
} // BINDERY::extract_expr
/*
Function BINDERY::advance() walks the many trees embedded in the
MEMO structure in order to find possible bindings. It is called
only by APPLY_RULE::perform. The walking is done with a finite
state machine, as follows.
State start:
If the original pattern is a leaf, we are done.
State = finished
Return TRUE
Skip over non-logical, non-matching expressions.
State = finished
break
Create a group bindery for each input and
try to create a binding for each input.
If successful
State = valid_binding
Return TRUE
else
delete input binderys
State = finished
break
State valid_binding:
Increment input bindings in right-to-left order.
If we found a next binding,
State = valid_binding
return TRUE
else
delete input binderys
state = finished
break
State finished
If original pattern is a leaf //second time through, so we are done
OR
this is an expr bindery //we finished the first expression, so done
OR
there is no next expression
return FALSE
else
state = start
break
*/
//##ModelId=3B0EA6DA029E
bool BINDERY::advance ()
{
#ifdef _REUSE_SIB
///XXXX Leave these comments alone - fix later
OP * patt_op = original -> GetOp();
// If the original pattern is a leaf, we will get one binding,
// to the entire group, then we will be done
if (patt_op -> is_leaf ())
{
switch(state)
{
case start:
state = finished; //failure next time, but
return true; // success now
case finished:
return false;
default:
assert(false);
}
}// if (patt_op -> is_leaf ())
if( !one_expr && state == start ) // begin the group binding
{ //Search entire group for bindings
cur_expr = Ssp->GetGroup(group_no)->GetFirstLogMExpr(); // get the first mexpr
}
// loop until either failure or success
for (;;)
{
// cache some function results
OP * op_arg = cur_expr -> GetOp();
int arity = op_arg -> GetArity();
int input_no;
assert( op_arg->is_logical() );
// state analysis and transitions
switch (state)
{
case start:
// is this expression unusable?
if ( arity != patt_op->GetArity() ||
!( patt_op->GetNameId() == op_arg->GetNameId())
)
{
state = finished; // try next expression
break;
}
if(arity==0) // only the Operator, matched
{
state = valid_binding;
return true;
} // successful bindings for the Operator without inputs
else
{
// Create a group bindery for each input
input = new BINDERY* [arity];
for (input_no = 0; input_no < arity; input_no ++ )
{
input[input_no] = new BINDERY
( cur_expr -> GetInput(input_no),
original -> GetInput(input_no)) ;
}
// Try to advance each (new) input bindery to a binding
// a failure is failure for the expr
for (input_no = 0; input_no < arity; input_no ++ )
if ( ! input[input_no]->advance() )
break; // terminate this loop
// check whether all inputs found a binding
if (input_no == arity ) // successful!
{
state = valid_binding;
return true;
} // successful bindings for new expression
else
{ // otherwise, failure! -- dealloc inputs
test_delete (arity);
state = finished;
break;
}
}// if(arity)
case valid_binding:
for (input_no= arity; --input_no >= 0;)
{
if (currentBind == NULL)
// try existing inputs in right-to-left order
// first success is overall success
{
if ( input[input_no]->advance() )
{
for (int other_input_no=input_no; ++ other_input_no < arity;)
{
// input[other_input_no] = get_first_bindery_in_list;
// currentBind->bindery = input[other_input_no];
input[other_input_no] = list->bindery;
currentBind = list;
}
state = valid_binding;
}
}
else
{
currentBind = currentBind->next;
if ( currentBind != NULL )
{
state = valid_binding;
input[input_no] = currentBind->bindery;
// input[input_no] = get_next_bindery_in_list;
return true;
}
else
return false;
}
}
if ( arity != 0 && !one_expr )
{
Node *newNode = new Node();
BINDERY *dup = new BINDERY(this);
newNode->bindery = dup;
if (list == NULL)
list = last = newNode;
else
{
last->next = newNode;
last = last->next;
}
// add_to_the_list (this bindery);
}
state = finished;
break;
case finished :
if( one_expr ||
((cur_expr = cur_expr->GetNextMExpr()) == NULL )
)
return false;
else
{
state = start;
break;
}
default :
assert (false);
} // state analysis and transitions
} // loop until either failure or success
assert(false); // should never terminate this loop
#else
OP * patt_op = original -> GetOp();
// If the original pattern is a leaf, we will get one binding,
// to the entire group, then we will be done
if (patt_op -> is_leaf ())
{
switch(state)
{
case start:
state = finished; //failure next time, but
return true; // success now
case finished:
return false;
default:
assert(false);
}
}// if (patt_op -> is_leaf ())
if( !one_expr && state == start ) // begin the group binding
{ //Search entire group for bindings
cur_expr = Ssp->GetGroup(group_no)->GetFirstLogMExpr(); // get the first mexpr
}
// loop until either failure or success
for (;;)
{
//PTRACE ("advancing the cur_expr: %s", cur_expr->Dump() );
// cache some function results
OP * op_arg = cur_expr -> GetOp();
int arity = op_arg -> GetArity();
int input_no;
assert( op_arg->is_logical() );
// state analysis and transitions
switch (state)
{
case start:
// is this expression unusable?
if ( arity != patt_op->GetArity() ||
!( patt_op->GetNameId() == op_arg->GetNameId())
)
{
state = finished; // try next expression
break;
}
if(arity==0) // only the Operator, matched
{
state = valid_binding;
return true;
} // successful bindings for the Operator without inputs
else
{
// Create a group bindery for each input
input = new BINDERY* [arity];
for (input_no = 0; input_no < arity; input_no ++ )
input[input_no] = new BINDERY
( cur_expr -> GetInput(input_no),
original -> GetInput(input_no)) ;
// Try to advance each (new) input bindery to a binding
// a failure is failure for the expr
for (input_no = 0; input_no < arity; input_no ++ )
if ( ! input[input_no]->advance() )
break; // terminate this loop
// check whether all inputs found a binding
if (input_no == arity ) // successful!
{
state = valid_binding;
return true;
} // successful bindings for new expression
else
{ // otherwise, failure! -- dealloc inputs
for (input_no = arity; -- input_no >= 0; )
delete input[input_no];
delete [] input; input = NULL;
state = finished;
break;
}
}// if(arity)
case valid_binding :
// try existing inputs in right-to-left order
// first success is overall success
for (input_no = arity; -- input_no >= 0; )
{
if ( input[input_no]->advance() )
// found one more binding
{
// If we have a new binding in a non-rightmost location,
// we must create new binderys for all inputs to the
// right of input_no, else we will not get all bindings.
// This is inefficient code since the each input on the
// right has multiple binderys created for it, and each
// bindery produces the same bindings as the others.
// The simplest example of this is the exchange rule.
for (int other_input_no = input_no;
++ other_input_no < arity; )
{
delete input[other_input_no];
input[other_input_no] = (new BINDERY (
cur_expr->GetInput(other_input_no),
original ->GetInput(other_input_no)) );
if (! input[other_input_no]->advance() )
// Impossible since we found these bindings earlier
ASSERT(false) ;
}
// return overall success
state = valid_binding;
return true;
} // found one more binding
} // try existing inputs in right-to-left order
// There are no more bindings to this log expr;
// dealloc input binderys.
if(arity)
{
for (input_no = arity; -- input_no >= 0; )
delete input[input_no];
delete [] input; input = NULL;
}
state = finished;
break;
case finished :
if( one_expr ||
((cur_expr = cur_expr->GetNextMExpr()) == NULL )
)
return false;
else
{
state = start;
break;
}
default :
assert (false);
} // state analysis and transitions
} // loop until either failure or success
assert(false); // should never terminate this loop
#endif
} // BINDERY::advance
#ifdef _REUSE_SIB
//##ModelId=3B0EA6DA024E
void BINDERY::test_delete (int arity) {
int input_no;
for (input_no = arity; -- input_no >= 0; )
// for (; -- input_no >= 0; )
delete input[input_no];
delete [] input; input = NULL;
}
#endif
/*
Rule Get -> File-scan
==== === == ====-====
*/
//##ModelId=3B0C086A00CB
GET_TO_FILE_SCAN::GET_TO_FILE_SCAN ()
: RULE ("GET_TO_FILE_SCAN", 0,
new EXPR (new GET (0)),
new EXPR (new FILE_SCAN (0) ) )
{
// set rule index
set_index(R_GET_TO_FILE_SCAN);
}; // GET_TO_FILE_SCAN::GET_TO_FILE_SCAN
//##ModelId=3B0C086A00DE
EXPR * GET_TO_FILE_SCAN::next_substitute ( EXPR * before, PHYS_PROP * ReqdProp)
{
EXPR * result;
// create transformed expression
result = new EXPR (new FILE_SCAN ( ((GET *)before->GetOp())->GetCollection() ) );
return result;
}; // GET_TO_FILE_SCAN::next_substitute
/*
Rule EQJOIN -> LOOPS JOIN
==== ====== == ==== ====
*/
//##ModelId=3B0C086A0161
EQ_TO_LOOPS::EQ_TO_LOOPS ()
: RULE ("EQJOIN->LOOPS_JOIN", 2,
new EXPR (new EQJOIN (0,0,0),
new EXPR (new LEAF_OP (0)),
new EXPR (new LEAF_OP (1))
),
new EXPR (new LOOPS_JOIN (0,0,0),
new EXPR (new LEAF_OP (0)),
new EXPR (new LEAF_OP (1))
)
)
{
// set rule index
set_index(R_EQ_TO_LOOPS);
} // EQ_TO_LOOPS::EQ_TO_LOOPS
//##ModelId=3B0C086A0174
EXPR * EQ_TO_LOOPS::next_substitute (EXPR * before,PHYS_PROP * ReqdProp)
{
EQJOIN * Op = (EQJOIN *) before->GetOp();
int size = Op->size;
int * lattrs = CopyArray ( Op->lattrs, size);
int * rattrs = CopyArray ( Op->rattrs, size);
// create transformed expression
EXPR * result = new EXPR (new LOOPS_JOIN (lattrs, rattrs, size),
new EXPR(*(before->GetInput(0))),
new EXPR(*(before->GetInput(1)))
);
return result ;
} // EQ_TO_LOOPS::next_substitute
#ifdef CONDPRUNE
//Is the plan a goner because an input is group pruned?
//##ModelId=3B0C086A017E
bool EQ_TO_LOOPS::condition ( EXPR * before, M_EXPR *mexpr, int ContextID)
{
COST inputs = *( Ssp->GetGroup(mexpr->GetInput(0))->GetLowerBd() );
inputs += *( Ssp->GetGroup(mexpr->GetInput(1))->GetLowerBd() );
if ( inputs >= *( CONT::vc[ContextID]->GetUpperBd() ) )
return(false);
return(true);
} //EQ_TO_LOOPS::condition
#endif
/*
Rule EQJOIN -> LOOPS INDEX JOIN
==== ====== == ==== ===== ====
*/
//##ModelId=3B0C086A0342
EQ_TO_LOOPS_INDEX::EQ_TO_LOOPS_INDEX ()
: RULE ("EQJOIN -> LOOPS_INDEX_JOIN", 1,
new EXPR (new EQJOIN (0, 0, 0),
new EXPR (new LEAF_OP (0)),
new EXPR (new GET(0))
),
new EXPR (new LOOPS_INDEX_JOIN (0, 0, 0, 0),
new EXPR (new LEAF_OP (0))
)
)
{
// set rule index
set_index(R_EQ_TO_LOOPS_INDEX);
} // EQ_TO_LOOPS_INDEX::EQ_TO_LOOPS_INDEX
//##ModelId=3B0C086A034B
EXPR * EQ_TO_LOOPS_INDEX::next_substitute (EXPR * before,PHYS_PROP * ReqdProp)
{
EXPR * result;
EQJOIN * Op = (EQJOIN *) before->GetOp();
int size = Op->size;
int * lattrs = CopyArray ( Op->lattrs, size);
int * rattrs = CopyArray ( Op->rattrs, size);
// Get the GET logical operator in order to get the indexed collection
GET *g = (GET *)before->GetInput(1)->GetOp();
// create transformed expression
result = new EXPR (new LOOPS_INDEX_JOIN (lattrs, rattrs, size, g->GetCollection()),
new EXPR (*(before->GetInput(0)))
);
return ( result );
} // EQ_TO_LOOPS_INDEX::next_substitute
// Need to check:
// that the join is on a attribute from each table (not a multi-attribute
// join)
// there is an index for the right join attribute -- need log-bulk-props
//##ModelId=3B0C086A0355
bool EQ_TO_LOOPS_INDEX::condition ( EXPR * before, M_EXPR *mexpr, int ContextID)
{
// Get the GET logical operator in order to get the indexed collection
GET *g = (GET *)before->GetInput(1)->GetOp();
INT_ARRAY * Indices = Cat->GetIndNames(g->GetCollection());
if(Indices == NULL ) return false;
int * rattrs = ((EQJOIN *) before -> GetOp()) -> rattrs ;
int size = ((EQJOIN *) before -> GetOp()) -> size ;
// Loop thru indices
for (int i=0; i < Indices->GetSize(); i++)
if ( size == 1 && Cat->GetIndProp((*Indices)[i])->Keys->ContainKey(rattrs[0]))
return ( true );
return ( false );
} // EQ_TO_LOOPS_INDEX::condition
/*
Rule EQJOIN -> MERGE JOIN
==== ====== == ===== ====
*/
//##ModelId=3B0C086A0214
EQ_TO_MERGE::EQ_TO_MERGE ()
: RULE ("EQJOIN -> MERGE_JOIN", 2,
new EXPR (new EQJOIN (0,0,0),
new EXPR (new LEAF_OP (0)),
new EXPR (new LEAF_OP (1))
),
new EXPR (new MERGE_JOIN (0,0,0),
new EXPR (new LEAF_OP (0)),
new EXPR (new LEAF_OP (1))
)
)
{
// set rule index
set_index(R_EQ_TO_MERGE);
} // EQ_TO_MERGE::EQ_TO_MERGE
//##ModelId=3B0C086A0215
int EQ_TO_MERGE::promise (OP* op_arg, int ContextID)
{
// if the merge-join attributes set is empty, don't fire this rule
int result = ( ((EQJOIN*)op_arg)->size == 0 ) ? 0 : MERGE_PROMISE ;
return ( result );
} // EQ_TO_MERGE::promise
//##ModelId=3B0C086A0228
EXPR * EQ_TO_MERGE::next_substitute (EXPR * before,PHYS_PROP * ReqdProp)
{
EXPR * result;
EQJOIN * Op = (EQJOIN *) before->GetOp();
int size = Op->size;
int * lattrs = CopyArray ( Op->lattrs, size);
int * rattrs = CopyArray ( Op->rattrs, size);
// create transformed expression
result = new EXPR (new MERGE_JOIN (lattrs, rattrs,size),
new EXPR(*(before->GetInput(0))),
new EXPR(*(before->GetInput(1)))
);
return ( result );
} // EQ_TO_MERGE::next_substitute
#ifdef CONDPRUNE
//Is the plan a goner because an input is group pruned?
//##ModelId=3B0C086A0232
bool EQ_TO_MERGE::condition ( EXPR * before, M_EXPR *mexpr, int ContextID)
{
COST inputs = *( Ssp->GetGroup(mexpr->GetInput(0))->GetLowerBd() );
inputs += *( Ssp->GetGroup(mexpr->GetInput(1))->GetLowerBd() );
if ( inputs >= *( CONT::vc[ContextID]->GetUpperBd() ) )
return(false);
return(true);
} //EQ_TO_MERGE::condition
#endif
/*
Rule EQJOIN -> HASH JOIN
==== ====== == ===== ====
*/
//##ModelId=3B0C086A02B4
EQ_TO_HASH::EQ_TO_HASH ()
: RULE ("EQJOIN->HASH_JOIN", 2,
new EXPR (new EQJOIN (0,0,0),
new EXPR (new LEAF_OP (0)),
new EXPR (new LEAF_OP (1))
),
new EXPR (new HASH_JOIN (0,0,0),
new EXPR (new LEAF_OP (0)),
new EXPR (new LEAF_OP (1))
)
)
{
// set rule index
set_index(R_EQ_TO_HASH);
} // EQ_TO_HASH::EQ_TO_HASH
//##ModelId=3B0C086A02B5
int EQ_TO_HASH::promise (OP* op_arg, int ContextID)
{
// if the hash-join attributes set is empty, don't fire this rule
int result = ( ((EQJOIN*)op_arg)->size == 0 ) ? 0 : HASH_PROMISE ;
return ( result );
} // EQ_TO_HASH::promise
//##ModelId=3B0C086A02C8
EXPR * EQ_TO_HASH::next_substitute (EXPR * before,PHYS_PROP * ReqdProp)
{
EXPR * result;
EQJOIN * Op = (EQJOIN *) before->GetOp();
int size = Op->size;
int * lattrs = CopyArray ( Op->lattrs, size);
int * rattrs = CopyArray ( Op->rattrs, size);
// create transformed expression
result = new EXPR (new HASH_JOIN (lattrs, rattrs,size),
new EXPR(*(before->GetInput(0))),
new EXPR(*(before->GetInput(1)))
);
return ( result );
} // EQ_TO_HASH::next_substitute
/*
Rule EQJOIN(A,B) -> EQJOIN(B,A)
==== =========== == ===========
*/
//##ModelId=3B0C086A03CE
EQJOIN_COMMUTE::EQJOIN_COMMUTE ()
: RULE ("EQJOIN_COMMUTE", 2,
new EXPR (new EQJOIN (0,0,0),
new EXPR (new LEAF_OP (0)),
new EXPR (new LEAF_OP (1))
),
new EXPR (new EQJOIN (0,0,0),
new EXPR (new LEAF_OP (1)),
new EXPR (new LEAF_OP (0))
)
)
{
// set rule mask and index
set_index(R_EQJOIN_COMMUTE);
set_mask( 1 << R_EQJOIN_COMMUTE |
1 << R_EQJOIN_LTOR |
1 << R_EQJOIN_RTOL |
1 << R_EXCHANGE
);
} // EQJOIN_COMMUTE::EQJOIN_COMMUTE
//##ModelId=3B0C086A03D8
EXPR * EQJOIN_COMMUTE::next_substitute (EXPR * before,PHYS_PROP * ReqdProp)
{
// lattrs and rattrs
EQJOIN * Op = (EQJOIN *) before->GetOp();
int size = Op->size;
int * lattrs = CopyArray ( Op->lattrs, size);
int * rattrs = CopyArray ( Op->rattrs, size);
// create transformed expression
EXPR *result = new EXPR (new EQJOIN ( rattrs, lattrs, size), // reverse l and r
new EXPR(*(before->GetInput(1))),
new EXPR(*(before->GetInput(0)))
);
return result ;
} // EQJOIN_COMMUTE::next_substitute
/*
Rule EQJOIN (AB) C -> EQJOIN A (BC)
==== ============= == =============
*/
// assoc of join left to right
//##ModelId=3B0C086B00A3
EQJOIN_LTOR::EQJOIN_LTOR ()
: RULE ("EQJOIN_LTOR", 3,
new EXPR (new EQJOIN (0,0,0),
new EXPR (new EQJOIN (0,0,0),
new EXPR (new LEAF_OP (0)), // A
new EXPR (new LEAF_OP (1)) // B
),
new EXPR (new LEAF_OP (2)) // C
), // original pattern
new EXPR (new EQJOIN (0,0,0),
new EXPR (new LEAF_OP (0)),
new EXPR (new EQJOIN (0,0,0),
new EXPR (new LEAF_OP (1)),
new EXPR (new LEAF_OP (2))
)
) // substitute
)
{
// set rule mask and index
set_index ( R_EQJOIN_LTOR );
set_mask ( 1 << R_EQJOIN_LTOR |
1 << R_EQJOIN_RTOL |
1 << R_EXCHANGE
);
} // EQJOIN_LTOR::EQJOIN_LTOR
//##ModelId=3B0C086B00AD
EXPR * EQJOIN_LTOR::next_substitute (EXPR * before,PHYS_PROP * ReqdProp)
{
/*
* Join numbering convention:
* 1 2 2 1 join number
* EQJOIN (AxB)xC -> EQJOIN Ax(BxC) original -> substitute patterns
*/
// from upper (second) join
EQJOIN * Op2 = (EQJOIN *) before->GetOp();