-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresSchema.java
More file actions
327 lines (282 loc) · 12.8 KB
/
PostgresSchema.java
File metadata and controls
327 lines (282 loc) · 12.8 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
package sqlancer.postgres;
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.HashMap;
import java.util.List;
import java.util.Map;
import org.postgresql.util.PSQLException;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.common.DBMSCommon;
import sqlancer.common.schema.AbstractRelationalTable;
import sqlancer.common.schema.AbstractRowValue;
import sqlancer.common.schema.AbstractSchema;
import sqlancer.common.schema.AbstractTableColumn;
import sqlancer.common.schema.AbstractTables;
import sqlancer.common.schema.TableIndex;
import sqlancer.postgres.PostgresSchema.PostgresTable;
import sqlancer.postgres.PostgresSchema.PostgresTable.TableType;
import sqlancer.postgres.ast.PostgresConstant;
public class PostgresSchema extends AbstractSchema<PostgresGlobalState, PostgresTable> {
private final String databaseName;
public enum PostgresDataType {
INT, BOOLEAN, TEXT, DECIMAL, FLOAT, REAL, RANGE, MONEY, BIT, INET;
public static PostgresDataType getRandomType() {
List<PostgresDataType> dataTypes = new ArrayList<>(Arrays.asList(values()));
if (PostgresProvider.generateOnlyKnown) {
dataTypes.remove(PostgresDataType.DECIMAL);
dataTypes.remove(PostgresDataType.FLOAT);
dataTypes.remove(PostgresDataType.REAL);
dataTypes.remove(PostgresDataType.INET);
dataTypes.remove(PostgresDataType.RANGE);
dataTypes.remove(PostgresDataType.MONEY);
dataTypes.remove(PostgresDataType.BIT);
}
return Randomly.fromList(dataTypes);
}
}
public static class PostgresColumn extends AbstractTableColumn<PostgresTable, PostgresDataType> {
public PostgresColumn(String name, PostgresDataType columnType) {
super(name, null, columnType);
}
public static PostgresColumn createDummy(String name) {
return new PostgresColumn(name, PostgresDataType.INT);
}
}
public static class PostgresTables extends AbstractTables<PostgresTable, PostgresColumn> {
public PostgresTables(List<PostgresTable> tables) {
super(tables);
}
public PostgresRowValue getRandomRowValue(SQLConnection con) 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");
}
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 IgnoreMeException();
}
}
values.put(column, constant);
}
assert !randomRowValues.next();
return new PostgresRowValue(this, values);
} catch (PSQLException e) {
throw new IgnoreMeException();
}
}
}
public 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":
case "regclass":
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 extends AbstractRowValue<PostgresTables, PostgresColumn, PostgresConstant> {
protected PostgresRowValue(PostgresTables tables, Map<PostgresColumn, PostgresConstant> values) {
super(tables, values);
}
}
public static class PostgresTable
extends AbstractRelationalTable<PostgresColumn, PostgresIndex, PostgresGlobalState> {
public enum TableType {
STANDARD, TEMPORARY
}
private final TableType tableType;
private final List<PostgresStatisticsObject> statistics;
private final 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(SQLConnection con, String databaseName) throws SQLException {
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_%' ORDER BY table_name;")) {
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) {
throw new AssertionError(e);
}
}
protected static List<PostgresStatisticsObject> getStatistics(SQLConnection con) throws SQLException {
List<PostgresStatisticsObject> statistics = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT stxname FROM pg_statistic_ext ORDER BY stxname;")) {
while (rs.next()) {
statistics.add(new PostgresStatisticsObject(rs.getString("stxname")));
}
}
}
return statistics;
}
protected 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;
}
protected static List<PostgresIndex> getIndexes(SQLConnection 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' ORDER BY indexname;", tableName))) {
while (rs.next()) {
String indexName = rs.getString("indexname");
if (DBMSCommon.matchesIndexName(indexName)) {
indexes.add(PostgresIndex.create(indexName));
}
}
}
}
return indexes;
}
protected static List<PostgresColumn> getTableColumns(SQLConnection 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 + "' ORDER BY column_name")) {
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) {
super(databaseTables);
this.databaseName = databaseName;
}
public PostgresTables getRandomTableNonEmptyTables() {
return new PostgresTables(Randomly.nonEmptySubset(getDatabaseTables()));
}
public String getDatabaseName() {
return databaseName;
}
}