forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresNoRECOracle.java
More file actions
212 lines (199 loc) · 9.11 KB
/
PostgresNoRECOracle.java
File metadata and controls
212 lines (199 loc) · 9.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
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
207
208
209
210
211
212
package sqlancer.postgres.test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import sqlancer.IgnoreMeException;
import sqlancer.Main.StateLogger;
import sqlancer.MainOptions;
import sqlancer.Query;
import sqlancer.QueryAdapter;
import sqlancer.Randomly;
import sqlancer.StateToReproduce.PostgresStateToReproduce;
import sqlancer.TestOracle;
import sqlancer.postgres.PostgresCompoundDataType;
import sqlancer.postgres.PostgresGlobalState;
import sqlancer.postgres.PostgresSchema;
import sqlancer.postgres.PostgresSchema.PostgresColumn;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
import sqlancer.postgres.PostgresSchema.PostgresTable;
import sqlancer.postgres.PostgresSchema.PostgresTables;
import sqlancer.postgres.PostgresVisitor;
import sqlancer.postgres.ast.PostgresCastOperation;
import sqlancer.postgres.ast.PostgresColumnValue;
import sqlancer.postgres.ast.PostgresExpression;
import sqlancer.postgres.ast.PostgresJoin;
import sqlancer.postgres.ast.PostgresJoin.PostgresJoinType;
import sqlancer.postgres.ast.PostgresPostfixText;
import sqlancer.postgres.ast.PostgresSelect;
import sqlancer.postgres.ast.PostgresSelect.PostgresFromTable;
import sqlancer.postgres.ast.PostgresSelect.SelectType;
import sqlancer.postgres.gen.PostgresCommon;
import sqlancer.postgres.gen.PostgresExpressionGenerator;
public class PostgresNoRECOracle implements TestOracle {
private PostgresSchema s;
private Connection con;
private PostgresStateToReproduce state;
private String firstQueryString;
private String secondQueryString;
private StateLogger logger;
private MainOptions options;
private final Set<String> errors = new HashSet<>();
private PostgresGlobalState globalState;
public PostgresNoRECOracle(PostgresGlobalState globalState) {
this.s = globalState.getSchema();
this.con = globalState.getConnection();
this.state = (PostgresStateToReproduce) globalState.getState();
this.logger = globalState.getLogger();
this.options = globalState.getOptions();
this.globalState = globalState;
}
@Override
public void check() throws SQLException {
PostgresCommon.addCommonExpressionErrors(errors);
PostgresCommon.addCommonFetchErrors(errors);
PostgresTables randomTables = s.getRandomTableNonEmptyTables();
List<PostgresColumn> columns = randomTables.getColumns();
PostgresExpression randomWhereCondition = getRandomWhereCondition(columns);
List<PostgresTable> tables = randomTables.getTables();
List<PostgresJoin> joinStatements = getJoinStatements(globalState, columns, tables);
List<PostgresExpression> fromTables = tables.stream().map(t -> new PostgresFromTable(t, Randomly.getBoolean()))
.collect(Collectors.toList());
int secondCount = getSecondQuery(fromTables, randomWhereCondition, joinStatements);
int firstCount = getFirstQueryCount(con, fromTables, columns, randomWhereCondition, joinStatements);
if (firstCount == -1 || secondCount == -1) {
throw new IgnoreMeException();
}
if (firstCount != secondCount) {
state.queryString = firstCount + " " + secondCount + " " + firstQueryString + ";\n" + secondQueryString
+ ";";
throw new AssertionError(firstQueryString + secondQueryString + firstCount + " " + secondCount);
}
}
public static List<PostgresJoin> getJoinStatements(PostgresGlobalState globalState, List<PostgresColumn> columns,
List<PostgresTable> tables) {
List<PostgresJoin> joinStatements = new ArrayList<>();
PostgresExpressionGenerator gen = new PostgresExpressionGenerator(globalState).setColumns(columns);
for (int i = 1; i < tables.size(); i++) {
PostgresExpression joinClause = gen.generateExpression(PostgresDataType.BOOLEAN);
PostgresTable table = Randomly.fromList(tables);
tables.remove(table);
PostgresJoinType options = PostgresJoinType.getRandom();
PostgresJoin j = new PostgresJoin(table, joinClause, options);
joinStatements.add(j);
}
return joinStatements;
}
private PostgresExpression getRandomWhereCondition(List<PostgresColumn> columns) {
return new PostgresExpressionGenerator(globalState).setColumns(columns).setGlobalState(globalState)
.generateExpression(PostgresDataType.BOOLEAN);
}
private int getSecondQuery(List<PostgresExpression> fromTables, PostgresExpression randomWhereCondition,
List<PostgresJoin> joinStatements) throws SQLException {
PostgresSelect select = new PostgresSelect();
// select.setGroupByClause(groupBys);
// PostgresExpression isTrue =
// PostgresPostfixOperation.create(randomWhereCondition,
// PostfixOperator.IS_TRUE);
PostgresCastOperation isTrue = new PostgresCastOperation(randomWhereCondition,
PostgresCompoundDataType.create(PostgresDataType.INT));
PostgresPostfixText asText = new PostgresPostfixText(isTrue, " as count", null, PostgresDataType.INT);
select.setFetchColumns(Arrays.asList(asText));
select.setFromList(fromTables);
select.setSelectType(SelectType.ALL);
select.setJoinClauses(joinStatements);
int secondCount = 0;
secondQueryString = "SELECT SUM(count) FROM (" + PostgresVisitor.asString(select) + ") as res";
if (options.logEachSelect()) {
logger.writeCurrent(secondQueryString);
}
errors.add("canceling statement due to statement timeout");
Query q = new QueryAdapter(secondQueryString, errors);
ResultSet rs;
try {
rs = q.executeAndGet(con);
} catch (Exception e) {
throw new AssertionError(secondQueryString, e);
}
if (rs == null) {
return -1;
}
if (rs.next()) {
secondCount += rs.getLong(1);
}
rs.close();
return secondCount;
}
private int getFirstQueryCount(Connection con, List<PostgresExpression> randomTables, List<PostgresColumn> columns,
PostgresExpression randomWhereCondition, List<PostgresJoin> joinStatements) throws SQLException {
PostgresSelect select = new PostgresSelect();
// select.setGroupByClause(groupBys);
// PostgresAggregate aggr = new PostgresAggregate(
PostgresColumnValue allColumns = new PostgresColumnValue(Randomly.fromList(columns), null);
// PostgresAggregateFunction.COUNT);
// select.setFetchColumns(Arrays.asList(aggr));
select.setFetchColumns(Arrays.asList(allColumns));
select.setFromList(randomTables);
select.setWhereClause(randomWhereCondition);
if (Randomly.getBooleanWithSmallProbability()) {
select.setOrderByExpressions(new PostgresExpressionGenerator(globalState).setColumns(columns)
.setGlobalState(globalState).generateOrderBy());
}
select.setSelectType(SelectType.ALL);
select.setJoinClauses(joinStatements);
int firstCount = 0;
try (Statement stat = con.createStatement()) {
firstQueryString = PostgresVisitor.asString(select);
if (options.logEachSelect()) {
logger.writeCurrent(firstQueryString);
}
try (ResultSet rs = stat.executeQuery(firstQueryString)) {
while (rs.next()) {
firstCount++;
}
}
} catch (SQLException e) {
throw new IgnoreMeException();
}
return firstCount;
}
// private int getFirstAlternativeQueryCount(Connection con,
// List<PostgresFromTable> randomTables,
// List<PostgresColumn> columns,
// PostgresExpression randomWhereCondition, List<PostgresExpression> groupBys,
// List<PostgresJoin> joinStatements) throws SQLException {
// PostgresSelect select = new PostgresSelect();
//// select.setGroupByClause(groupBys);
// PostgresColumnValue aggr = new PostgresColumnValue(new PostgresColumn("*",
// PostgresDataType.INT), null);
// select.setFetchColumns(Arrays.asList(aggr));
// select.setFromTables(randomTables);
// select.setWhereClause(randomWhereCondition);
// if (Randomly.getBooleanWithSmallProbability()) {
//// select.setOrderByClause(getRandomExpressions(columns));
// }
// select.setSelectType(SelectType.ALL);
// select.setJoinClauses(joinStatements);
// int firstCount = 0;
// try (Statement stat = con.createStatement()) {
// firstQueryString = PostgresVisitor.asString(select);
//// if (options.logEachSelect()) {
//// logger.writeCurrent(firstQueryString);
//// }
// try (ResultSet rs = stat.executeQuery(firstQueryString)) {
// while (rs.next()) {
// firstCount += 1;
// }
// }
// } catch (SQLException e) {
// throw new IgnoreMeException();
// }
// return firstCount;
// }
}