/
EnumerableMergeJoinRule.java
108 lines (103 loc) · 4.09 KB
/
EnumerableMergeJoinRule.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
/*
* 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.adapter.enumerable;
import org.apache.calcite.linq4j.Ord;
import org.apache.calcite.plan.Convention;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelCollations;
import org.apache.calcite.rel.RelFieldCollation;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.convert.ConverterRule;
import org.apache.calcite.rel.core.JoinInfo;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rel.logical.LogicalJoin;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexUtil;
import java.util.ArrayList;
import java.util.List;
/** Planner rule that converts a
* {@link org.apache.calcite.rel.logical.LogicalJoin} relational expression
* {@link EnumerableConvention enumerable calling convention}.
*
* @see EnumerableJoinRule
*/
class EnumerableMergeJoinRule extends ConverterRule {
EnumerableMergeJoinRule() {
super(LogicalJoin.class,
Convention.NONE,
EnumerableConvention.INSTANCE,
"EnumerableMergeJoinRule");
}
@Override public RelNode convert(RelNode rel) {
LogicalJoin join = (LogicalJoin) rel;
final JoinInfo info = join.analyzeCondition();
if (join.getJoinType() != JoinRelType.INNER) {
// EnumerableMergeJoin only supports inner join.
// (It supports non-equi join, using a post-filter; see below.)
return null;
}
if (info.pairs().size() == 0) {
// EnumerableMergeJoin CAN support cartesian join, but disable it for now.
return null;
}
final List<RelNode> newInputs = new ArrayList<>();
final List<RelCollation> collations = new ArrayList<>();
int offset = 0;
for (Ord<RelNode> ord : Ord.zip(join.getInputs())) {
RelTraitSet traits = ord.e.getTraitSet()
.replace(EnumerableConvention.INSTANCE);
if (!info.pairs().isEmpty()) {
final List<RelFieldCollation> fieldCollations = new ArrayList<>();
for (int key : info.keys().get(ord.i)) {
fieldCollations.add(
new RelFieldCollation(key, RelFieldCollation.Direction.ASCENDING,
RelFieldCollation.NullDirection.LAST));
}
final RelCollation collation = RelCollations.of(fieldCollations);
collations.add(RelCollations.shift(collation, offset));
traits = traits.replace(collation);
}
newInputs.add(convert(ord.e, traits));
offset += ord.e.getRowType().getFieldCount();
}
final RelNode left = newInputs.get(0);
final RelNode right = newInputs.get(1);
final RelOptCluster cluster = join.getCluster();
RelNode newRel;
RelTraitSet traitSet = join.getTraitSet()
.replace(EnumerableConvention.INSTANCE);
if (!collations.isEmpty()) {
traitSet = traitSet.replace(collations);
}
newRel = new EnumerableMergeJoin(cluster,
traitSet,
left,
right,
info.getEquiCondition(left, right, cluster.getRexBuilder()),
join.getVariablesSet(),
join.getJoinType());
if (!info.isEqui()) {
RexNode nonEqui = RexUtil.composeConjunction(cluster.getRexBuilder(),
info.nonEquiConditions);
newRel = new EnumerableFilter(cluster, newRel.getTraitSet(),
newRel, nonEqui);
}
return newRel;
}
}