-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathTiDBJoin.java
More file actions
120 lines (98 loc) · 4.29 KB
/
TiDBJoin.java
File metadata and controls
120 lines (98 loc) · 4.29 KB
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
package sqlancer.tidb.ast;
import java.util.ArrayList;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.tidb.TiDBExpressionGenerator;
import sqlancer.tidb.TiDBProvider.TiDBGlobalState;
import sqlancer.tidb.TiDBSchema.TiDBColumn;
public class TiDBJoin implements TiDBExpression {
private final TiDBExpression leftTable;
private final TiDBExpression rightTable;
private final JoinType joinType;
private final TiDBExpression onCondition;
private NaturalJoinType outerType;
public enum JoinType {
INNER, NATURAL, STRAIGHT, LEFT, RIGHT;
public static JoinType getRandom() {
return Randomly.fromOptions(values());
}
}
public enum NaturalJoinType {
INNER, LEFT, RIGHT;
public static NaturalJoinType getRandom() {
return Randomly.fromOptions(values());
}
}
public TiDBJoin(TiDBExpression leftTable, TiDBExpression rightTable, JoinType joinType,
TiDBExpression whereCondition) {
this.leftTable = leftTable;
this.rightTable = rightTable;
this.joinType = joinType;
this.onCondition = whereCondition;
}
public TiDBExpression getLeftTable() {
return leftTable;
}
public TiDBExpression getRightTable() {
return rightTable;
}
public JoinType getJoinType() {
return joinType;
}
public TiDBExpression getOnCondition() {
return onCondition;
}
public static TiDBJoin createNaturalJoin(TiDBExpression left, TiDBExpression right, NaturalJoinType type) {
TiDBJoin tiDBJoin = new TiDBJoin(left, right, JoinType.NATURAL, null);
tiDBJoin.setNaturalJoinType(type);
return tiDBJoin;
}
public static TiDBJoin createInnerJoin(TiDBExpression left, TiDBExpression right, TiDBExpression onClause) {
return new TiDBJoin(left, right, JoinType.INNER, onClause);
}
public static TiDBJoin createLeftOuterJoin(TiDBExpression left, TiDBExpression right, TiDBExpression onClause) {
return new TiDBJoin(left, right, JoinType.LEFT, onClause);
}
public static TiDBJoin createRightOuterJoin(TiDBExpression left, TiDBExpression right, TiDBExpression onClause) {
return new TiDBJoin(left, right, JoinType.RIGHT, onClause);
}
public static TiDBJoin createStraightJoin(TiDBExpression left, TiDBExpression right, TiDBExpression onClause) {
return new TiDBJoin(left, right, JoinType.STRAIGHT, onClause);
}
private void setNaturalJoinType(NaturalJoinType outerType) {
this.outerType = outerType;
}
public NaturalJoinType getNaturalJoinType() {
return outerType;
}
public static List<TiDBExpression> getJoins(List<TiDBExpression> tableList, TiDBGlobalState globalState) {
List<TiDBExpression> joinExpressions = new ArrayList<>();
while (tableList.size() >= 2 && Randomly.getBoolean()) {
TiDBTableReference leftTable = (TiDBTableReference) tableList.remove(0);
TiDBTableReference rightTable = (TiDBTableReference) tableList.remove(0);
List<TiDBColumn> columns = new ArrayList<>(leftTable.getTable().getColumns());
columns.addAll(rightTable.getTable().getColumns());
TiDBExpressionGenerator joinGen = new TiDBExpressionGenerator(globalState).setColumns(columns);
switch (TiDBJoin.JoinType.getRandom()) {
case INNER:
joinExpressions.add(TiDBJoin.createInnerJoin(leftTable, rightTable, joinGen.generateExpression()));
break;
case NATURAL:
joinExpressions.add(TiDBJoin.createNaturalJoin(leftTable, rightTable, NaturalJoinType.getRandom()));
break;
case STRAIGHT:
joinExpressions.add(TiDBJoin.createStraightJoin(leftTable, rightTable, joinGen.generateExpression()));
break;
case LEFT:
joinExpressions.add(TiDBJoin.createLeftOuterJoin(leftTable, rightTable, joinGen.generateExpression()));
break;
case RIGHT:
joinExpressions.add(TiDBJoin.createRightOuterJoin(leftTable, rightTable, joinGen.generateExpression()));
break;
default:
throw new AssertionError();
}
}
return joinExpressions;
}
}