-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathH2Join.java
More file actions
105 lines (87 loc) · 3.72 KB
/
H2Join.java
File metadata and controls
105 lines (87 loc) · 3.72 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
package sqlancer.h2.ast;
import java.util.ArrayList;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.common.ast.newast.Join;
import sqlancer.h2.H2ExpressionGenerator;
import sqlancer.h2.H2Provider.H2GlobalState;
import sqlancer.h2.H2Schema.H2Column;
import sqlancer.h2.H2Schema.H2Table;
public class H2Join implements H2Expression, Join<H2Expression, H2Table, H2Column> {
private final H2TableReference leftTable;
private final H2TableReference rightTable;
private final JoinType joinType;
private H2Expression onCondition;
public enum JoinType {
INNER, CROSS, NATURAL, LEFT, RIGHT;
public static JoinType getRandom() {
return Randomly.fromOptions(values());
}
}
public H2Join(H2TableReference leftTable, H2TableReference rightTable, JoinType joinType,
H2Expression whereCondition) {
this.leftTable = leftTable;
this.rightTable = rightTable;
this.joinType = joinType;
this.onCondition = whereCondition;
}
public H2TableReference getLeftTable() {
return leftTable;
}
public H2TableReference getRightTable() {
return rightTable;
}
public JoinType getJoinType() {
return joinType;
}
public H2Expression getOnCondition() {
return onCondition;
}
public static List<H2Join> getJoins(List<H2TableReference> tableList, H2GlobalState globalState) {
List<H2Join> joinExpressions = new ArrayList<>();
while (tableList.size() >= 2 && Randomly.getBooleanWithRatherLowProbability()) {
H2TableReference leftTable = tableList.remove(0);
H2TableReference rightTable = tableList.remove(0);
List<H2Column> columns = new ArrayList<>(leftTable.getTable().getColumns());
columns.addAll(rightTable.getTable().getColumns());
H2ExpressionGenerator joinGen = new H2ExpressionGenerator(globalState).setColumns(columns);
JoinType random = H2Join.JoinType.getRandom();
switch (random) {
case INNER:
joinExpressions.add(H2Join.createInnerJoin(leftTable, rightTable, joinGen.generateExpression()));
break;
case NATURAL:
joinExpressions.add(H2Join.createNaturalJoin(leftTable, rightTable));
break;
case LEFT:
joinExpressions.add(H2Join.createLeftOuterJoin(leftTable, rightTable, joinGen.generateExpression()));
break;
case RIGHT:
joinExpressions.add(H2Join.createRightOuterJoin(leftTable, rightTable, joinGen.generateExpression()));
break;
case CROSS:
joinExpressions.add(new H2Join(leftTable, rightTable, JoinType.CROSS, null));
break;
default:
throw new AssertionError(random);
}
}
return joinExpressions;
}
public static H2Join createRightOuterJoin(H2TableReference left, H2TableReference right, H2Expression predicate) {
return new H2Join(left, right, JoinType.RIGHT, predicate);
}
public static H2Join createLeftOuterJoin(H2TableReference left, H2TableReference right, H2Expression predicate) {
return new H2Join(left, right, JoinType.LEFT, predicate);
}
public static H2Join createInnerJoin(H2TableReference left, H2TableReference right, H2Expression predicate) {
return new H2Join(left, right, JoinType.INNER, predicate);
}
public static H2Join createNaturalJoin(H2TableReference left, H2TableReference right) {
return new H2Join(left, right, JoinType.NATURAL, null);
}
@Override
public void setOnClause(H2Expression onClause) {
onCondition = onClause;
}
}