Skip to content

Commit 0315e7a

Browse files
committed
Pass a global state object to Query for extensibility
1 parent a36a3c5 commit 0315e7a

42 files changed

Lines changed: 116 additions & 182 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/sqlancer/ComparatorHelper.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sqlancer;
22

33
import java.io.IOException;
4-
import java.sql.Connection;
54
import java.sql.ResultSet;
65
import java.sql.SQLException;
76
import java.util.ArrayList;
@@ -33,7 +32,7 @@ static boolean equals(double a, double b) {
3332
return Math.abs(a - b) < 0.0001 * Math.max(Math.abs(a), Math.abs(b));
3433
}
3534

36-
public static List<String> getResultSetFirstColumnAsString(String queryString, Set<String> errors, Connection con,
35+
public static List<String> getResultSetFirstColumnAsString(String queryString, Set<String> errors,
3736
GlobalState<?> state) throws SQLException {
3837
if (state.getOptions().logEachSelect()) {
3938
// TODO: refactor me
@@ -49,7 +48,7 @@ public static List<String> getResultSetFirstColumnAsString(String queryString, S
4948
List<String> resultSet = new ArrayList<>();
5049
ResultSet result = null;
5150
try {
52-
result = q.executeAndGet(con);
51+
result = q.executeAndGet(state);
5352
if (result == null) {
5453
throw new IgnoreMeException();
5554
}
@@ -125,15 +124,12 @@ public static List<String> getCombinedResultSet(String firstQueryString, String
125124
String unionString = firstQueryString + " UNION ALL " + secondQueryString + " UNION ALL "
126125
+ thirdQueryString;
127126
combinedString.add(unionString);
128-
secondResultSet = getResultSetFirstColumnAsString(unionString, errors, state.getConnection(), state);
127+
secondResultSet = getResultSetFirstColumnAsString(unionString, errors, state);
129128
} else {
130129
secondResultSet = new ArrayList<>();
131-
secondResultSet
132-
.addAll(getResultSetFirstColumnAsString(firstQueryString, errors, state.getConnection(), state));
133-
secondResultSet
134-
.addAll(getResultSetFirstColumnAsString(secondQueryString, errors, state.getConnection(), state));
135-
secondResultSet
136-
.addAll(getResultSetFirstColumnAsString(thirdQueryString, errors, state.getConnection(), state));
130+
secondResultSet.addAll(getResultSetFirstColumnAsString(firstQueryString, errors, state));
131+
secondResultSet.addAll(getResultSetFirstColumnAsString(secondQueryString, errors, state));
132+
secondResultSet.addAll(getResultSetFirstColumnAsString(thirdQueryString, errors, state));
137133
combinedString.add(firstQueryString);
138134
combinedString.add(secondQueryString);
139135
combinedString.add(thirdQueryString);
@@ -153,7 +149,7 @@ public static List<String> getCombinedResultSetNoDuplicates(String firstQueryStr
153149
}
154150
List<String> secondResultSet;
155151
combinedString.add(unionString);
156-
secondResultSet = getResultSetFirstColumnAsString(unionString, errors, state.getConnection(), state);
152+
secondResultSet = getResultSetFirstColumnAsString(unionString, errors, state);
157153
return secondResultSet;
158154
}
159155

src/sqlancer/Main.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,15 @@ private void printState(FileWriter writer, StateToReproduce state) {
233233

234234
public static class QueryManager {
235235

236-
private final Connection con;
237-
private final StateToReproduce stateToRepro;
238-
239-
QueryManager(Connection con, StateToReproduce state) {
240-
if (con == null || state == null) {
241-
throw new IllegalArgumentException();
242-
}
243-
this.con = con;
244-
this.stateToRepro = state;
236+
private final GlobalState<?> globalState;
245237

238+
QueryManager(GlobalState<?> globalState) {
239+
this.globalState = globalState;
246240
}
247241

248242
public boolean execute(Query q) throws SQLException {
249-
stateToRepro.statements.add(q);
250-
boolean success = q.execute(con);
243+
globalState.getState().statements.add(q);
244+
boolean success = q.execute(globalState);
251245
Main.nrSuccessfulActions.addAndGet(1);
252246
return success;
253247
}
@@ -315,7 +309,7 @@ public void run() throws SQLException {
315309
state.setMainOptions(options);
316310
state.setDmbsSpecificOptions(command);
317311
try (Connection con = provider.createDatabase(state)) {
318-
QueryManager manager = new QueryManager(con, stateToRepro);
312+
QueryManager manager = new QueryManager(state);
319313
try {
320314
java.sql.DatabaseMetaData meta = con.getMetaData();
321315
stateToRepro.databaseVersion = meta.getDatabaseProductVersion();

src/sqlancer/Query.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package sqlancer;
22

3-
import java.sql.Connection;
43
import java.sql.ResultSet;
54
import java.sql.SQLException;
65
import java.util.Collection;
@@ -24,7 +23,7 @@ public abstract class Query {
2423
*
2524
* @throws SQLException
2625
*/
27-
public abstract boolean execute(Connection con) throws SQLException;
26+
public abstract boolean execute(GlobalState<?> globalState) throws SQLException;
2827

2928
public abstract Collection<String> getExpectedErrors();
3029

@@ -33,18 +32,18 @@ public String toString() {
3332
return getQueryString();
3433
}
3534

36-
public ResultSet executeAndGet(Connection con) throws SQLException {
35+
public ResultSet executeAndGet(GlobalState<?> globalState) throws SQLException {
3736
throw new AssertionError();
3837
}
3938

4039
public boolean executeLogged(GlobalState<?> globalState) throws SQLException {
4140
logQueryString(globalState);
42-
return execute(globalState.getConnection());
41+
return execute(globalState);
4342
}
4443

4544
public ResultSet executeAndGetLogged(GlobalState<?> globalState) throws SQLException {
4645
logQueryString(globalState);
47-
return executeAndGet(globalState.getConnection());
46+
return executeAndGet(globalState);
4847
}
4948

5049
private void logQueryString(GlobalState<?> globalState) {

src/sqlancer/QueryAdapter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package sqlancer;
22

3-
import java.sql.Connection;
43
import java.sql.ResultSet;
54
import java.sql.SQLException;
65
import java.sql.Statement;
@@ -39,8 +38,8 @@ public String getQueryString() {
3938
}
4039

4140
@Override
42-
public boolean execute(Connection con) throws SQLException {
43-
try (Statement s = con.createStatement()) {
41+
public boolean execute(GlobalState<?> globalState) throws SQLException {
42+
try (Statement s = globalState.getConnection().createStatement()) {
4443
s.execute(query);
4544
Main.nrSuccessfulActions.addAndGet(1);
4645
return true;
@@ -65,8 +64,8 @@ public void checkException(Exception e) throws AssertionError {
6564
}
6665

6766
@Override
68-
public ResultSet executeAndGet(Connection con) throws SQLException {
69-
Statement s = con.createStatement();
67+
public ResultSet executeAndGet(GlobalState<?> globalState) throws SQLException {
68+
Statement s = globalState.getConnection().createStatement();
7069
ResultSet result = null;
7170
try {
7271
result = s.executeQuery(query);

src/sqlancer/QueryResultCheckAdapter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package sqlancer;
22

3-
import java.sql.Connection;
43
import java.sql.ResultSet;
54
import java.sql.SQLException;
65
import java.sql.Statement;
@@ -16,8 +15,8 @@ public QueryResultCheckAdapter(String query, Consumer<ResultSet> rsChecker) {
1615
}
1716

1817
@Override
19-
public boolean execute(Connection con) throws SQLException {
20-
try (Statement s = con.createStatement()) {
18+
public boolean execute(GlobalState<?> globalState) throws SQLException {
19+
try (Statement s = globalState.getConnection().createStatement()) {
2120
ResultSet rs = s.executeQuery(getQueryString());
2221
rsChecker.accept(rs);
2322
return true;

src/sqlancer/clickhouse/oracle/tlp/ClickHouseTLPGroupByOracle.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public void check() throws SQLException {
2525
select.setWhereClause(null);
2626
String originalQueryString = ClickHouseVisitor.asString(select);
2727

28-
List<String> resultSet = ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors,
29-
state.getConnection(), state);
28+
List<String> resultSet = ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors, state);
3029

3130
select.setWhereClause(predicate);
3231
String firstQueryString = ClickHouseVisitor.asString(select);

src/sqlancer/clickhouse/oracle/tlp/ClickHouseTLPHavingOracle.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public void check() throws SQLException {
4949
select.setHavingClause(null);
5050
String originalQueryString = ClickHouseVisitor.asString(select);
5151

52-
List<String> resultSet = ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors,
53-
state.getConnection(), state);
52+
List<String> resultSet = ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors, state);
5453

5554
ClickHouseExpression predicate = aggrGen.getHavingClause();
5655
select.setHavingClause(predicate);
@@ -62,8 +61,7 @@ public void check() throws SQLException {
6261
ClickHouseUnaryPostfixOperation.ClickHouseUnaryPostfixOperator.IS_NULL, false));
6362
String thirdQueryString = ClickHouseVisitor.asString(select);
6463
String combinedString = firstQueryString + " UNION ALL " + secondQueryString + " UNION ALL " + thirdQueryString;
65-
List<String> secondResultSet = ComparatorHelper.getResultSetFirstColumnAsString(combinedString, errors,
66-
state.getConnection(), state);
64+
List<String> secondResultSet = ComparatorHelper.getResultSetFirstColumnAsString(combinedString, errors, state);
6765
if (state.getOptions().logEachSelect()) {
6866
state.getLogger().writeCurrent(originalQueryString);
6967
state.getLogger().writeCurrent(combinedString);

src/sqlancer/clickhouse/oracle/tlp/ClickHouseTLPWhereOracle.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public void check() throws SQLException {
2222
select.setOrderByExpressions(gen.generateOrderBys());
2323
}
2424
String originalQueryString = ClickHouseVisitor.asString(select);
25-
List<String> resultSet = ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors,
26-
state.getConnection(), state);
25+
List<String> resultSet = ComparatorHelper.getResultSetFirstColumnAsString(originalQueryString, errors, state);
2726

2827
boolean orderBy = Randomly.getBooleanWithRatherLowProbability();
2928
if (orderBy) {

src/sqlancer/cockroachdb/oracle/CockroachDBNoRECOracle.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package sqlancer.cockroachdb.oracle;
22

3-
import java.sql.Connection;
43
import java.sql.ResultSet;
54
import java.sql.SQLException;
65
import java.util.ArrayList;
@@ -10,6 +9,7 @@
109
import java.util.Set;
1110
import java.util.stream.Collectors;
1211

12+
import sqlancer.GlobalState;
1313
import sqlancer.IgnoreMeException;
1414
import sqlancer.Query;
1515
import sqlancer.QueryAdapter;
@@ -56,13 +56,11 @@ public void check() throws SQLException {
5656
gen = new CockroachDBExpressionGenerator(globalState).setColumns(tables.getColumns());
5757
List<CockroachDBExpression> joinExpressions = getJoins(tableList, globalState);
5858
CockroachDBExpression whereCondition = gen.generateExpression(CockroachDBDataType.BOOL.get());
59-
int optimizableCount = getOptimizedResult(globalState.getConnection(), whereCondition, tableList, errors,
60-
joinExpressions);
59+
int optimizableCount = getOptimizedResult(whereCondition, tableList, errors, joinExpressions);
6160
if (optimizableCount == -1) {
6261
throw new IgnoreMeException();
6362
}
64-
int nonOptimizableCount = getNonOptimizedResult(globalState.getConnection(), whereCondition, tableList, errors,
65-
joinExpressions);
63+
int nonOptimizableCount = getNonOptimizedResult(whereCondition, tableList, errors, joinExpressions);
6664
if (nonOptimizableCount == -1) {
6765
throw new IgnoreMeException();
6866
}
@@ -104,9 +102,8 @@ public static List<CockroachDBExpression> getJoins(List<CockroachDBExpression> t
104102
return joinExpressions;
105103
}
106104

107-
private int getOptimizedResult(Connection con, CockroachDBExpression whereCondition,
108-
List<CockroachDBExpression> tableList, Set<String> errors, List<CockroachDBExpression> joinExpressions)
109-
throws SQLException {
105+
private int getOptimizedResult(CockroachDBExpression whereCondition, List<CockroachDBExpression> tableList,
106+
Set<String> errors, List<CockroachDBExpression> joinExpressions) throws SQLException {
110107
CockroachDBSelect select = new CockroachDBSelect();
111108
CockroachDBColumn c = new CockroachDBColumn("COUNT(*)", null, false, false);
112109
select.setFetchColumns(Arrays.asList(new CockroachDBColumnReference(c)));
@@ -122,12 +119,11 @@ private int getOptimizedResult(Connection con, CockroachDBExpression whereCondit
122119
}
123120
this.optimizableQueryString = s;
124121
Query q = new QueryAdapter(s, errors);
125-
return getCount(con, q);
122+
return getCount(globalState, q);
126123
}
127124

128-
private int getNonOptimizedResult(Connection con, CockroachDBExpression whereCondition,
129-
List<CockroachDBExpression> tableList, Set<String> errors, List<CockroachDBExpression> joinList)
130-
throws SQLException {
125+
private int getNonOptimizedResult(CockroachDBExpression whereCondition, List<CockroachDBExpression> tableList,
126+
Set<String> errors, List<CockroachDBExpression> joinList) throws SQLException {
131127
String fromString = tableList.stream().map(t -> ((CockroachDBTableReference) t).getTable().getName())
132128
.collect(Collectors.joining(", "));
133129
if (!tableList.isEmpty() && !joinList.isEmpty()) {
@@ -141,12 +137,12 @@ private int getNonOptimizedResult(Connection con, CockroachDBExpression whereCon
141137
}
142138
this.unoptimizedQuery = s;
143139
Query q = new QueryAdapter(s, errors);
144-
return getCount(con, q);
140+
return getCount(globalState, q);
145141
}
146142

147-
private int getCount(Connection con, Query q) throws AssertionError {
143+
private int getCount(GlobalState<?> globalState, Query q) throws AssertionError {
148144
int count = 0;
149-
try (ResultSet rs = q.executeAndGet(con)) {
145+
try (ResultSet rs = q.executeAndGet(globalState)) {
150146
if (rs == null) {
151147
return -1;
152148
}

src/sqlancer/cockroachdb/oracle/tlp/CockroachDBTLPAggregateOracle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private String createMetamorphicUnionQuery(CockroachDBSelect select, CockroachDB
118118
private String getAggregateResult(String queryString) throws SQLException {
119119
String resultString;
120120
QueryAdapter q = new QueryAdapter(queryString, errors);
121-
try (ResultSet result = q.executeAndGet(state.getConnection())) {
121+
try (ResultSet result = q.executeAndGet(state)) {
122122
if (result == null) {
123123
throw new IgnoreMeException();
124124
}

0 commit comments

Comments
 (0)