-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathCockroachDBSchema.java
More file actions
363 lines (312 loc) · 14 KB
/
CockroachDBSchema.java
File metadata and controls
363 lines (312 loc) · 14 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
package sqlancer.cockroachdb;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.cockroachdb.CockroachDBProvider.CockroachDBGlobalState;
import sqlancer.cockroachdb.CockroachDBSchema.CockroachDBTable;
import sqlancer.common.schema.AbstractRelationalTable;
import sqlancer.common.schema.AbstractSchema;
import sqlancer.common.schema.AbstractTableColumn;
import sqlancer.common.schema.AbstractTables;
import sqlancer.common.schema.TableIndex;
public class CockroachDBSchema extends AbstractSchema<CockroachDBGlobalState, CockroachDBTable> {
public enum CockroachDBDataType {
INT, BOOL, STRING, FLOAT, BYTES, BIT, VARBIT, SERIAL, INTERVAL, TIMESTAMP, TIMESTAMPTZ, DECIMAL, JSONB, TIME,
TIMETZ, ARRAY;
public static CockroachDBDataType getRandom() {
return Randomly.fromOptions(values());
}
public CockroachDBCompositeDataType get() {
return CockroachDBCompositeDataType.getRandomForType(this);
}
}
public static class CockroachDBCompositeDataType {
private final CockroachDBDataType dataType;
private final int size;
private CockroachDBCompositeDataType elementType;
public CockroachDBCompositeDataType(CockroachDBDataType dataType) {
this.dataType = dataType;
this.size = -1;
}
public CockroachDBCompositeDataType(CockroachDBDataType dataType, int size) {
this.dataType = dataType;
this.size = size;
}
public CockroachDBCompositeDataType(CockroachDBDataType dataType, CockroachDBCompositeDataType elementType) {
if (dataType != CockroachDBDataType.ARRAY) {
throw new IllegalArgumentException();
}
this.dataType = dataType;
this.size = -1;
this.elementType = elementType;
}
public CockroachDBDataType getPrimitiveDataType() {
return dataType;
}
public int getSize() {
if (size == -1) {
throw new AssertionError(this);
}
return size;
}
public boolean isString() {
return dataType == CockroachDBDataType.STRING;
}
public static CockroachDBCompositeDataType getInt(int size) {
return new CockroachDBCompositeDataType(CockroachDBDataType.INT, size);
}
public static CockroachDBCompositeDataType getBit(int size) {
return new CockroachDBCompositeDataType(CockroachDBDataType.BIT, size);
}
@Override
public String toString() {
switch (dataType) {
case INT:
switch (size) {
case 2:
return Randomly.fromOptions("INT2", "SMALLINT");
case 4:
return "INT4";
case 8:
// "INTEGER": can be affected by a session variable
return Randomly.fromOptions("INT8", "INT64", "BIGINT");
default:
return "INT";
}
case SERIAL:
switch (size) {
case 2:
return Randomly.fromOptions("SERIAL2", "SMALLSERIAL");
case 4:
return "SERIAL4";
case 8:
return Randomly.fromOptions("SERIAL8", "BIGSERIAL");
default:
throw new AssertionError();
}
case BIT:
if (size == 1 && Randomly.getBoolean()) {
return "BIT";
} else {
return String.format("BIT(%d)", size);
}
case VARBIT:
if (size == -1) {
return String.format("VARBIT");
} else {
return String.format("VARBIT(%d)", size);
}
case ARRAY:
return String.format("%s[]", elementType.toString());
default:
return dataType.toString();
}
}
public static CockroachDBCompositeDataType getRandom() {
CockroachDBDataType randomDataType = CockroachDBDataType.getRandom();
return getRandomForType(randomDataType);
}
private static CockroachDBCompositeDataType getRandomForType(CockroachDBDataType randomDataType) {
if (randomDataType == CockroachDBDataType.INT || randomDataType == CockroachDBDataType.SERIAL) {
return new CockroachDBCompositeDataType(randomDataType, Randomly.fromOptions(2, 4, 8));
} else if (randomDataType == CockroachDBDataType.BIT) {
return new CockroachDBCompositeDataType(randomDataType, (int) Randomly.getNotCachedInteger(1, 200));
} else if (randomDataType == CockroachDBDataType.VARBIT) {
return new CockroachDBCompositeDataType(randomDataType, (int) Randomly.getNotCachedInteger(1, 200));
} else if (randomDataType == CockroachDBDataType.ARRAY) {
return new CockroachDBCompositeDataType(randomDataType, getRandomForType(getArrayElementType()));
} else {
return new CockroachDBCompositeDataType(randomDataType);
}
}
private static CockroachDBDataType getArrayElementType() {
while (true) {
CockroachDBDataType type = CockroachDBDataType.getRandom();
if (type != CockroachDBDataType.ARRAY && type != CockroachDBDataType.JSONB) {
// nested arrays are not supported:
// https://github.com/cockroachdb/cockroach/issues/32552
// JSONB arrays are not supported as well:
// https://github.com/cockroachdb/cockroach/issues/23468
return type;
}
}
}
public static CockroachDBCompositeDataType getVarBit(int maxSize) {
return new CockroachDBCompositeDataType(CockroachDBDataType.VARBIT, maxSize);
}
public CockroachDBCompositeDataType getElementType() {
return elementType;
}
}
public static class CockroachDBColumn extends AbstractTableColumn<CockroachDBTable, CockroachDBCompositeDataType> {
private final boolean isPrimaryKey;
private final boolean isNullable;
public CockroachDBColumn(String name, CockroachDBCompositeDataType columnType, boolean isPrimaryKey,
boolean isNullable) {
super(name, null, columnType);
this.isPrimaryKey = isPrimaryKey;
this.isNullable = isNullable;
}
public boolean isPrimaryKey() {
return isPrimaryKey;
}
public boolean isNullable() {
return isNullable;
}
}
public static class CockroachDBTables extends AbstractTables<CockroachDBTable, CockroachDBColumn> {
public CockroachDBTables(List<CockroachDBTable> tables) {
super(tables);
}
}
public CockroachDBSchema(List<CockroachDBTable> databaseTables) {
super(databaseTables);
}
public CockroachDBTables getRandomTableNonEmptyTables() {
return new CockroachDBTables(Randomly.nonEmptySubset(getDatabaseTables()));
}
public CockroachDBTables getRandomTableNonEmptyTables(int nr) {
return new CockroachDBTables(Randomly.nonEmptySubsetLeast(getDatabaseTables(), nr));
}
private static CockroachDBCompositeDataType getColumnType(String typeString) {
if (typeString.endsWith("[]")) {
String substring = typeString.substring(0, typeString.length() - 2);
CockroachDBCompositeDataType elementType = getColumnType(substring);
return new CockroachDBCompositeDataType(CockroachDBDataType.ARRAY, elementType);
}
if (typeString.startsWith("STRING COLLATE")) {
return new CockroachDBCompositeDataType(CockroachDBDataType.STRING);
}
if (typeString.startsWith("BIT(")) {
int val = Integer.parseInt(typeString.substring(4, typeString.length() - 1));
return CockroachDBCompositeDataType.getBit(val);
}
if (typeString.startsWith("VARBIT(")) {
int val = Integer.parseInt(typeString.substring(7, typeString.length() - 1));
return CockroachDBCompositeDataType.getBit(val);
}
switch (typeString) {
case "VARBIT":
return CockroachDBCompositeDataType.getVarBit(-1);
case "BIT":
return CockroachDBCompositeDataType.getBit(1);
case "INT8":
return CockroachDBCompositeDataType.getInt(8);
case "INT4":
return CockroachDBCompositeDataType.getInt(4);
case "INT2":
return CockroachDBCompositeDataType.getInt(2);
case "BOOL":
return new CockroachDBCompositeDataType(CockroachDBDataType.BOOL);
case "STRING":
return new CockroachDBCompositeDataType(CockroachDBDataType.STRING);
case "FLOAT8":
return new CockroachDBCompositeDataType(CockroachDBDataType.FLOAT);
case "BYTES":
return new CockroachDBCompositeDataType(CockroachDBDataType.BYTES);
case "INTERVAL":
return new CockroachDBCompositeDataType(CockroachDBDataType.INTERVAL);
case "DECIMAL":
return new CockroachDBCompositeDataType(CockroachDBDataType.DECIMAL);
case "TIMESTAMP":
return new CockroachDBCompositeDataType(CockroachDBDataType.TIMESTAMP);
case "TIMESTAMPTZ":
return new CockroachDBCompositeDataType(CockroachDBDataType.TIMESTAMPTZ);
case "JSONB":
return new CockroachDBCompositeDataType(CockroachDBDataType.JSONB);
case "TIME":
return new CockroachDBCompositeDataType(CockroachDBDataType.TIME);
case "TIMETZ":
return new CockroachDBCompositeDataType(CockroachDBDataType.TIMETZ);
default:
throw new AssertionError(typeString);
}
}
public static class CockroachDBTable
extends AbstractRelationalTable<CockroachDBColumn, TableIndex, CockroachDBGlobalState> {
public CockroachDBTable(String tableName, List<CockroachDBColumn> columns, List<TableIndex> indexes,
boolean isView) {
super(tableName, columns, indexes, isView);
}
}
public int getIndexCount() {
int count = 0;
for (CockroachDBTable table : getDatabaseTables()) {
count += table.getIndexes().size();
}
return count;
}
public static CockroachDBSchema fromConnection(SQLConnection con, String databaseName) throws SQLException {
List<CockroachDBTable> databaseTables = new ArrayList<>();
List<String> tableNames = getTableNames(con);
for (String tableName : tableNames) {
List<CockroachDBColumn> databaseColumns = getTableColumns(con, tableName);
List<TableIndex> indexes = getIndexes(con, tableName);
boolean isView = tableName.startsWith("v");
CockroachDBTable t = new CockroachDBTable(tableName, databaseColumns, indexes, isView);
for (CockroachDBColumn c : databaseColumns) {
c.setTable(t);
}
// To avoid some situations that columns can not be retrieved.
if (databaseColumns.isEmpty()) {
continue;
}
databaseTables.add(t);
}
return new CockroachDBSchema(databaseTables);
}
private static List<String> getTableNames(SQLConnection con) throws SQLException {
List<String> tableNames = new ArrayList<>();
try (Statement s = con.createStatement()) {
ResultSet tableRs = s.executeQuery(
"SELECT table_name FROM information_schema.tables WHERE TABLE_TYPE IN ('BASE TABLE', 'LOCAL TEMPORARY');");
while (tableRs.next()) {
String tableName = tableRs.getString(1);
tableNames.add(tableName);
}
}
return tableNames;
}
private static List<TableIndex> getIndexes(SQLConnection con, String tableName) throws SQLException {
List<TableIndex> indexes = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery(String.format("SHOW INDEX FROM %s", tableName))) {
while (rs.next()) {
String indexName = rs.getString("index_name");
indexes.add(TableIndex.create(indexName));
}
}
}
return indexes;
}
private static List<CockroachDBColumn> getTableColumns(SQLConnection con, String tableName) throws SQLException {
List<CockroachDBColumn> columns = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SHOW COLUMNS FROM " + tableName)) {
while (rs.next()) {
String columnName = rs.getString("column_name");
if (columnName.contains("crdb_internal")) {
continue; // created for CREATE INDEX ON t0(c0) USING HASH WITH BUCKET_COUNT = 1;
}
String dataType = rs.getString("data_type");
boolean isNullable = rs.getBoolean("is_nullable");
String indices = rs.getString("indices");
boolean isPrimaryKey = indices.contains("primary");
CockroachDBColumn c = new CockroachDBColumn(columnName, getColumnType(dataType), isPrimaryKey,
isNullable);
columns.add(c);
}
} catch (SQLException e) {
if (CockroachDBBugs.bug85394 && e.getMessage().contains("incompatible type annotation for ARRAY")) {
return columns;
}
throw e;
}
}
return columns;
}
}