-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
HepPlanner.java
1070 lines (952 loc) · 34.2 KB
/
HepPlanner.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.plan.hep;
import org.apache.calcite.linq4j.function.Function2;
import org.apache.calcite.linq4j.function.Functions;
import org.apache.calcite.plan.AbstractRelOptPlanner;
import org.apache.calcite.plan.CommonRelSubExprRule;
import org.apache.calcite.plan.Context;
import org.apache.calcite.plan.RelDigest;
import org.apache.calcite.plan.RelOptCost;
import org.apache.calcite.plan.RelOptCostFactory;
import org.apache.calcite.plan.RelOptCostImpl;
import org.apache.calcite.plan.RelOptMaterialization;
import org.apache.calcite.plan.RelOptPlanner;
import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.plan.RelOptRuleOperand;
import org.apache.calcite.plan.RelTrait;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.convert.Converter;
import org.apache.calcite.rel.convert.ConverterRule;
import org.apache.calcite.rel.convert.TraitMatchingRule;
import org.apache.calcite.rel.core.RelFactories;
import org.apache.calcite.rel.metadata.RelMdUtil;
import org.apache.calcite.rel.metadata.RelMetadataProvider;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;
import org.apache.calcite.util.graph.BreadthFirstIterator;
import org.apache.calcite.util.graph.CycleDetector;
import org.apache.calcite.util.graph.DefaultDirectedGraph;
import org.apache.calcite.util.graph.DefaultEdge;
import org.apache.calcite.util.graph.DepthFirstIterator;
import org.apache.calcite.util.graph.DirectedGraph;
import org.apache.calcite.util.graph.Graphs;
import org.apache.calcite.util.graph.TopologicalOrderIterator;
import com.google.common.collect.ImmutableList;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.calcite.linq4j.Nullness.castNonNull;
import static java.util.Objects.requireNonNull;
/**
* HepPlanner is a heuristic implementation of the {@link RelOptPlanner}
* interface.
*/
public class HepPlanner extends AbstractRelOptPlanner {
//~ Instance fields --------------------------------------------------------
private final HepProgram mainProgram;
private @Nullable HepRelVertex root;
private @Nullable RelTraitSet requestedRootTraits;
/**
* {@link RelDataType} is represented with its field types as {@code List<RelDataType>}.
* This enables to treat as equal projects that differ in expression names only.
*/
private final Map<RelDigest, HepRelVertex> mapDigestToVertex =
new HashMap<>();
private int nTransformations;
private int graphSizeLastGC;
private int nTransformationsLastGC;
private final boolean noDag;
/**
* Query graph, with edges directed from parent to child. This is a
* single-rooted DAG, possibly with additional roots corresponding to
* discarded plan fragments which remain to be garbage-collected.
*/
private final DirectedGraph<HepRelVertex, DefaultEdge> graph =
DefaultDirectedGraph.create();
private final Function2<RelNode, RelNode, Void> onCopyHook;
private final List<RelOptMaterialization> materializations =
new ArrayList<>();
//~ Constructors -----------------------------------------------------------
/**
* Creates a new HepPlanner that allows DAG.
*
* @param program program controlling rule application
*/
public HepPlanner(HepProgram program) {
this(program, null, false, null, RelOptCostImpl.FACTORY);
}
/**
* Creates a new HepPlanner that allows DAG.
*
* @param program program controlling rule application
* @param context to carry while planning
*/
public HepPlanner(HepProgram program, @Nullable Context context) {
this(program, context, false, null, RelOptCostImpl.FACTORY);
}
/**
* Creates a new HepPlanner with the option to keep the graph a
* tree (noDag = true) or allow DAG (noDag = false).
*
* @param noDag If false, create shared nodes if expressions are
* identical
* @param program Program controlling rule application
* @param onCopyHook Function to call when a node is copied
*/
public HepPlanner(
HepProgram program,
@Nullable Context context,
boolean noDag,
@Nullable Function2<RelNode, RelNode, Void> onCopyHook,
RelOptCostFactory costFactory) {
super(costFactory, context);
this.mainProgram = requireNonNull(program, "program");
this.onCopyHook = Util.first(onCopyHook, Functions.ignore2());
this.noDag = noDag;
}
//~ Methods ----------------------------------------------------------------
@Override public void setRoot(RelNode rel) {
root = addRelToGraph(rel);
dumpGraph();
}
@Override public @Nullable RelNode getRoot() {
return root;
}
@Override public void clear() {
super.clear();
for (RelOptRule rule : getRules()) {
removeRule(rule);
}
this.materializations.clear();
}
@Override public RelNode changeTraits(RelNode rel, RelTraitSet toTraits) {
// Ignore traits, except for the root, where we remember
// what the final conversion should be.
if ((rel == root) || (rel == requireNonNull(root, "root").getCurrentRel())) {
requestedRootTraits = toTraits;
}
return rel;
}
@Override public RelNode findBestExp() {
requireNonNull(root, "root");
executeProgram(mainProgram);
// Get rid of everything except what's in the final plan.
collectGarbage();
dumpRuleAttemptsInfo();
return buildFinalPlan(requireNonNull(root, "root"));
}
/** Top-level entry point for a program. Initializes state and then invokes
* the program. */
private void executeProgram(HepProgram program) {
final HepInstruction.PrepareContext px =
HepInstruction.PrepareContext.create(this);
final HepState state = program.prepare(px);
state.execute();
}
void executeProgram(HepProgram instruction, HepProgram.State state) {
state.init();
state.instructionStates.forEach(instructionState -> {
instructionState.execute();
int delta = nTransformations - nTransformationsLastGC;
if (delta > graphSizeLastGC) {
// The number of transformations performed since the last
// garbage collection is greater than the number of vertices in
// the graph at that time. That means there should be a
// reasonable amount of garbage to collect now. We do it this
// way to amortize garbage collection cost over multiple
// instructions, while keeping the highwater memory usage
// proportional to the graph size.
collectGarbage();
}
});
}
void executeMatchLimit(HepInstruction.MatchLimit instruction,
HepInstruction.MatchLimit.State state) {
LOGGER.trace("Setting match limit to {}", instruction.limit);
state.programState.matchLimit = instruction.limit;
}
void executeMatchOrder(HepInstruction.MatchOrder instruction,
HepInstruction.MatchOrder.State state) {
LOGGER.trace("Setting match order to {}", instruction.order);
state.programState.matchOrder = instruction.order;
}
void executeRuleInstance(HepInstruction.RuleInstance instruction,
HepInstruction.RuleInstance.State state) {
if (state.programState.skippingGroup()) {
return;
}
applyRules(state.programState, ImmutableList.of(instruction.rule), true);
}
void executeRuleLookup(HepInstruction.RuleLookup instruction,
HepInstruction.RuleLookup.State state) {
if (state.programState.skippingGroup()) {
return;
}
RelOptRule rule = state.rule;
if (rule == null) {
state.rule = rule = getRuleByDescription(instruction.ruleDescription);
LOGGER.trace("Looking up rule with description {}, found {}",
instruction.ruleDescription, rule);
}
if (rule != null) {
applyRules(state.programState, ImmutableList.of(rule), true);
}
}
void executeRuleClass(HepInstruction.RuleClass instruction,
HepInstruction.RuleClass.State state) {
if (state.programState.skippingGroup()) {
return;
}
LOGGER.trace("Applying rule class {}", instruction.ruleClass);
Set<RelOptRule> ruleSet = state.ruleSet;
if (ruleSet == null) {
state.ruleSet = ruleSet = new LinkedHashSet<>();
Class<?> ruleClass = instruction.ruleClass;
for (RelOptRule rule : mapDescToRule.values()) {
if (ruleClass.isInstance(rule)) {
ruleSet.add(rule);
}
}
}
applyRules(state.programState, ruleSet, true);
}
void executeRuleCollection(HepInstruction.RuleCollection instruction,
HepInstruction.RuleCollection.State state) {
if (state.programState.skippingGroup()) {
return;
}
applyRules(state.programState, instruction.rules, true);
}
void executeConverterRules(HepInstruction.ConverterRules instruction,
HepInstruction.ConverterRules.State state) {
checkArgument(state.programState.group == null);
Set<RelOptRule> ruleSet = state.ruleSet;
if (ruleSet == null) {
state.ruleSet = ruleSet = new LinkedHashSet<>();
for (RelOptRule rule : mapDescToRule.values()) {
if (!(rule instanceof ConverterRule)) {
continue;
}
ConverterRule converter = (ConverterRule) rule;
if (converter.isGuaranteed() != instruction.guaranteed) {
continue;
}
// Add the rule itself to work top-down
ruleSet.add(converter);
if (!instruction.guaranteed) {
// Add a TraitMatchingRule to work bottom-up
ruleSet.add(
TraitMatchingRule.config(converter, RelFactories.LOGICAL_BUILDER)
.toRule());
}
}
}
applyRules(state.programState, ruleSet, instruction.guaranteed);
}
void executeCommonRelSubExprRules(
HepInstruction.CommonRelSubExprRules instruction,
HepInstruction.CommonRelSubExprRules.State state) {
checkArgument(state.programState.group == null);
Set<RelOptRule> ruleSet = state.ruleSet;
if (ruleSet == null) {
state.ruleSet = ruleSet = new LinkedHashSet<>();
for (RelOptRule rule : mapDescToRule.values()) {
if (!(rule instanceof CommonRelSubExprRule)) {
continue;
}
ruleSet.add(rule);
}
}
applyRules(state.programState, ruleSet, true);
}
void executeSubProgram(HepInstruction.SubProgram instruction,
HepInstruction.SubProgram.State state) {
LOGGER.trace("Entering subprogram");
for (;;) {
int nTransformationsBefore = nTransformations;
state.programState.execute();
if (nTransformations == nTransformationsBefore) {
// Nothing happened this time around.
break;
}
}
LOGGER.trace("Leaving subprogram");
}
void executeBeginGroup(HepInstruction.BeginGroup instruction,
HepInstruction.BeginGroup.State state) {
checkArgument(state.programState.group == null);
state.programState.group = state.endGroup;
LOGGER.trace("Entering group");
}
void executeEndGroup(HepInstruction.EndGroup instruction,
HepInstruction.EndGroup.State state) {
checkArgument(state.programState.group == state);
state.programState.group = null;
state.collecting = false;
applyRules(state.programState, state.ruleSet, true);
LOGGER.trace("Leaving group");
}
private int depthFirstApply(HepProgram.State programState,
Iterator<HepRelVertex> iter, Collection<RelOptRule> rules,
boolean forceConversions, int nMatches) {
while (iter.hasNext()) {
HepRelVertex vertex = iter.next();
for (RelOptRule rule : rules) {
HepRelVertex newVertex =
applyRule(rule, vertex, forceConversions);
if (newVertex == null || newVertex == vertex) {
continue;
}
++nMatches;
if (nMatches >= programState.matchLimit) {
return nMatches;
}
// To the extent possible, pick up where we left
// off; have to create a new iterator because old
// one was invalidated by transformation.
Iterator<HepRelVertex> depthIter =
getGraphIterator(programState, newVertex);
nMatches =
depthFirstApply(programState, depthIter, rules, forceConversions,
nMatches);
break;
}
}
return nMatches;
}
private void applyRules(HepProgram.State programState,
Collection<RelOptRule> rules, boolean forceConversions) {
final HepInstruction.EndGroup.State group = programState.group;
if (group != null) {
checkArgument(group.collecting);
Set<RelOptRule> ruleSet = requireNonNull(group.ruleSet, "group.ruleSet");
ruleSet.addAll(rules);
return;
}
LOGGER.trace("Applying rule set {}", rules);
final boolean fullRestartAfterTransformation =
programState.matchOrder != HepMatchOrder.ARBITRARY
&& programState.matchOrder != HepMatchOrder.DEPTH_FIRST;
int nMatches = 0;
boolean fixedPoint;
do {
Iterator<HepRelVertex> iter =
getGraphIterator(programState, requireNonNull(root, "root"));
fixedPoint = true;
while (iter.hasNext()) {
HepRelVertex vertex = iter.next();
for (RelOptRule rule : rules) {
HepRelVertex newVertex =
applyRule(rule, vertex, forceConversions);
if (newVertex == null || newVertex == vertex) {
continue;
}
++nMatches;
if (nMatches >= programState.matchLimit) {
return;
}
if (fullRestartAfterTransformation) {
iter = getGraphIterator(programState, requireNonNull(root, "root"));
} else {
// To the extent possible, pick up where we left
// off; have to create a new iterator because old
// one was invalidated by transformation.
iter = getGraphIterator(programState, newVertex);
if (programState.matchOrder == HepMatchOrder.DEPTH_FIRST) {
nMatches =
depthFirstApply(programState, iter, rules, forceConversions, nMatches);
if (nMatches >= programState.matchLimit) {
return;
}
}
// Remember to go around again since we're
// skipping some stuff.
fixedPoint = false;
}
break;
}
}
} while (!fixedPoint);
}
private Iterator<HepRelVertex> getGraphIterator(
HepProgram.State programState, HepRelVertex start) {
// Make sure there's no garbage, because topological sort
// doesn't start from a specific root, and rules can't
// deal with firing on garbage.
// FIXME jvs 25-Sept-2006: I had to move this earlier because
// of FRG-215, which is still under investigation. Once we
// figure that one out, move down to location below for
// better optimizer performance.
collectGarbage();
switch (requireNonNull(programState.matchOrder, "programState.matchOrder")) {
case ARBITRARY:
case DEPTH_FIRST:
return DepthFirstIterator.of(graph, start).iterator();
case TOP_DOWN:
assert start == root;
// see above
/*
collectGarbage();
*/
return TopologicalOrderIterator.of(graph).iterator();
case BOTTOM_UP:
default:
assert start == root;
// see above
/*
collectGarbage();
*/
// TODO jvs 4-Apr-2006: enhance TopologicalOrderIterator
// to support reverse walk.
final List<HepRelVertex> list = new ArrayList<>();
for (HepRelVertex vertex : TopologicalOrderIterator.of(graph)) {
list.add(vertex);
}
Collections.reverse(list);
return list.iterator();
}
}
private @Nullable HepRelVertex applyRule(
RelOptRule rule,
HepRelVertex vertex,
boolean forceConversions) {
if (!graph.vertexSet().contains(vertex)) {
return null;
}
RelTrait parentTrait = null;
List<RelNode> parents = null;
if (rule instanceof ConverterRule) {
// Guaranteed converter rules require special casing to make sure
// they only fire where actually needed, otherwise they tend to
// fire to infinity and beyond.
ConverterRule converterRule = (ConverterRule) rule;
if (converterRule.isGuaranteed() || !forceConversions) {
if (!doesConverterApply(converterRule, vertex)) {
return null;
}
parentTrait = converterRule.getOutTrait();
}
} else if (rule instanceof CommonRelSubExprRule) {
// Only fire CommonRelSubExprRules if the vertex is a common
// subexpression.
List<HepRelVertex> parentVertices = getVertexParents(vertex);
if (parentVertices.size() < 2) {
return null;
}
parents = new ArrayList<>();
for (HepRelVertex pVertex : parentVertices) {
parents.add(pVertex.getCurrentRel());
}
}
final List<RelNode> bindings = new ArrayList<>();
final Map<RelNode, List<RelNode>> nodeChildren = new HashMap<>();
boolean match =
matchOperands(
rule.getOperand(),
vertex.getCurrentRel(),
bindings,
nodeChildren);
if (!match) {
return null;
}
HepRuleCall call =
new HepRuleCall(
this,
rule.getOperand(),
bindings.toArray(new RelNode[0]),
nodeChildren,
parents);
// Allow the rule to apply its own side-conditions.
if (!rule.matches(call)) {
return null;
}
fireRule(call);
if (!call.getResults().isEmpty()) {
return applyTransformationResults(
vertex,
call,
parentTrait);
}
return null;
}
private boolean doesConverterApply(
ConverterRule converterRule,
HepRelVertex vertex) {
RelTrait outTrait = converterRule.getOutTrait();
List<HepRelVertex> parents = Graphs.predecessorListOf(graph, vertex);
for (HepRelVertex parent : parents) {
RelNode parentRel = parent.getCurrentRel();
if (parentRel instanceof Converter) {
// We don't support converter chains.
continue;
}
if (parentRel.getTraitSet().contains(outTrait)) {
// This parent wants the traits produced by the converter.
return true;
}
}
return (vertex == root)
&& (requestedRootTraits != null)
&& requestedRootTraits.contains(outTrait);
}
/**
* Retrieves the parent vertices of a vertex. If a vertex appears multiple
* times as an input into a parent, then that counts as multiple parents,
* one per input reference.
*
* @param vertex the vertex
* @return the list of parents for the vertex
*/
private List<HepRelVertex> getVertexParents(HepRelVertex vertex) {
final List<HepRelVertex> parents = new ArrayList<>();
final List<HepRelVertex> parentVertices =
Graphs.predecessorListOf(graph, vertex);
for (HepRelVertex pVertex : parentVertices) {
RelNode parent = pVertex.getCurrentRel();
for (int i = 0; i < parent.getInputs().size(); i++) {
HepRelVertex child = (HepRelVertex) parent.getInputs().get(i);
if (child == vertex) {
parents.add(pVertex);
}
}
}
return parents;
}
private static boolean matchOperands(
RelOptRuleOperand operand,
RelNode rel,
List<RelNode> bindings,
Map<RelNode, List<RelNode>> nodeChildren) {
if (!operand.matches(rel)) {
return false;
}
for (RelNode input : rel.getInputs()) {
if (!(input instanceof HepRelVertex)) {
// The graph could be partially optimized for materialized view. In that
// case, the input would be a RelNode and shouldn't be matched again here.
return false;
}
}
bindings.add(rel);
@SuppressWarnings("unchecked")
List<HepRelVertex> childRels = (List) rel.getInputs();
switch (operand.childPolicy) {
case ANY:
return true;
case UNORDERED:
// For each operand, at least one child must match. If
// matchAnyChildren, usually there's just one operand.
for (RelOptRuleOperand childOperand : operand.getChildOperands()) {
boolean match = false;
for (HepRelVertex childRel : childRels) {
match =
matchOperands(
childOperand,
childRel.getCurrentRel(),
bindings,
nodeChildren);
if (match) {
break;
}
}
if (!match) {
return false;
}
}
final List<RelNode> children = new ArrayList<>(childRels.size());
for (HepRelVertex childRel : childRels) {
children.add(childRel.getCurrentRel());
}
nodeChildren.put(rel, children);
return true;
default:
int n = operand.getChildOperands().size();
if (childRels.size() < n) {
return false;
}
for (Pair<HepRelVertex, RelOptRuleOperand> pair
: Pair.zip(childRels, operand.getChildOperands())) {
boolean match =
matchOperands(
pair.right,
pair.left.getCurrentRel(),
bindings,
nodeChildren);
if (!match) {
return false;
}
}
return true;
}
}
private HepRelVertex applyTransformationResults(
HepRelVertex vertex,
HepRuleCall call,
@Nullable RelTrait parentTrait) {
// TODO jvs 5-Apr-2006: Take the one that gives the best
// global cost rather than the best local cost. That requires
// "tentative" graph edits.
assert !call.getResults().isEmpty();
RelNode bestRel = null;
if (call.getResults().size() == 1) {
// No costing required; skip it to minimize the chance of hitting
// rels without cost information.
bestRel = call.getResults().get(0);
} else {
RelOptCost bestCost = null;
final RelMetadataQuery mq = call.getMetadataQuery();
for (RelNode rel : call.getResults()) {
RelOptCost thisCost = getCost(rel, mq);
if (LOGGER.isTraceEnabled()) {
// Keep in the isTraceEnabled for the getRowCount method call
LOGGER.trace("considering {} with cumulative cost={} and rowcount={}",
rel, thisCost, mq.getRowCount(rel));
}
if (thisCost == null) {
continue;
}
if (bestRel == null || thisCost.isLt(castNonNull(bestCost))) {
bestRel = rel;
bestCost = thisCost;
}
}
}
++nTransformations;
notifyTransformation(
call,
requireNonNull(bestRel, "bestRel"),
true);
// Before we add the result, make a copy of the list of vertex's
// parents. We'll need this later during contraction so that
// we only update the existing parents, not the new parents
// (otherwise loops can result). Also take care of filtering
// out parents by traits in case we're dealing with a converter rule.
final List<HepRelVertex> allParents =
Graphs.predecessorListOf(graph, vertex);
final List<HepRelVertex> parents = new ArrayList<>();
for (HepRelVertex parent : allParents) {
if (parentTrait != null) {
RelNode parentRel = parent.getCurrentRel();
if (parentRel instanceof Converter) {
// We don't support automatically chaining conversions.
// Treating a converter as a candidate parent here
// can cause the "iParentMatch" check below to
// throw away a new converter needed in
// the multi-parent DAG case.
continue;
}
if (!parentRel.getTraitSet().contains(parentTrait)) {
// This parent does not want the converted result.
continue;
}
}
parents.add(parent);
}
HepRelVertex newVertex = addRelToGraph(bestRel);
// There's a chance that newVertex is the same as one
// of the parents due to common subexpression recognition
// (e.g. the LogicalProject added by JoinCommuteRule). In that
// case, treat the transformation as a nop to avoid
// creating a loop.
int iParentMatch = parents.indexOf(newVertex);
if (iParentMatch != -1) {
newVertex = parents.get(iParentMatch);
} else {
contractVertices(newVertex, vertex, parents);
}
if (getListener() != null) {
// Assume listener doesn't want to see garbage.
collectGarbage();
}
notifyTransformation(
call,
bestRel,
false);
dumpGraph();
return newVertex;
}
@Override public RelNode register(
RelNode rel,
@Nullable RelNode equivRel) {
// Ignore; this call is mostly to tell Volcano how to avoid
// infinite loops.
return rel;
}
@Override public void onCopy(RelNode rel, RelNode newRel) {
onCopyHook.apply(rel, newRel);
}
@Override public RelNode ensureRegistered(RelNode rel, @Nullable RelNode equivRel) {
return rel;
}
@Override public boolean isRegistered(RelNode rel) {
return true;
}
private HepRelVertex addRelToGraph(
RelNode rel) {
// Check if a transformation already produced a reference
// to an existing vertex.
if (graph.vertexSet().contains(rel)) {
return (HepRelVertex) rel;
}
// Recursively add children, replacing this rel's inputs
// with corresponding child vertices.
final List<RelNode> inputs = rel.getInputs();
final List<RelNode> newInputs = new ArrayList<>();
for (RelNode input1 : inputs) {
HepRelVertex childVertex = addRelToGraph(input1);
newInputs.add(childVertex);
}
if (!Util.equalShallow(inputs, newInputs)) {
RelNode oldRel = rel;
rel = rel.copy(rel.getTraitSet(), newInputs);
onCopy(oldRel, rel);
}
// Compute digest first time we add to DAG,
// otherwise can't get equivVertex for common sub-expression
rel.recomputeDigest();
// try to find equivalent rel only if DAG is allowed
if (!noDag) {
// Now, check if an equivalent vertex already exists in graph.
HepRelVertex equivVertex = mapDigestToVertex.get(rel.getRelDigest());
if (equivVertex != null) {
// Use existing vertex.
return equivVertex;
}
}
// No equivalence: create a new vertex to represent this rel.
HepRelVertex newVertex = new HepRelVertex(rel);
graph.addVertex(newVertex);
updateVertex(newVertex, rel);
for (RelNode input : rel.getInputs()) {
graph.addEdge(newVertex, (HepRelVertex) input);
}
nTransformations++;
return newVertex;
}
private void contractVertices(
HepRelVertex preservedVertex,
HepRelVertex discardedVertex,
List<HepRelVertex> parents) {
if (preservedVertex == discardedVertex) {
// Nop.
return;
}
RelNode rel = preservedVertex.getCurrentRel();
updateVertex(preservedVertex, rel);
// Update specified parents of discardedVertex.
for (HepRelVertex parent : parents) {
RelNode parentRel = parent.getCurrentRel();
List<RelNode> inputs = parentRel.getInputs();
for (int i = 0; i < inputs.size(); ++i) {
RelNode child = inputs.get(i);
if (child != discardedVertex) {
continue;
}
parentRel.replaceInput(i, preservedVertex);
}
clearCache(parent);
graph.removeEdge(parent, discardedVertex);
graph.addEdge(parent, preservedVertex);
updateVertex(parent, parentRel);
}
// NOTE: we don't actually do graph.removeVertex(discardedVertex),
// because it might still be reachable from preservedVertex.
// Leave that job for garbage collection.
if (discardedVertex == root) {
root = preservedVertex;
}
}
/**
* Clears metadata cache for the RelNode and its ancestors.
*
* @param vertex relnode
*/
private void clearCache(HepRelVertex vertex) {
RelMdUtil.clearCache(vertex.getCurrentRel());
if (!RelMdUtil.clearCache(vertex)) {
return;
}
Queue<DefaultEdge> queue =
new ArrayDeque<>(graph.getInwardEdges(vertex));
while (!queue.isEmpty()) {
DefaultEdge edge = queue.remove();
HepRelVertex source = (HepRelVertex) edge.source;
RelMdUtil.clearCache(source.getCurrentRel());
if (RelMdUtil.clearCache(source)) {
queue.addAll(graph.getInwardEdges(source));
}
}
}
private void updateVertex(HepRelVertex vertex, RelNode rel) {
if (rel != vertex.getCurrentRel()) {
// REVIEW jvs 5-Apr-2006: We'll do this again later
// during garbage collection. Or we could get rid
// of mark/sweep garbage collection and do it precisely
// at this point by walking down to all rels which are only
// reachable from here.
notifyDiscard(vertex.getCurrentRel());
}
RelDigest oldKey = vertex.getCurrentRel().getRelDigest();
if (mapDigestToVertex.get(oldKey) == vertex) {
mapDigestToVertex.remove(oldKey);
}
// When a transformation happened in one rule apply, support
// vertex2 replace vertex1, but the current relNode of
// vertex1 and vertex2 is same,
// then the digest is also same. but we can't remove vertex2,
// otherwise the digest will be removed wrongly in the mapDigestToVertex
// when collectGC
// so it must update the digest that map to vertex
mapDigestToVertex.put(rel.getRelDigest(), vertex);
if (rel != vertex.getCurrentRel()) {
vertex.replaceRel(rel);
}
notifyEquivalence(
rel,
vertex,
false);
}
private RelNode buildFinalPlan(HepRelVertex vertex) {
RelNode rel = vertex.getCurrentRel();
notifyChosen(rel);
// Recursively process children, replacing this rel's inputs
// with corresponding child rels.
List<RelNode> inputs = rel.getInputs();
for (int i = 0; i < inputs.size(); ++i) {
RelNode child = inputs.get(i);
if (!(child instanceof HepRelVertex)) {
// Already replaced.
continue;
}
child = buildFinalPlan((HepRelVertex) child);
rel.replaceInput(i, child);
}
RelMdUtil.clearCache(rel);
rel.recomputeDigest();
return rel;
}
private void collectGarbage() {
if (nTransformations == nTransformationsLastGC) {
// No modifications have taken place since the last gc,
// so there can't be any garbage.
return;
}
nTransformationsLastGC = nTransformations;
LOGGER.trace("collecting garbage");
// Yer basic mark-and-sweep.
final Set<HepRelVertex> rootSet = new HashSet<>();
HepRelVertex root = requireNonNull(this.root, "this.root");
if (graph.vertexSet().contains(root)) {
BreadthFirstIterator.reachable(rootSet, graph, root);
}
if (rootSet.size() == graph.vertexSet().size()) {
// Everything is reachable: no garbage to collect.
return;
}
final Set<HepRelVertex> sweepSet = new HashSet<>();
for (HepRelVertex vertex : graph.vertexSet()) {
if (!rootSet.contains(vertex)) {
sweepSet.add(vertex);
RelNode rel = vertex.getCurrentRel();
notifyDiscard(rel);
}
}
assert !sweepSet.isEmpty();
graph.removeAllVertices(sweepSet);
graphSizeLastGC = graph.vertexSet().size();
// Clean up digest map too.
Iterator<Map.Entry<RelDigest, HepRelVertex>> digestIter =
mapDigestToVertex.entrySet().iterator();
while (digestIter.hasNext()) {
HepRelVertex vertex = digestIter.next().getValue();
if (sweepSet.contains(vertex)) {