/
DrillJoinRelBase.java
165 lines (140 loc) · 6.61 KB
/
DrillJoinRelBase.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
/**
* 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.drill.exec.planner.common;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.drill.exec.ExecConstants;
import org.apache.drill.exec.expr.holders.IntHolder;
import org.apache.drill.exec.planner.cost.DrillCostBase;
import org.apache.drill.exec.physical.impl.join.JoinUtils;
import org.apache.drill.exec.physical.impl.join.JoinUtils.JoinCategory;
import org.apache.drill.exec.planner.cost.DrillCostBase.DrillCostFactory;
import org.apache.drill.exec.planner.physical.PrelUtil;
import org.apache.calcite.rel.InvalidRelException;
import org.apache.calcite.rel.core.Join;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptCost;
import org.apache.calcite.plan.RelOptPlanner;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rex.RexNode;
import com.google.common.collect.Lists;
/**
* Base class for logical and physical Joins implemented in Drill.
*/
public abstract class DrillJoinRelBase extends Join implements DrillRelNode {
protected List<Integer> leftKeys = Lists.newArrayList();
protected List<Integer> rightKeys = Lists.newArrayList() ;
private final double joinRowFactor;
public DrillJoinRelBase(RelOptCluster cluster, RelTraitSet traits, RelNode left, RelNode right, RexNode condition,
JoinRelType joinType) throws InvalidRelException {
super(cluster, traits, left, right, condition, joinType, Collections.<String> emptySet());
this.joinRowFactor = PrelUtil.getPlannerSettings(cluster.getPlanner()).getRowCountEstimateFactor();
}
@Override
public RelOptCost computeSelfCost(RelOptPlanner planner) {
JoinCategory category = JoinUtils.getJoinCategory(left, right, condition, leftKeys, rightKeys);
if (category == JoinCategory.CARTESIAN || category == JoinCategory.INEQUALITY) {
if (PrelUtil.getPlannerSettings(planner).isNestedLoopJoinEnabled()) {
if (PrelUtil.getPlannerSettings(planner).isNlJoinForScalarOnly()) {
if (hasScalarSubqueryInput()) {
return computeLogicalJoinCost(planner);
} else {
return ((DrillCostFactory)planner.getCostFactory()).makeInfiniteCost();
}
} else {
return computeLogicalJoinCost(planner);
}
}
return ((DrillCostFactory)planner.getCostFactory()).makeInfiniteCost();
}
return computeLogicalJoinCost(planner);
}
@Override
public double getRows() {
if (this.condition.isAlwaysTrue()) {
return joinRowFactor * this.getLeft().getRows() * this.getRight().getRows();
} else {
return joinRowFactor * Math.max(this.getLeft().getRows(), this.getRight().getRows());
}
}
/**
* Returns whether there are any elements in common between left and right.
*/
private static <T> boolean intersects(List<T> left, List<T> right) {
return new HashSet<>(left).removeAll(right);
}
protected boolean uniqueFieldNames(RelDataType rowType) {
return isUnique(rowType.getFieldNames());
}
protected static <T> boolean isUnique(List<T> list) {
return new HashSet<>(list).size() == list.size();
}
public List<Integer> getLeftKeys() {
return this.leftKeys;
}
public List<Integer> getRightKeys() {
return this.rightKeys;
}
protected RelOptCost computeLogicalJoinCost(RelOptPlanner planner) {
// During Logical Planning, although we don't care much about the actual physical join that will
// be chosen, we do care about which table - bigger or smaller - is chosen as the right input
// of the join since that is important at least for hash join and we don't currently have
// hybrid-hash-join that can swap the inputs dynamically. The Calcite planner's default cost of a join
// is the same whether the bigger table is used as left input or right. In order to overcome that,
// we will use the Hash Join cost as the logical cost such that cardinality of left and right inputs
// is considered appropriately.
return computeHashJoinCost(planner);
}
protected RelOptCost computeHashJoinCost(RelOptPlanner planner) {
double probeRowCount = RelMetadataQuery.getRowCount(this.getLeft());
double buildRowCount = RelMetadataQuery.getRowCount(this.getRight());
// cpu cost of hashing the join keys for the build side
double cpuCostBuild = DrillCostBase.HASH_CPU_COST * getRightKeys().size() * buildRowCount;
// cpu cost of hashing the join keys for the probe side
double cpuCostProbe = DrillCostBase.HASH_CPU_COST * getLeftKeys().size() * probeRowCount;
// cpu cost of evaluating each leftkey=rightkey join condition
double joinConditionCost = DrillCostBase.COMPARE_CPU_COST * this.getLeftKeys().size();
double factor = PrelUtil.getPlannerSettings(planner).getOptions()
.getOption(ExecConstants.HASH_JOIN_TABLE_FACTOR_KEY).float_val;
long fieldWidth = PrelUtil.getPlannerSettings(planner).getOptions()
.getOption(ExecConstants.AVERAGE_FIELD_WIDTH_KEY).num_val;
// table + hashValues + links
double memCost =
(
(fieldWidth * this.getRightKeys().size()) +
IntHolder.WIDTH +
IntHolder.WIDTH
) * buildRowCount * factor;
double cpuCost = joinConditionCost * (probeRowCount) // probe size determine the join condition comparison cost
+ cpuCostBuild + cpuCostProbe ;
DrillCostFactory costFactory = (DrillCostFactory) planner.getCostFactory();
return costFactory.makeCost(buildRowCount + probeRowCount, cpuCost, 0, 0, memCost);
}
private boolean hasScalarSubqueryInput() {
if (JoinUtils.isScalarSubquery(this.getLeft())
|| JoinUtils.isScalarSubquery(this.getRight())) {
return true;
}
return false;
}
}