Skip to content

Commit ff23da5

Browse files
committed
Use options in Postgres
1 parent 1431e65 commit ff23da5

4 files changed

Lines changed: 81 additions & 13 deletions

File tree

src/sqlancer/postgres/PostgresGlobalState.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class PostgresGlobalState extends GlobalState {
1616
private List<String> collates;
1717
private List<String> opClasses;
1818
private PostgresSchema schema;
19+
private PostgresOptions postgresOptions;
1920

2021
@Override
2122
public void setConnection(Connection con) {
@@ -98,4 +99,12 @@ public String getRandomOpclass() {
9899
return Randomly.fromList(opClasses);
99100
}
100101

102+
public void setPostgresOptions(PostgresOptions postgresOptions) {
103+
this.postgresOptions = postgresOptions;
104+
}
105+
106+
public PostgresOptions getPostgresOptions() {
107+
return postgresOptions;
108+
}
109+
101110
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package sqlancer.postgres;
2+
3+
import java.sql.SQLException;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import com.beust.jcommander.Parameter;
9+
10+
import sqlancer.CompositeTestOracle;
11+
import sqlancer.MainOptions.DBMSConverter;
12+
import sqlancer.TestOracle;
13+
import sqlancer.postgres.test.PostgresNoRECOracle;
14+
import sqlancer.postgres.test.PostgresPivotedQuerySynthesisGenerator;
15+
16+
public class PostgresOptions {
17+
18+
@Parameter(names = "--oracle", converter = DBMSConverter.class)
19+
public List<PostgresOracle> oracle = Arrays.asList(PostgresOracle.QUERY_PARTITIONING);
20+
21+
public static enum PostgresOracle {
22+
NOREC {
23+
@Override
24+
public TestOracle create(PostgresGlobalState globalState) throws SQLException {
25+
return new PostgresNoRECOracle(globalState);
26+
}
27+
},
28+
PQS {
29+
@Override
30+
public TestOracle create(PostgresGlobalState globalState) throws SQLException {
31+
return new PostgresPivotedQuerySynthesisGenerator(globalState);
32+
}
33+
},
34+
QUERY_PARTITIONING {
35+
@Override
36+
public TestOracle create(PostgresGlobalState globalState) throws SQLException {
37+
List<TestOracle> oracles = new ArrayList<>();
38+
return new CompositeTestOracle(oracles);
39+
}
40+
};
41+
42+
public abstract TestOracle create(PostgresGlobalState globalState) throws SQLException;
43+
44+
}
45+
46+
}

src/sqlancer/postgres/PostgresProvider.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import java.util.List;
1212
import java.util.stream.Collectors;
1313

14+
import com.beust.jcommander.JCommander;
15+
1416
import sqlancer.AbstractAction;
17+
import sqlancer.CompositeTestOracle;
1518
import sqlancer.DatabaseProvider;
1619
import sqlancer.IgnoreMeException;
1720
import sqlancer.Main.QueryManager;
@@ -23,6 +26,7 @@
2326
import sqlancer.StateToReproduce;
2427
import sqlancer.StateToReproduce.PostgresStateToReproduce;
2528
import sqlancer.StatementExecutor;
29+
import sqlancer.TestOracle;
2630
import sqlancer.postgres.PostgresSchema.PostgresColumn;
2731
import sqlancer.postgres.PostgresSchema.PostgresTable;
2832
import sqlancer.postgres.ast.PostgresExpression;
@@ -47,7 +51,6 @@
4751
import sqlancer.postgres.gen.PostgresUpdateGenerator;
4852
import sqlancer.postgres.gen.PostgresVacuumGenerator;
4953
import sqlancer.postgres.gen.PostgresViewGenerator;
50-
import sqlancer.postgres.test.PostgresNoRECOracle;
5154
import sqlancer.sqlite3.gen.SQLite3Common;
5255

5356
// EXISTS
@@ -201,6 +204,9 @@ public void generateAndTestDatabase(PostgresGlobalState globalState) throws SQLE
201204
String databaseName = globalState.getDatabaseName();
202205
Connection con = globalState.getConnection();
203206
QueryManager manager = globalState.getManager();
207+
PostgresOptions PostgresOptions = new PostgresOptions();
208+
JCommander.newBuilder().addObject(PostgresOptions).build().parse(globalState.getOptions().getDbmsOptions().split(" "));
209+
globalState.setPostgresOptions(PostgresOptions);
204210
if (options.logEachSelect()) {
205211
logger.writeCurrent(state);
206212
}
@@ -245,11 +251,20 @@ public void generateAndTestDatabase(PostgresGlobalState globalState) throws SQLE
245251
globalState.setSchema(PostgresSchema.fromConnection(con, databaseName));
246252

247253
manager.execute(new QueryAdapter("SET SESSION statement_timeout = 5000;\n"));
248-
PostgresNoRECOracle or = new PostgresNoRECOracle(globalState.getSchema(), globalState.getRandomly(), con,
249-
(PostgresStateToReproduce) state, logger, options, manager, globalState);
254+
255+
256+
List<TestOracle> oracles = globalState.getPostgresOptions().oracle.stream().map(o -> {
257+
try {
258+
return o.create(globalState);
259+
} catch (SQLException e1) {
260+
throw new AssertionError(e1);
261+
}
262+
}).collect(Collectors.toList());
263+
CompositeTestOracle oracle = new CompositeTestOracle(oracles);
264+
250265
for (int i = 0; i < options.getNrQueries(); i++) {
251266
try {
252-
or.check();
267+
oracle.check();
253268
} catch (IgnoreMeException e) {
254269
continue;
255270
}

src/sqlancer/postgres/test/PostgresNoRECOracle.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.stream.Collectors;
1212

1313
import sqlancer.IgnoreMeException;
14-
import sqlancer.Main.QueryManager;
1514
import sqlancer.Main.StateLogger;
1615
import sqlancer.MainOptions;
1716
import sqlancer.Query;
@@ -52,14 +51,13 @@ public class PostgresNoRECOracle implements TestOracle {
5251
private final List<String> errors = new ArrayList<>();
5352
private PostgresGlobalState globalState;
5453

55-
public PostgresNoRECOracle(PostgresSchema s, Randomly r, Connection con,
56-
PostgresStateToReproduce state, StateLogger logger, MainOptions options, QueryManager manager, PostgresGlobalState globalState) {
57-
this.s = s;
58-
this.r = r;
59-
this.con = con;
60-
this.state = state;
61-
this.logger = logger;
62-
this.options = options;
54+
public PostgresNoRECOracle(PostgresGlobalState globalState) {
55+
this.s = globalState.getSchema();
56+
this.r = globalState.getRandomly();
57+
this.con = globalState.getConnection();
58+
this.state = (PostgresStateToReproduce) globalState.getState();
59+
this.logger = globalState.getLogger();
60+
this.options = globalState.getOptions();
6361
this.globalState = globalState;
6462
}
6563

0 commit comments

Comments
 (0)