-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathTiDBSchema.java
More file actions
369 lines (322 loc) · 11.6 KB
/
TiDBSchema.java
File metadata and controls
369 lines (322 loc) · 11.6 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
package sqlancer.tidb;
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.common.schema.AbstractRelationalTable;
import sqlancer.common.schema.AbstractSchema;
import sqlancer.common.schema.AbstractTableColumn;
import sqlancer.common.schema.AbstractTables;
import sqlancer.common.schema.TableIndex;
import sqlancer.tidb.TiDBProvider.TiDBGlobalState;
import sqlancer.tidb.TiDBSchema.TiDBTable;
public class TiDBSchema extends AbstractSchema<TiDBGlobalState, TiDBTable> {
public enum TiDBDataType {
INT, TEXT, BOOL, FLOATING, CHAR, DECIMAL, NUMERIC, BLOB;
private final boolean isPrimitive;
TiDBDataType() {
isPrimitive = true;
}
TiDBDataType(boolean isPrimitive) {
this.isPrimitive = isPrimitive;
}
public static TiDBDataType getRandom() {
return Randomly.fromOptions(values());
}
public boolean isPrimitive() {
return isPrimitive;
}
public boolean isNumeric() {
switch (this) {
case INT:
case DECIMAL:
case FLOATING:
case BOOL:
case NUMERIC:
return true;
case CHAR:
case TEXT:
case BLOB:
return false;
default:
throw new AssertionError(this);
}
}
public boolean canHaveDefault() {
switch (this) {
case INT:
case DECIMAL:
case FLOATING:
case BOOL:
case CHAR:
return true;
case NUMERIC:
case TEXT:
case BLOB:
return false;
default:
throw new AssertionError(this);
}
}
}
public static class TiDBCompositeDataType {
private final TiDBDataType dataType;
private final int size;
public TiDBCompositeDataType(TiDBDataType dataType) {
this.dataType = dataType;
this.size = -1;
}
public TiDBCompositeDataType(TiDBDataType dataType, int size) {
this.dataType = dataType;
this.size = size;
}
public TiDBDataType getPrimitiveDataType() {
return dataType;
}
public int getSize() {
if (size == -1) {
throw new AssertionError(this);
}
return size;
}
public static TiDBCompositeDataType getInt(int size) {
return new TiDBCompositeDataType(TiDBDataType.INT, size);
}
public static TiDBCompositeDataType getRandom() {
TiDBDataType primitiveType = TiDBDataType.getRandom();
int size = -1;
switch (primitiveType) {
case INT:
size = Randomly.fromOptions(1, 2, 4, 8);
break;
case FLOATING:
size = Randomly.fromOptions(4, 8);
break;
default:
break;
}
return new TiDBCompositeDataType(primitiveType, size);
}
@Override
public String toString() {
switch (getPrimitiveDataType()) {
case INT:
switch (size) {
case 1:
return "TINYINT";
case 2:
return "SMALLINT";
case 3:
return "MEDIUMINT";
case 4:
return "INTEGER";
case 8:
return "BIGINT";
default:
throw new AssertionError(size);
}
case FLOATING:
switch (size) {
case 4:
return "FLOAT";
case 8:
return "DOUBLE";
default:
throw new AssertionError(size);
}
default:
return getPrimitiveDataType().toString();
}
}
}
public static class TiDBColumn extends AbstractTableColumn<TiDBTable, TiDBCompositeDataType> {
private final boolean isPrimaryKey;
private final boolean isNullable;
private final boolean hasDefault;
public TiDBColumn(String name, TiDBCompositeDataType columnType, boolean isPrimaryKey, boolean isNullable,
boolean hasDefault) {
super(name, null, columnType);
this.isPrimaryKey = isPrimaryKey;
this.isNullable = isNullable;
this.hasDefault = hasDefault;
}
public boolean isPrimaryKey() {
return isPrimaryKey;
}
public boolean isNullable() {
return isNullable;
}
public boolean hasDefault() {
return hasDefault;
}
}
public static class TiDBTables extends AbstractTables<TiDBTable, TiDBColumn> {
public TiDBTables(List<TiDBTable> tables) {
super(tables);
}
}
public TiDBSchema(List<TiDBTable> databaseTables) {
super(databaseTables);
}
public TiDBTables getRandomTableNonEmptyTables() {
return new TiDBTables(Randomly.nonEmptySubset(getDatabaseTables()));
}
public int getIndexCount() {
int count = 0;
for (TiDBTable table : getDatabaseTables()) {
count += table.getIndexes().size();
}
return count;
}
private static TiDBCompositeDataType getColumnType(String typeString) {
String trimmedStringType = typeString.replace(" zerofill", "").replace(" unsigned", "");
if (trimmedStringType.contains("decimal")) {
return new TiDBCompositeDataType(TiDBDataType.DECIMAL);
}
if (trimmedStringType.startsWith("var_string") || trimmedStringType.contains("binary")
|| trimmedStringType.startsWith("varchar")) {
return new TiDBCompositeDataType(TiDBDataType.TEXT);
}
if (trimmedStringType.startsWith("char")) {
return new TiDBCompositeDataType(TiDBDataType.CHAR);
}
TiDBDataType primitiveType;
int size = -1;
if (trimmedStringType.startsWith("bigint")) {
primitiveType = TiDBDataType.INT;
size = 8;
} else {
switch (trimmedStringType) {
case "text":
case "mediumtext":
case "longtext":
case "tinytext":
primitiveType = TiDBDataType.TEXT;
break;
case "float":
size = 4;
primitiveType = TiDBDataType.FLOATING;
break;
case "double":
case "double(8,6)": // workaround to address https://github.com/sqlancer/sqlancer/issues/669
case "double(23,16)":
size = 8;
primitiveType = TiDBDataType.FLOATING;
break;
case "tinyint(1)":
primitiveType = TiDBDataType.BOOL;
size = 1;
break;
case "null":
primitiveType = TiDBDataType.INT;
size = 1;
break;
case "tinyint(2)":
case "tinyint(3)":
case "tinyint(4)":
primitiveType = TiDBDataType.INT;
size = 1;
break;
case "smallint(5)":
case "smallint(6)":
primitiveType = TiDBDataType.INT;
size = 2;
break;
case "int(10)":
case "int(11)":
primitiveType = TiDBDataType.INT;
size = 4;
break;
case "blob":
case "mediumblob":
case "longblob":
case "tinyblob":
primitiveType = TiDBDataType.BLOB;
break;
case "date":
case "datetime":
case "datetime(6)": // workaround to address https://github.com/sqlancer/sqlancer/issues/669
case "timestamp":
case "time":
case "year":
primitiveType = TiDBDataType.NUMERIC;
break;
default:
throw new AssertionError(trimmedStringType);
}
}
return new TiDBCompositeDataType(primitiveType, size);
}
public static class TiDBTable extends AbstractRelationalTable<TiDBColumn, TableIndex, TiDBGlobalState> {
public TiDBTable(String tableName, List<TiDBColumn> columns, List<TableIndex> indexes, boolean isView) {
super(tableName, columns, indexes, isView);
}
public boolean hasPrimaryKey() {
return getColumns().stream().anyMatch(c -> c.isPrimaryKey());
}
}
public static TiDBSchema fromConnection(SQLConnection con, String databaseName) throws SQLException {
List<TiDBTable> databaseTables = new ArrayList<>();
List<String> tableNames = getTableNames(con);
for (String tableName : tableNames) {
List<TiDBColumn> databaseColumns = getTableColumns(con, tableName);
// Ignore invalid views
if (databaseColumns.isEmpty()) {
continue;
}
List<TableIndex> indexes = getIndexes(con, tableName);
boolean isView = tableName.startsWith("v");
TiDBTable t = new TiDBTable(tableName, databaseColumns, indexes, isView);
for (TiDBColumn c : databaseColumns) {
c.setTable(t);
}
databaseTables.add(t);
}
return new TiDBSchema(databaseTables);
}
private static List<String> getTableNames(SQLConnection con) throws SQLException {
List<String> tableNames = new ArrayList<>();
try (Statement s = con.createStatement()) {
ResultSet tableRs = s.executeQuery("SHOW TABLES");
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("Key_name");
indexes.add(TableIndex.create(indexName));
}
}
}
return indexes;
}
private static List<TiDBColumn> getTableColumns(SQLConnection con, String tableName) throws SQLException {
List<TiDBColumn> columns = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SHOW COLUMNS FROM " + tableName)) {
while (rs.next()) {
String columnName = rs.getString("Field");
String dataType = rs.getString("Type");
boolean isNullable = rs.getString("Null").contentEquals("YES");
boolean isPrimaryKey = rs.getString("Key").contains("PRI");
boolean hasDefault = rs.getString("Default") != null;
TiDBColumn c = new TiDBColumn(columnName, getColumnType(dataType), isPrimaryKey, isNullable,
hasDefault);
columns.add(c);
}
}
} catch (SQLException e) { // Happens when
}
return columns;
}
}