forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresSchema.java
More file actions
395 lines (341 loc) · 14.7 KB
/
PostgresSchema.java
File metadata and controls
395 lines (341 loc) · 14.7 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package sqlancer.postgres;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.StateToReproduce.PostgresStateToReproduce;
import sqlancer.postgres.PostgresSchema.PostgresTable.TableType;
import sqlancer.postgres.ast.PostgresConstant;
import sqlancer.schema.AbstractTable;
import sqlancer.schema.AbstractTableColumn;
import sqlancer.schema.AbstractTables;
import sqlancer.schema.TableIndex;
public class PostgresSchema {
private final List<PostgresTable> databaseTables;
private String databaseName;
public enum PostgresDataType {
INT, BOOLEAN, TEXT, DECIMAL, FLOAT, REAL, RANGE, MONEY, BIT, INET;
public static PostgresDataType getRandomType() {
List<PostgresDataType> dataTypes = Arrays.asList(values());
if (PostgresProvider.generateOnlyKnown) {
dataTypes.remove(PostgresDataType.DECIMAL);
dataTypes.remove(PostgresDataType.FLOAT);
dataTypes.remove(PostgresDataType.REAL);
dataTypes.remove(PostgresDataType.INET);
}
return Randomly.fromList(dataTypes);
}
}
public static class PostgresColumn extends AbstractTableColumn<PostgresTable, PostgresDataType> {
public PostgresColumn(String name, PostgresDataType columnType) {
super(name, null, columnType);
}
}
public static class PostgresTables extends AbstractTables<PostgresTable, PostgresColumn> {
public PostgresTables(List<PostgresTable> tables) {
super(tables);
}
public PostgresRowValue getRandomRowValue(Connection con, PostgresStateToReproduce state) throws SQLException {
String randomRow = String.format("SELECT %s FROM %s ORDER BY RANDOM() LIMIT 1", columnNamesAsString(
c -> c.getTable().getName() + "." + c.getName() + " AS " + c.getTable().getName() + c.getName()),
// columnNamesAsString(c -> "typeof(" + c.getTable().getName() + "." +
// c.getName() + ")")
tableNamesAsString());
Map<PostgresColumn, PostgresConstant> values = new HashMap<>();
try (Statement s = con.createStatement()) {
ResultSet randomRowValues = s.executeQuery(randomRow);
if (!randomRowValues.next()) {
throw new AssertionError("could not find random row! " + randomRow + "\n" + state);
}
for (int i = 0; i < getColumns().size(); i++) {
PostgresColumn column = getColumns().get(i);
int columnIndex = randomRowValues.findColumn(column.getTable().getName() + column.getName());
assert columnIndex == i + 1;
PostgresConstant constant;
if (randomRowValues.getString(columnIndex) == null) {
constant = PostgresConstant.createNullConstant();
} else {
switch (column.getType()) {
case INT:
constant = PostgresConstant.createIntConstant(randomRowValues.getLong(columnIndex));
break;
case BOOLEAN:
constant = PostgresConstant.createBooleanConstant(randomRowValues.getBoolean(columnIndex));
break;
case TEXT:
constant = PostgresConstant.createTextConstant(randomRowValues.getString(columnIndex));
break;
default:
throw new AssertionError(column.getType());
}
}
values.put(column, constant);
}
assert (!randomRowValues.next());
state.randomRowValues = values;
return new PostgresRowValue(this, values);
}
}
}
private static PostgresDataType getColumnType(String typeString) {
switch (typeString) {
case "smallint":
case "integer":
case "bigint":
return PostgresDataType.INT;
case "boolean":
return PostgresDataType.BOOLEAN;
case "text":
case "character":
case "character varying":
case "name":
return PostgresDataType.TEXT;
case "numeric":
return PostgresDataType.DECIMAL;
case "double precision":
return PostgresDataType.FLOAT;
case "real":
return PostgresDataType.REAL;
case "int4range":
return PostgresDataType.RANGE;
case "money":
return PostgresDataType.MONEY;
case "bit":
case "bit varying":
return PostgresDataType.BIT;
case "inet":
return PostgresDataType.INET;
default:
throw new AssertionError(typeString);
}
}
public static class PostgresRowValue {
private final PostgresTables tables;
private final Map<PostgresColumn, PostgresConstant> values;
PostgresRowValue(PostgresTables tables, Map<PostgresColumn, PostgresConstant> values) {
this.tables = tables;
this.values = values;
}
public PostgresTables getTable() {
return tables;
}
public Map<PostgresColumn, PostgresConstant> getValues() {
return values;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
int i = 0;
for (PostgresColumn c : tables.getColumns()) {
if (i++ != 0) {
sb.append(", ");
}
sb.append(values.get(c));
}
return sb.toString();
}
public String getRowValuesAsString() {
List<PostgresColumn> columnsToCheck = tables.getColumns();
return getRowValuesAsString(columnsToCheck);
}
public String getRowValuesAsString(List<PostgresColumn> columnsToCheck) {
StringBuilder sb = new StringBuilder();
Map<PostgresColumn, PostgresConstant> expectedValues = getValues();
for (int i = 0; i < columnsToCheck.size(); i++) {
if (i != 0) {
sb.append(", ");
}
PostgresConstant expectedColumnValue = expectedValues.get(columnsToCheck.get(i));
PostgresToStringVisitor visitor = new PostgresToStringVisitor();
visitor.visit(expectedColumnValue);
sb.append(visitor.get());
}
return sb.toString();
}
}
public static class PostgresTable extends AbstractTable<PostgresColumn, PostgresIndex> {
public enum TableType {
STANDARD, TEMPORARY
}
private final TableType tableType;
private List<PostgresStatisticsObject> statistics;
private boolean isInsertable;
public PostgresTable(String tableName, List<PostgresColumn> columns, List<PostgresIndex> indexes,
TableType tableType, List<PostgresStatisticsObject> statistics, boolean isView, boolean isInsertable) {
super(tableName, columns, indexes, isView);
this.statistics = statistics;
this.isInsertable = isInsertable;
this.tableType = tableType;
}
public List<PostgresStatisticsObject> getStatistics() {
return statistics;
}
public TableType getTableType() {
return tableType;
}
public boolean isInsertable() {
return isInsertable;
}
}
public static final class PostgresStatisticsObject {
private final String name;
public PostgresStatisticsObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static final class PostgresIndex extends TableIndex {
private PostgresIndex(String indexName) {
super(indexName);
}
public static PostgresIndex create(String indexName) {
return new PostgresIndex(indexName);
}
@Override
public String getIndexName() {
if (super.getIndexName().contentEquals("PRIMARY")) {
return "`PRIMARY`";
} else {
return super.getIndexName();
}
}
}
public static PostgresSchema fromConnection(Connection con, String databaseName) throws SQLException {
Exception ex = null;
try {
List<PostgresTable> databaseTables = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery(
"SELECT table_name, table_schema, table_type, is_insertable_into FROM information_schema.tables WHERE table_schema='public' OR table_schema LIKE 'pg_temp_%';")) {
while (rs.next()) {
String tableName = rs.getString("table_name");
String tableTypeSchema = rs.getString("table_schema");
boolean isInsertable = rs.getBoolean("is_insertable_into");
// TODO: also check insertable
// TODO: insert into view?
boolean isView = tableName.startsWith("v"); // tableTypeStr.contains("VIEW") ||
// tableTypeStr.contains("LOCAL TEMPORARY") &&
// !isInsertable;
PostgresTable.TableType tableType = getTableType(tableTypeSchema);
List<PostgresColumn> databaseColumns = getTableColumns(con, tableName);
List<PostgresIndex> indexes = getIndexes(con, tableName);
List<PostgresStatisticsObject> statistics = getStatistics(con);
PostgresTable t = new PostgresTable(tableName, databaseColumns, indexes, tableType, statistics,
isView, isInsertable);
for (PostgresColumn c : databaseColumns) {
c.setTable(t);
}
databaseTables.add(t);
}
}
}
return new PostgresSchema(databaseTables, databaseName);
} catch (SQLIntegrityConstraintViolationException e) {
ex = e;
}
throw new AssertionError(ex);
}
private static List<PostgresStatisticsObject> getStatistics(Connection con) throws SQLException {
List<PostgresStatisticsObject> statistics = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT stxname FROM pg_statistic_ext;")) {
while (rs.next()) {
statistics.add(new PostgresStatisticsObject(rs.getString("stxname")));
}
}
}
return statistics;
}
private static PostgresTable.TableType getTableType(String tableTypeStr) throws AssertionError {
PostgresTable.TableType tableType;
if (tableTypeStr.contentEquals("public")) {
tableType = TableType.STANDARD;
} else if (tableTypeStr.startsWith("pg_temp")) {
tableType = TableType.TEMPORARY;
} else {
throw new AssertionError(tableTypeStr);
}
return tableType;
}
private static List<PostgresIndex> getIndexes(Connection con, String tableName) throws SQLException {
List<PostgresIndex> indexes = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s
.executeQuery(String.format("SELECT indexname FROM pg_indexes WHERE tablename='%s';", tableName))) {
while (rs.next()) {
String indexName = rs.getString("indexname");
if (indexName.length() != 2) {
// FIXME: implement cleanly
continue; // skip internal indexes
}
indexes.add(PostgresIndex.create(indexName));
}
}
}
return indexes;
}
private static List<PostgresColumn> getTableColumns(Connection con, String tableName) throws SQLException {
List<PostgresColumn> columns = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s
.executeQuery("select column_name, data_type from INFORMATION_SCHEMA.COLUMNS where table_name = '"
+ tableName + "'")) {
while (rs.next()) {
String columnName = rs.getString("column_name");
String dataType = rs.getString("data_type");
PostgresColumn c = new PostgresColumn(columnName, getColumnType(dataType));
columns.add(c);
}
}
}
return columns;
}
public PostgresSchema(List<PostgresTable> databaseTables, String databaseName) {
this.databaseTables = Collections.unmodifiableList(databaseTables);
this.databaseName = databaseName;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (PostgresTable t : getDatabaseTables()) {
sb.append(t + "\n");
}
return sb.toString();
}
public PostgresTable getRandomTable() {
return Randomly.fromList(getDatabaseTables());
}
public PostgresTables getRandomTableNonEmptyTables() {
return new PostgresTables(Randomly.nonEmptySubset(databaseTables));
}
public List<PostgresTable> getDatabaseTables() {
return databaseTables;
}
public List<PostgresTable> getDatabaseTablesRandomSubsetNotEmpty() {
return Randomly.nonEmptySubset(databaseTables);
}
public String getDatabaseName() {
return databaseName;
}
public PostgresTable getRandomTable(Function<PostgresTable, Boolean> f) {
List<PostgresTable> relevantTables = databaseTables.stream().filter(t -> f.apply(t))
.collect(Collectors.toList());
if (relevantTables.isEmpty()) {
throw new IgnoreMeException();
}
return Randomly.fromList(relevantTables);
}
}