forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuckDBSchema.java
More file actions
272 lines (235 loc) · 9.02 KB
/
DuckDBSchema.java
File metadata and controls
272 lines (235 loc) · 9.02 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
package sqlancer.duckdb;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import sqlancer.IgnoreMeException;
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.duckdb.DuckDBProvider.DuckDBGlobalState;
import sqlancer.duckdb.DuckDBSchema.DuckDBTable;
public class DuckDBSchema extends AbstractSchema<DuckDBGlobalState, DuckDBTable> {
public enum DuckDBDataType {
INT, VARCHAR, BOOLEAN, FLOAT, DATE, TIMESTAMP;
public static DuckDBDataType getRandom() {
return Randomly.fromOptions(values());
}
}
public static class DuckDBCompositeDataType {
private final DuckDBDataType dataType;
private final int size;
public DuckDBCompositeDataType(DuckDBDataType dataType, int size) {
this.dataType = dataType;
this.size = size;
}
public DuckDBDataType getPrimitiveDataType() {
return dataType;
}
public int getSize() {
if (size == -1) {
throw new AssertionError(this);
}
return size;
}
public static DuckDBCompositeDataType getRandom() {
DuckDBDataType type = DuckDBDataType.getRandom();
int size = -1;
switch (type) {
case INT:
size = Randomly.fromOptions(1, 2, 4, 8);
break;
case FLOAT:
size = Randomly.fromOptions(4, 8);
break;
case BOOLEAN:
case VARCHAR:
case DATE:
case TIMESTAMP:
size = 0;
break;
default:
throw new AssertionError(type);
}
return new DuckDBCompositeDataType(type, size);
}
@Override
public String toString() {
switch (getPrimitiveDataType()) {
case INT:
switch (size) {
case 8:
return Randomly.fromOptions("BIGINT", "INT8");
case 4:
return Randomly.fromOptions("INTEGER", "INT", "INT4", "SIGNED");
case 2:
return Randomly.fromOptions("SMALLINT", "INT2");
case 1:
return Randomly.fromOptions("TINYINT", "INT1");
default:
throw new AssertionError(size);
}
case VARCHAR:
return "VARCHAR";
case FLOAT:
switch (size) {
case 8:
return Randomly.fromOptions("DOUBLE");
case 4:
return Randomly.fromOptions("REAL", "FLOAT4");
default:
throw new AssertionError(size);
}
case BOOLEAN:
return Randomly.fromOptions("BOOLEAN", "BOOL");
case TIMESTAMP:
return Randomly.fromOptions("TIMESTAMP", "DATETIME");
case DATE:
return Randomly.fromOptions("DATE");
default:
throw new AssertionError(getPrimitiveDataType());
}
}
}
public static class DuckDBColumn extends AbstractTableColumn<DuckDBTable, DuckDBCompositeDataType> {
private final boolean isPrimaryKey;
private final boolean isNullable;
public DuckDBColumn(String name, DuckDBCompositeDataType 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 DuckDBTables extends AbstractTables<DuckDBTable, DuckDBColumn> {
public DuckDBTables(List<DuckDBTable> tables) {
super(tables);
}
}
public DuckDBSchema(List<DuckDBTable> databaseTables) {
super(databaseTables);
}
public DuckDBTables getRandomTableNonEmptyTables() {
return new DuckDBTables(Randomly.nonEmptySubset(getDatabaseTables()));
}
private static DuckDBCompositeDataType getColumnType(String typeString) {
DuckDBDataType primitiveType;
int size = -1;
if (typeString.startsWith("DECIMAL")) { // Ugly hack
return new DuckDBCompositeDataType(DuckDBDataType.FLOAT, 8);
}
switch (typeString) {
case "INTEGER":
primitiveType = DuckDBDataType.INT;
size = 4;
break;
case "SMALLINT":
primitiveType = DuckDBDataType.INT;
size = 2;
break;
case "BIGINT":
case "HUGEINT": // TODO: 16-bit int
primitiveType = DuckDBDataType.INT;
size = 8;
break;
case "TINYINT":
primitiveType = DuckDBDataType.INT;
size = 1;
break;
case "VARCHAR":
primitiveType = DuckDBDataType.VARCHAR;
break;
case "FLOAT":
primitiveType = DuckDBDataType.FLOAT;
size = 4;
break;
case "DOUBLE":
primitiveType = DuckDBDataType.FLOAT;
size = 8;
break;
case "BOOLEAN":
primitiveType = DuckDBDataType.BOOLEAN;
break;
case "DATE":
primitiveType = DuckDBDataType.DATE;
break;
case "TIMESTAMP":
primitiveType = DuckDBDataType.TIMESTAMP;
break;
case "INTERVAL":
throw new IgnoreMeException();
// TODO: caused when a view contains a computation like ((TIMESTAMP '1970-01-05 11:26:57')-(TIMESTAMP
// '1969-12-29 06:50:27'))
default:
throw new AssertionError(typeString);
}
return new DuckDBCompositeDataType(primitiveType, size);
}
public static class DuckDBTable extends AbstractRelationalTable<DuckDBColumn, TableIndex, DuckDBGlobalState> {
public DuckDBTable(String tableName, List<DuckDBColumn> columns, boolean isView) {
super(tableName, columns, Collections.emptyList(), isView);
}
}
public static DuckDBSchema fromConnection(SQLConnection con, String databaseName) throws SQLException {
List<DuckDBTable> databaseTables = new ArrayList<>();
List<String> tableNames = getTableNames(con);
for (String tableName : tableNames) {
if (DBMSCommon.matchesIndexName(tableName)) {
continue; // TODO: unexpected?
}
List<DuckDBColumn> databaseColumns = getTableColumns(con, tableName);
boolean isView = tableName.startsWith("v");
DuckDBTable t = new DuckDBTable(tableName, databaseColumns, isView);
for (DuckDBColumn c : databaseColumns) {
c.setTable(t);
}
databaseTables.add(t);
}
return new DuckDBSchema(databaseTables);
}
private static List<String> getTableNames(SQLConnection con) throws SQLException {
List<String> tableNames = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT * FROM sqlite_master()")) {
while (rs.next()) {
tableNames.add(rs.getString("name"));
}
}
}
return tableNames;
}
private static List<DuckDBColumn> getTableColumns(SQLConnection con, String tableName) throws SQLException {
List<DuckDBColumn> columns = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery(String.format("SELECT * FROM pragma_table_info('%s');", tableName))) {
while (rs.next()) {
String columnName = rs.getString("name");
String dataType = rs.getString("type");
boolean isNullable = rs.getString("notnull").contentEquals("false");
boolean isPrimaryKey = rs.getString("pk").contains("true");
DuckDBColumn c = new DuckDBColumn(columnName, getColumnType(dataType), isPrimaryKey, isNullable);
columns.add(c);
}
}
}
if (columns.stream().noneMatch(c -> c.isPrimaryKey())) {
// https://github.com/cwida/duckdb/issues/589
// https://github.com/cwida/duckdb/issues/588
// TODO: implement an option to enable/disable rowids
columns.add(new DuckDBColumn("rowid", new DuckDBCompositeDataType(DuckDBDataType.INT, 4), false, false));
}
return columns;
}
}