-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathQuestDBSchema.java
More file actions
314 lines (275 loc) · 9.86 KB
/
QuestDBSchema.java
File metadata and controls
314 lines (275 loc) · 9.86 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
package sqlancer.questdb;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.common.DBMSCommon;
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.questdb.QuestDBProvider.QuestDBGlobalState;
import sqlancer.questdb.QuestDBSchema.QuestDBTable;
public class QuestDBSchema extends AbstractSchema<QuestDBGlobalState, QuestDBTable> {
public enum QuestDBDataType {
BOOLEAN, // CHAR,
/* STRING, */
INT, FLOAT, SYMBOL,
// DATE, TIMESTAMP,
/* GEOHASH, */
NULL;
public static QuestDBDataType getRandomWithoutNull() {
QuestDBDataType dt;
do {
dt = Randomly.fromOptions(values());
} while (dt == QuestDBDataType.NULL);
return dt;
}
}
public static class QuestDBCompositeDataType {
private final QuestDBDataType dataType;
private final int size;
private final boolean isNullable;
public QuestDBCompositeDataType(QuestDBDataType dataType, int size) {
this.dataType = dataType;
this.size = size;
switch (dataType) {
case INT:
switch (size) {
case 1:
case 2:
isNullable = false;
break;
default:
isNullable = true;
break;
}
break;
case BOOLEAN:
isNullable = false;
break;
case SYMBOL:
isNullable = true;
break;
default:
isNullable = true;
}
}
public QuestDBDataType getPrimitiveDataType() {
return dataType;
}
public int getSize() {
if (size == -1) {
throw new AssertionError(this);
}
return size;
}
public boolean isNullable() {
return isNullable;
}
public static QuestDBCompositeDataType getRandomWithoutNull() {
QuestDBDataType type = QuestDBDataType.getRandomWithoutNull();
int size = -1;
switch (type) {
case INT:
size = Randomly.fromOptions(1, 2, 4);
break;
case FLOAT:
size = Randomly.fromOptions(4, 8, 32);
break;
case BOOLEAN:
// case CHAR:
// case DATE:
// case TIMESTAMP:
size = 0;
break;
case SYMBOL:
size = 0;
break;
default:
throw new AssertionError(type);
}
return new QuestDBCompositeDataType(type, size);
}
@Override
public String toString() {
switch (getPrimitiveDataType()) {
case INT:
switch (size) {
case 1:
return Randomly.fromOptions("BYTE");
case 2:
return Randomly.fromOptions("SHORT");
case 4:
return Randomly.fromOptions("INT");
default:
throw new AssertionError(size);
}
// case CHAR:
// return "CHAR";
case FLOAT:
switch (size) {
case 4:
return Randomly.fromOptions("FLOAT");
case 8:
return Randomly.fromOptions(/* "DOUBLE", */"LONG");
case 32:
return Randomly.fromOptions("LONG256");
default:
throw new AssertionError(size);
}
case BOOLEAN:
return Randomly.fromOptions("BOOLEAN");
case SYMBOL:
return "SYMBOL";
// case TIMESTAMP:
// return Randomly.fromOptions("TIMESTAMP");
// case DATE:
// return Randomly.fromOptions("DATE");
case NULL:
return Randomly.fromOptions("NULL");
default:
throw new AssertionError(getPrimitiveDataType());
}
}
}
public static class QuestDBColumn extends AbstractTableColumn<QuestDBTable, QuestDBCompositeDataType> {
private final boolean isIndexed;
private final boolean isNullable;
public QuestDBColumn(String name, QuestDBCompositeDataType columnType, boolean isIndexed) {
super(name, null, columnType);
this.isIndexed = isIndexed;
this.isNullable = columnType == null || columnType.isNullable();
}
public boolean isIndexed() {
return isIndexed;
}
public boolean isNullable() {
return isNullable;
}
}
public static class QuestDBTables extends AbstractTables<QuestDBTable, QuestDBColumn> {
public static final Set<String> RESERVED_TABLES = new HashSet<>(
Arrays.asList("sys.column_versions_purge_log", "telemetry_config", "telemetry", "sys.telemetry_wal"));
public QuestDBTables(List<QuestDBTable> tables) {
super(tables);
}
}
public QuestDBSchema(List<QuestDBTable> databaseTables) {
super(databaseTables);
}
public QuestDBTables getRandomTableNonEmptyTables() {
return new QuestDBTables(Randomly.nonEmptySubset(getDatabaseTables()));
}
private static QuestDBCompositeDataType getColumnType(String typeString) {
QuestDBDataType primitiveType;
int size = -1;
switch (typeString) {
case "INT":
primitiveType = QuestDBDataType.INT;
size = 4;
break;
// case "CHAR":
// primitiveType = QuestDBDataType.CHAR;
// break;
case "FLOAT":
primitiveType = QuestDBDataType.FLOAT;
size = 4;
break;
case "LONG":
primitiveType = QuestDBDataType.FLOAT;
size = 8;
break;
case "LONG256":
primitiveType = QuestDBDataType.FLOAT;
size = 32;
break;
case "BOOLEAN":
primitiveType = QuestDBDataType.BOOLEAN;
break;
// case "DATE":
// primitiveType = QuestDBDataType.DATE;
// break;
// case "TIMESTAMP":
// primitiveType = QuestDBDataType.TIMESTAMP;
// break;
case "BYTE":
primitiveType = QuestDBDataType.INT;
size = 1;
break;
case "SHORT":
primitiveType = QuestDBDataType.INT;
size = 2;
break;
case "SYMBOL":
primitiveType = QuestDBDataType.SYMBOL;
break;
case "NULL":
primitiveType = QuestDBDataType.NULL;
break;
default:
throw new AssertionError(typeString);
}
return new QuestDBCompositeDataType(primitiveType, size);
}
public static class QuestDBTable extends AbstractRelationalTable<QuestDBColumn, TableIndex, QuestDBGlobalState> {
public QuestDBTable(String tableName, List<QuestDBColumn> columns, boolean isView) {
super(tableName, columns, Collections.emptyList(), isView);
}
}
public static QuestDBSchema fromConnection(SQLConnection con, String databaseName) throws SQLException {
List<QuestDBTable> databaseTables = new ArrayList<>();
List<String> tableNames = getTableNames(con);
for (String tableName : tableNames) {
if (DBMSCommon.matchesIndexName(tableName)) {
continue; // TODO: unexpected?
}
List<QuestDBColumn> databaseColumns = getTableColumns(con, tableName);
boolean isView = tableName.startsWith("v");
QuestDBTable t = new QuestDBTable(tableName, databaseColumns, isView);
for (QuestDBColumn c : databaseColumns) {
c.setTable(t);
}
databaseTables.add(t);
}
return new QuestDBSchema(databaseTables);
}
protected static List<String> getTableNames(SQLConnection con) throws SQLException {
List<String> tableNames = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SHOW TABLES;")) {
while (rs.next()) {
String tName = rs.getString("table");
// exclude reserved tables for testing
if (!QuestDBTables.RESERVED_TABLES.contains(tName)) {
tableNames.add(tName);
}
}
}
}
return tableNames;
}
private static List<QuestDBColumn> getTableColumns(SQLConnection con, String tableName) throws SQLException {
List<QuestDBColumn> columns = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery(String.format("SHOW COLUMNS FROM %s;", tableName))) {
while (rs.next()) {
String columnName = rs.getString("column");
String dataType = rs.getString("type");
boolean isIndexed = rs.getString("indexed").contains("true");
QuestDBColumn c = new QuestDBColumn(columnName, getColumnType(dataType), isIndexed);
columns.add(c);
}
}
}
return columns;
}
}