forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFelderaSchema.java
More file actions
315 lines (261 loc) · 10.6 KB
/
FelderaSchema.java
File metadata and controls
315 lines (261 loc) · 10.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
package sqlancer.feldera;
import java.util.*;
import java.util.stream.Collectors;
import sqlancer.Randomly;
import sqlancer.common.schema.*;
import sqlancer.feldera.ast.FelderaColumnReference;
import sqlancer.feldera.ast.FelderaConstant;
import sqlancer.feldera.ast.FelderaExpression;
public class FelderaSchema extends AbstractSchema<FelderaGlobalState, FelderaSchema.FelderaTable> {
private final String pipelineName;
public FelderaSchema(List<FelderaTable> databaseTables, String pipelineName) {
super(databaseTables);
this.pipelineName = pipelineName;
}
public FelderaSchema(String pipelineName) {
super(new ArrayList<>());
this.pipelineName = pipelineName;
}
public FelderaSchema addTable(FelderaTable table) {
List<FelderaTable> tables = new ArrayList<>(this.getDatabaseTables());
tables.add(table);
return new FelderaSchema(tables, this.pipelineName);
}
public static FelderaSchema fromConnection(FelderaConnection con) throws Exception {
return new FelderaSchema(new ArrayList<>(), con.getPipelineName());
}
protected List<FelderaColumn> getTableColumns(String tableName) throws Exception {
return this.getDatabaseTable(tableName).getColumns();
}
public FelderaTables getRandomTableNonEmptyTables() {
return new FelderaTables(Randomly.nonEmptySubset(getDatabaseTables()));
}
public String getPipelineName() {
return pipelineName;
}
public enum FelderaDataType {
BOOLEAN, INT, VARCHAR, CHAR, NULL, TIME, DATE, TIMESTAMP, DECIMAL, FLOAT, ANY,
// VARIANT,
// VARBINARY,
// INTERVAL,
// GEOMETRY,
// ROW,
// MAP,
ARRAY;
public static FelderaDataType getRandomNumericType() {
return Randomly
.fromList(Arrays.stream(values()).filter(FelderaDataType::isNumeric).collect(Collectors.toList()));
}
public boolean isNumeric() {
switch (this) {
case FLOAT:
case INT:
case DECIMAL:
return true;
default:
return false;
}
}
public static FelderaDataType getRandomNonNullType() {
return Randomly.fromList(Arrays.stream(values())
.filter(t -> t != FelderaDataType.NULL && t != FelderaDataType.ANY).collect(Collectors.toList()));
}
public static FelderaDataType[] nonNullValues() {
return Arrays.stream(values()).filter(t -> t != FelderaDataType.NULL && t != FelderaDataType.ANY)
.toArray(FelderaDataType[]::new);
}
public static FelderaDataType getRandomType() {
return Randomly.fromOptions(values());
}
}
public static class FelderaCompositeDataType {
private final FelderaDataType dataType;
private final int size;
private final int scale;
private final FelderaCompositeDataType elementType;
public FelderaCompositeDataType(FelderaDataType dataType, int size, int scale) {
this.dataType = dataType;
this.size = size;
this.scale = scale;
this.elementType = null;
}
public static FelderaCompositeDataType arrayOf(FelderaDataType elementType) {
return new FelderaCompositeDataType(FelderaDataType.ARRAY, getRandomFromPrimitiveType(elementType));
}
public FelderaCompositeDataType(FelderaDataType dataType, FelderaCompositeDataType elementType) {
if (dataType != FelderaDataType.ARRAY) {
throw new IllegalArgumentException("dataType must be ARRAY");
}
this.dataType = dataType;
this.scale = -1;
this.size = -1;
this.elementType = elementType;
}
public FelderaExpression getRandomConstant(FelderaGlobalState globalState) {
if (Randomly.getBooleanWithSmallProbability()) {
return FelderaConstant.createNullConstant();
}
return FelderaConstant.getRandomConstant(globalState, this);
}
public boolean isNumeric() {
return this.getPrimitiveType().isNumeric();
}
public FelderaCompositeDataType getElementType() {
return this.elementType;
}
public static FelderaCompositeDataType getBooleanType() {
return FelderaCompositeDataType.getRandomFromPrimitiveType(FelderaDataType.BOOLEAN);
}
public static FelderaCompositeDataType getRandomVarcharType() {
return FelderaCompositeDataType.getRandomFromPrimitiveType(FelderaDataType.VARCHAR);
}
public static FelderaCompositeDataType getRandomNumericType() {
FelderaDataType type = FelderaDataType.getRandomNumericType();
return FelderaCompositeDataType.getRandomFromPrimitiveType(type);
}
public FelderaDataType getPrimitiveType() {
return dataType;
}
public int getSize() {
return size;
}
public int getScale() {
return scale;
}
public boolean isArray() {
return dataType == FelderaDataType.ARRAY;
}
public static FelderaCompositeDataType getRandomFromPrimitiveType(FelderaDataType type) {
int size = -1;
int scale = -1;
switch (type) {
case FLOAT:
size = Randomly.fromOptions(32, 64);
return new FelderaCompositeDataType(type, size, scale);
case INT:
size = Randomly.fromOptions(8, 16, 32, 64);
return new FelderaCompositeDataType(type, size, scale);
case ARRAY:
return new FelderaCompositeDataType(type, FelderaCompositeDataType.getRandomWithoutNull());
case DECIMAL:
scale = (int) Randomly.getNotCachedInteger(0, 10);
size = (int) Randomly.getNotCachedInteger(scale, 25);
return new FelderaCompositeDataType(type, size, scale);
case CHAR:
size = (int) Randomly.getNotCachedInteger(1, 10);
return new FelderaCompositeDataType(type, size, scale);
case VARCHAR:
if (Randomly.getBoolean()) {
size = (int) Randomly.getNotCachedInteger(1, 30);
}
return new FelderaCompositeDataType(type, size, scale);
default:
return new FelderaCompositeDataType(type, size, scale);
}
}
public static FelderaCompositeDataType getRandomWithoutNull() {
FelderaDataType type = FelderaDataType.getRandomNonNullType();
return FelderaCompositeDataType.getRandomFromPrimitiveType(type);
}
@Override
public String toString() {
switch (dataType) {
case INT:
switch (size) {
case 8:
return "TINYINT";
case 16:
return "SMALLINT";
case 32:
return "INT";
case 64:
return "BIGINT";
default:
throw new AssertionError(dataType.toString() + scale);
}
case FLOAT:
switch (size) {
case 32:
return "REAL";
case 64:
return "DOUBLE";
default:
throw new AssertionError(dataType.toString() + scale);
}
case ARRAY:
if (elementType == null) {
throw new AssertionError(this);
}
return elementType + " ARRAY";
case CHAR:
return "CHAR(" + size + ")";
case VARCHAR:
if (size == -1) {
return "VARCHAR";
}
return "VARCHAR(" + size + ")";
case DECIMAL:
return "DECIMAL(" + size + ", " + scale + ")";
default:
return dataType.toString();
}
}
}
public static class FelderaFieldColumn extends FelderaColumn {
public FelderaFieldColumn(String name, FelderaCompositeDataType columnType) {
super(name, columnType);
}
public FelderaFieldColumn(String name, FelderaCompositeDataType columnType, boolean isNullable) {
super(name, columnType, isNullable);
// Note to self: later, assert that the Field column isn't something like INTERVAL
}
}
public static class FelderaColumn extends AbstractTableColumn<FelderaTable, FelderaCompositeDataType> {
private final boolean isNullable;
public FelderaColumn(String name, FelderaCompositeDataType columnType) {
super(name, null, columnType);
this.isNullable = false;
}
public FelderaColumn(String name, FelderaCompositeDataType columnType, boolean isNullable) {
super(name, null, columnType);
this.isNullable = isNullable;
}
public FelderaColumnReference asColumnReference() {
return new FelderaColumnReference(this);
}
public static FelderaColumn createDummy(String name) {
return new FelderaColumn(name, FelderaCompositeDataType.getRandomWithoutNull());
}
public boolean isNullable() {
return isNullable;
}
}
public static class FelderaTables extends AbstractTables<FelderaTable, FelderaColumn> {
public FelderaTables(List<FelderaTable> tables) {
super(tables);
}
}
public static class FelderaTable extends AbstractTable<FelderaColumn, TableIndex, FelderaGlobalState> {
public FelderaTable(String tableName, List<FelderaColumn> columns) {
super(tableName, columns, null, false);
}
// SELECT COUNT(*) FROM table;
@Override
public long getNrRows(FelderaGlobalState globalState) {
// TODO
return 0;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FelderaTable) {
FelderaTable other = (FelderaTable) obj;
return Objects.equals(this.name, other.name) && this.getColumns() == other.getColumns();
} else {
return false;
}
}
public static List<FelderaColumn> getAllColumns(List<FelderaTable> tables) {
return tables.stream().map(AbstractTable::getColumns).flatMap(List::stream).collect(Collectors.toList());
}
}
}