-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresJoin.java
More file actions
121 lines (99 loc) · 4.11 KB
/
PostgresJoin.java
File metadata and controls
121 lines (99 loc) · 4.11 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
121
package sqlancer.postgres.ast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.common.ast.newast.Join;
import sqlancer.postgres.PostgresGlobalState;
import sqlancer.postgres.PostgresSchema.PostgresColumn;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
import sqlancer.postgres.PostgresSchema.PostgresTable;
import sqlancer.postgres.gen.PostgresExpressionGenerator;
public class PostgresJoin implements PostgresExpression, Join<PostgresExpression, PostgresTable, PostgresColumn> {
public enum PostgresJoinType {
INNER, LEFT, RIGHT, FULL, CROSS;
public static PostgresJoinType getRandom() {
return Randomly.fromOptions(values());
}
public static PostgresJoinType getRandomExcept(PostgresJoinType... exclude) {
PostgresJoinType[] values = Arrays.stream(values()).filter(m -> !Arrays.asList(exclude).contains(m))
.toArray(PostgresJoinType[]::new);
return Randomly.fromOptions(values);
}
}
private final PostgresExpression tableReference;
private PostgresExpression onClause;
private PostgresJoinType type;
private final PostgresExpression leftTable;
private final PostgresExpression rightTable;
public PostgresJoin(PostgresExpression tableReference, PostgresExpression onClause, PostgresJoinType type) {
this.tableReference = tableReference;
this.onClause = onClause;
this.type = type;
this.leftTable = null;
this.rightTable = null;
}
public PostgresJoin(PostgresExpression leftTable, PostgresExpression rightTable, PostgresJoinType joinType,
PostgresExpression whereCondition) {
this.leftTable = leftTable;
this.rightTable = rightTable;
this.type = joinType;
this.onClause = whereCondition;
this.tableReference = null;
}
public static PostgresJoin createJoin(PostgresExpression left, PostgresExpression right, PostgresJoinType type,
PostgresExpression onClause) {
if (type == PostgresJoinType.CROSS) {
return new PostgresJoin(left, right, type, null);
} else {
return new PostgresJoin(left, right, type, onClause);
}
}
public static List<PostgresExpression> getJoins(List<PostgresExpression> tableList,
PostgresGlobalState globalState) {
// Clone Table to prevent the original list from being manipulated
List<PostgresExpression> tbl = new ArrayList<>(tableList);
List<PostgresExpression> joinExpressions = new ArrayList<>();
while (tbl.size() >= 2 && Randomly.getBoolean()) {
PostgresTableReference left = (PostgresTableReference) tbl.remove(0);
PostgresTableReference right = (PostgresTableReference) tbl.remove(0);
List<PostgresColumn> columns = new ArrayList<>();
columns.addAll(left.getTable().getColumns());
columns.addAll(right.getTable().getColumns());
PostgresExpressionGenerator joinGen = new PostgresExpressionGenerator(globalState).setColumns(columns);
joinExpressions.add(PostgresJoin.createJoin(left, right, PostgresJoinType.getRandom(),
joinGen.generateExpression(0, PostgresDataType.BOOLEAN)));
}
return joinExpressions;
}
@Override
public void setOnClause(PostgresExpression clause) {
this.onClause = clause;
}
public void setType(PostgresJoinType type) {
this.type = type;
}
public PostgresExpression getTableReference() {
return tableReference;
}
public PostgresExpression getLeftTable() {
return leftTable;
}
public PostgresExpression getRightTable() {
return rightTable;
}
public PostgresExpression getOnClause() {
return onClause;
}
public PostgresJoinType getType() {
return type;
}
@Override
public PostgresDataType getExpressionType() {
throw new AssertionError();
}
@Override
public PostgresConstant getExpectedValue() {
throw new AssertionError();
}
}