Skip to content

Commit edcb646

Browse files
kasakriszsoumyakanti3578
authored andcommitted
Copy the imlementation of Lopt optimization and apply changes from CALCITE-6737
1 parent 33159af commit edcb646

File tree

7 files changed

+4263
-230
lines changed

7 files changed

+4263
-230
lines changed

Diff for: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/Bug.java

+5
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@ public final class Bug {
100100
* Whether <a href="https://issues.apache.org/jira/browse/CALCITE-6513">CALCITE-6513</a> is fixed.
101101
*/
102102
public static final boolean CALCITE_6513_FIXED = false;
103+
104+
/**
105+
* Whether <a href="https://issues.apache.org/jira/browse/CALCITE-6513">CALCITE-6737</a> is fixed.
106+
*/
107+
public static final boolean CALCITE_6737_FIXED = false;
103108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.hadoop.hive.ql.optimizer.calcite.rules;
18+
19+
import org.apache.calcite.rel.RelNode;
20+
import org.apache.calcite.rel.core.Join;
21+
import org.apache.hadoop.hive.ql.optimizer.calcite.Bug;
22+
import org.checkerframework.checker.initialization.qual.NotOnlyInitialized;
23+
import org.checkerframework.checker.initialization.qual.UnderInitialization;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.Objects;
28+
29+
public class HiveLoptJoinTree {
30+
//~ Instance fields --------------------------------------------------------
31+
32+
@NotOnlyInitialized
33+
private final BinaryTree factorTree;
34+
private final RelNode joinTree;
35+
private final boolean removableSelfJoin;
36+
37+
//~ Constructors -----------------------------------------------------------
38+
39+
/**
40+
* Creates a join-tree consisting of a single node.
41+
*
42+
* @param joinTree RelNode corresponding to the single node
43+
* @param factorId factor id of the node
44+
*/
45+
@SuppressWarnings("argument.type.incompatible")
46+
public HiveLoptJoinTree(RelNode joinTree, int factorId) {
47+
if (Bug.CALCITE_6737_FIXED) {
48+
throw new IllegalStateException("This class is redundant when the fix for CALCITE-6737 is merged into Calcite. " +
49+
"Remove it and use LoptOptimizeJoinRule instead of HiveLoptOptimizeJoinRule");
50+
}
51+
52+
this.joinTree = joinTree;
53+
this.factorTree = new Leaf(factorId, this);
54+
this.removableSelfJoin = false;
55+
}
56+
57+
/**
58+
* Associates the factor ids with a join-tree.
59+
*
60+
* @param joinTree RelNodes corresponding to the join tree
61+
* @param factorTree tree of the factor ids
62+
* @param removableSelfJoin whether the join corresponds to a removable
63+
* self-join
64+
*/
65+
public HiveLoptJoinTree(
66+
RelNode joinTree,
67+
BinaryTree factorTree,
68+
boolean removableSelfJoin) {
69+
if (Bug.CALCITE_6737_FIXED) {
70+
throw new IllegalStateException("This class is redundant when the fix for CALCITE-6737 is merged into Calcite. " +
71+
"Remove it and use LoptOptimizeJoinRule instead of HiveLoptOptimizeJoinRule");
72+
}
73+
74+
this.joinTree = joinTree;
75+
this.factorTree = factorTree;
76+
this.removableSelfJoin = removableSelfJoin;
77+
}
78+
79+
/**
80+
* Associates the factor ids with a join-tree given the factors corresponding
81+
* to the left and right subtrees of the join.
82+
*
83+
* @param joinTree RelNodes corresponding to the join tree
84+
* @param leftFactorTree tree of the factor ids for left subtree
85+
* @param rightFactorTree tree of the factor ids for the right subtree
86+
*/
87+
public HiveLoptJoinTree(
88+
RelNode joinTree,
89+
BinaryTree leftFactorTree,
90+
BinaryTree rightFactorTree) {
91+
this(joinTree, leftFactorTree, rightFactorTree, false);
92+
}
93+
94+
/**
95+
* Associates the factor ids with a join-tree given the factors corresponding
96+
* to the left and right subtrees of the join. Also indicates whether the
97+
* join is a removable self-join.
98+
*
99+
* @param joinTree RelNodes corresponding to the join tree
100+
* @param leftFactorTree tree of the factor ids for left subtree
101+
* @param rightFactorTree tree of the factor ids for the right subtree
102+
* @param removableSelfJoin true if the join is a removable self-join
103+
*/
104+
public HiveLoptJoinTree(
105+
RelNode joinTree,
106+
BinaryTree leftFactorTree,
107+
BinaryTree rightFactorTree,
108+
boolean removableSelfJoin) {
109+
if (Bug.CALCITE_6737_FIXED) {
110+
throw new IllegalStateException("This class is redundant when the fix for CALCITE-6737 is merged into Calcite. "
111+
+ "Remove it and use LoptOptimizeJoinRule instead of HiveLoptOptimizeJoinRule");
112+
}
113+
114+
factorTree = new HiveLoptJoinTree.Node(leftFactorTree, rightFactorTree, this);
115+
this.joinTree = joinTree;
116+
this.removableSelfJoin = removableSelfJoin;
117+
}
118+
119+
//~ Methods ----------------------------------------------------------------
120+
121+
public RelNode getJoinTree() {
122+
return joinTree;
123+
}
124+
125+
public HiveLoptJoinTree getLeft() {
126+
final HiveLoptJoinTree.Node node = (HiveLoptJoinTree.Node) factorTree;
127+
return new HiveLoptJoinTree(
128+
((Join) joinTree).getLeft(),
129+
node.getLeft(),
130+
node.getLeft().getParent().isRemovableSelfJoin());
131+
}
132+
133+
public HiveLoptJoinTree getRight() {
134+
final HiveLoptJoinTree.Node node = (HiveLoptJoinTree.Node) factorTree;
135+
return new HiveLoptJoinTree(
136+
((Join) joinTree).getRight(),
137+
node.getRight(),
138+
node.getRight().getParent().isRemovableSelfJoin());
139+
}
140+
141+
public BinaryTree getFactorTree() {
142+
return factorTree;
143+
}
144+
145+
public List<Integer> getTreeOrder() {
146+
List<Integer> treeOrder = new ArrayList<>();
147+
getTreeOrder(treeOrder);
148+
return treeOrder;
149+
}
150+
151+
public void getTreeOrder(List<Integer> treeOrder) {
152+
factorTree.getTreeOrder(treeOrder);
153+
}
154+
155+
public boolean isRemovableSelfJoin() {
156+
return removableSelfJoin;
157+
}
158+
159+
//~ Inner Classes ----------------------------------------------------------
160+
161+
/**
162+
* Simple binary tree class that stores an id in the leaf nodes and keeps
163+
* track of the parent HiveLoptJoinTree object associated with the binary tree.
164+
*/
165+
protected abstract static class BinaryTree {
166+
@NotOnlyInitialized
167+
private final HiveLoptJoinTree parent;
168+
169+
protected BinaryTree(@UnderInitialization HiveLoptJoinTree parent) {
170+
this.parent = parent;
171+
}
172+
173+
public HiveLoptJoinTree getParent() {
174+
return parent;
175+
}
176+
177+
public abstract void getTreeOrder(List<Integer> treeOrder);
178+
}
179+
180+
/** Binary tree node that has no children. */
181+
protected static class Leaf extends BinaryTree {
182+
private final int id;
183+
184+
public Leaf(int rootId, @UnderInitialization HiveLoptJoinTree parent) {
185+
super(parent);
186+
this.id = rootId;
187+
}
188+
189+
/** Returns the id associated with a leaf node in a binary tree. */
190+
public int getId() {
191+
return id;
192+
}
193+
194+
@Override public void getTreeOrder(List<Integer> treeOrder) {
195+
treeOrder.add(id);
196+
}
197+
}
198+
199+
/** Binary tree node that has two children. */
200+
protected static class Node extends BinaryTree {
201+
private final BinaryTree left;
202+
private final BinaryTree right;
203+
204+
public Node(BinaryTree left, BinaryTree right, @UnderInitialization HiveLoptJoinTree parent) {
205+
super(parent);
206+
this.left = Objects.requireNonNull(left, "left");
207+
this.right = Objects.requireNonNull(right, "right");
208+
}
209+
210+
public BinaryTree getLeft() {
211+
return left;
212+
}
213+
214+
public BinaryTree getRight() {
215+
return right;
216+
}
217+
218+
@Override public void getTreeOrder(List<Integer> treeOrder) {
219+
left.getTreeOrder(treeOrder);
220+
right.getTreeOrder(treeOrder);
221+
}
222+
}
223+
}

0 commit comments

Comments
 (0)