-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathTLPWhereOracle.java
More file actions
206 lines (178 loc) · 9.38 KB
/
Copy pathTLPWhereOracle.java
File metadata and controls
206 lines (178 loc) · 9.38 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package sqlancer.common.oracle;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import sqlancer.ComparatorHelper;
import sqlancer.Randomly;
import sqlancer.Reproducer;
import sqlancer.SQLGlobalState;
import sqlancer.common.ast.newast.Expression;
import sqlancer.common.ast.newast.Join;
import sqlancer.common.ast.newast.Select;
import sqlancer.common.gen.TLPWhereGenerator;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.schema.AbstractSchema;
import sqlancer.common.schema.AbstractTable;
import sqlancer.common.schema.AbstractTableColumn;
import sqlancer.common.schema.AbstractTables;
public class TLPWhereOracle<Z extends Select<J, E, T, C>, J extends Join<E, T, C>, E extends Expression<C>, S extends AbstractSchema<?, T>, T extends AbstractTable<C, ?, ?>, C extends AbstractTableColumn<?, ?>, G extends SQLGlobalState<?, S>>
implements TestOracle<G> {
private final G state;
private TLPWhereGenerator<Z, J, E, T, C> gen;
private final ExpectedErrors errors;
private Reproducer<G> reproducer;
private String generatedQueryString;
// A side's result set, together with the human-readable combined-query strings that
// getCombinedResultSet fills in for the transformed side (unused, and null, for the original side)
private static final class TLPResultSet {
final List<String> resultSet;
final List<String> combinedString;
TLPResultSet(List<String> resultSet, List<String> combinedString) {
this.resultSet = resultSet;
this.combinedString = combinedString;
}
}
private class TLPWhereReproducer extends AbstractComparisonReproducer<G, TLPResultSet> {
final String firstQueryString;
final String secondQueryString;
final String thirdQueryString;
final String originalQueryString;
final boolean orderBy;
TLPWhereReproducer(String firstQueryString, String secondQueryString, String thirdQueryString,
String originalQueryString, boolean orderBy) {
this.firstQueryString = firstQueryString;
this.secondQueryString = secondQueryString;
this.thirdQueryString = thirdQueryString;
this.originalQueryString = originalQueryString;
this.orderBy = orderBy;
}
@Override
protected TLPResultSet evaluateOriginal(G globalState) throws SQLException {
return new TLPResultSet(
ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors, globalState), null);
}
@Override
protected TLPResultSet evaluateTransformed(G globalState) throws SQLException {
List<String> combinedString = new ArrayList<>();
List<String> secondResultSet = ComparatorHelper.getCombinedResultSet(firstQueryString, secondQueryString,
thirdQueryString, combinedString, !orderBy, globalState, errors);
return new TLPResultSet(secondResultSet, combinedString);
}
@Override
protected boolean sidesDiffer(TLPResultSet original, TLPResultSet transformed, G globalState) {
try {
ComparatorHelper.assumeResultSetsAreEqual(original.resultSet, transformed.resultSet,
originalQueryString, transformed.combinedString, globalState);
} catch (AssertionError resultSetMismatch) {
return true;
}
return false;
}
@Override
protected String mismatchHeaderLine() {
return "-- On the database set up by the statements above, the result sets of the following"
+ " queries mismatch:";
}
@Override
protected void appendQueryLines(StringBuilder sb) {
renderQueryLines(sb, originalQueryString, firstQueryString, secondQueryString, thirdQueryString, orderBy);
}
}
// Renders the failing queries as commented lines, shared by the mismatch and the unexpected-error reproducers. The
// partition queries are null when the error struck the original query before they were built.
private static void renderQueryLines(StringBuilder sb, String originalQueryString, String firstQueryString,
String secondQueryString, String thirdQueryString, boolean orderBy) {
sb.append("-- ").append(originalQueryString).append(';').append(System.lineSeparator());
if (firstQueryString != null) {
if (orderBy) {
sb.append("-- ").append(firstQueryString).append(';').append(System.lineSeparator());
sb.append("-- ").append(secondQueryString).append(';').append(System.lineSeparator());
sb.append("-- ").append(thirdQueryString).append(';').append(System.lineSeparator());
} else {
sb.append("-- ").append(firstQueryString).append(" UNION ALL ").append(secondQueryString)
.append(" UNION ALL ").append(thirdQueryString).append(';').append(System.lineSeparator());
}
}
}
// Builds the reproducer for an unexpected DBMS error, which re-runs the query (or the whole-table query plus the
// three partition queries) and checks the same error still fires. The partition queries are null when the error
// struck the original query before they were built.
private UnexpectedErrorReproducer<G> errorReproducer(String originalQueryString, String firstQueryString,
String secondQueryString, String thirdQueryString, boolean orderBy, String expectedErrorMessage) {
UnexpectedErrorReproducer.Execution<G> execution = globalState -> {
ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors, globalState);
if (firstQueryString != null) {
ComparatorHelper.getCombinedResultSet(firstQueryString, secondQueryString, thirdQueryString,
new ArrayList<>(), !orderBy, globalState, errors);
}
};
StringBuilder sb = new StringBuilder();
renderQueryLines(sb, originalQueryString, firstQueryString, secondQueryString, thirdQueryString, orderBy);
return new UnexpectedErrorReproducer<>(execution, expectedErrorMessage, sb.toString());
}
public TLPWhereOracle(G state, TLPWhereGenerator<Z, J, E, T, C> gen, ExpectedErrors expectedErrors) {
if (state == null || gen == null || expectedErrors == null) {
throw new IllegalArgumentException("Null variables used to initialize test oracle.");
}
this.state = state;
this.gen = gen;
this.errors = expectedErrors;
}
@Override
public void check() throws SQLException {
reproducer = null;
S s = state.getSchema();
AbstractTables<T, C> targetTables = TestOracleUtils.getRandomTableNonEmptyTables(s);
gen = gen.setTablesAndColumns(targetTables);
Select<J, E, T, C> select = gen.generateSelect();
boolean shouldCreateDummy = true;
select.setFetchColumns(gen.generateFetchColumns(shouldCreateDummy));
select.setJoinClauses(gen.getRandomJoinClauses());
select.setFromList(gen.getTableRefs());
select.setWhereClause(null);
String originalQueryString = select.asString();
generatedQueryString = originalQueryString;
List<String> firstResultSet;
try {
firstResultSet = ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors, state);
} catch (AssertionError unexpectedError) {
reproducer = errorReproducer(originalQueryString, null, null, null, false,
TestOracleUtils.getUnexpectedErrorMessage(unexpectedError));
throw unexpectedError;
}
boolean orderBy = Randomly.getBooleanWithSmallProbability();
if (orderBy) {
select.setOrderByClauses(gen.generateOrderBys());
}
TestOracleUtils.PredicateVariants<E, C> predicates = TestOracleUtils.initializeTernaryPredicateVariants(gen,
gen.generateBooleanExpression());
select.setWhereClause(predicates.predicate);
String firstQueryString = select.asString();
select.setWhereClause(predicates.negatedPredicate);
String secondQueryString = select.asString();
select.setWhereClause(predicates.isNullPredicate);
String thirdQueryString = select.asString();
List<String> combinedString = new ArrayList<>();
List<String> secondResultSet;
try {
secondResultSet = ComparatorHelper.getCombinedResultSet(firstQueryString, secondQueryString,
thirdQueryString, combinedString, !orderBy, state, errors);
} catch (AssertionError unexpectedError) {
reproducer = errorReproducer(originalQueryString, firstQueryString, secondQueryString, thirdQueryString,
orderBy, TestOracleUtils.getUnexpectedErrorMessage(unexpectedError));
throw unexpectedError;
}
reproducer = new TLPWhereReproducer(firstQueryString, secondQueryString, thirdQueryString, originalQueryString,
orderBy);
ComparatorHelper.assumeResultSetsAreEqual(firstResultSet, secondResultSet, originalQueryString, combinedString,
state);
}
@Override
public Reproducer<G> getLastReproducer() {
return reproducer;
}
@Override
public String getLastQueryString() {
return generatedQueryString;
}
}