forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClickHouseSchema.java
More file actions
561 lines (485 loc) · 21.5 KB
/
Copy pathClickHouseSchema.java
File metadata and controls
561 lines (485 loc) · 21.5 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package sqlancer.clickhouse;
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 java.util.Map;
import java.util.Optional;
import com.clickhouse.data.ClickHouseDataType;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.clickhouse.ClickHouseProvider.ClickHouseGlobalState;
import sqlancer.clickhouse.ClickHouseSchema.ClickHouseTable;
import sqlancer.clickhouse.ClickHouseType.Array;
import sqlancer.clickhouse.ClickHouseType.DateTime64Type;
import sqlancer.clickhouse.ClickHouseType.Decimal;
import sqlancer.clickhouse.ClickHouseType.FixedString;
import sqlancer.clickhouse.ClickHouseType.Kind;
import sqlancer.clickhouse.ClickHouseType.LowCardinality;
import sqlancer.clickhouse.ClickHouseType.Nullable;
import sqlancer.clickhouse.ClickHouseType.Primitive;
import sqlancer.clickhouse.ClickHouseType.Unknown;
import sqlancer.clickhouse.ast.ClickHouseColumnReference;
import sqlancer.clickhouse.ast.ClickHouseConstant;
import sqlancer.clickhouse.ast.constant.ClickHouseCreateConstant;
import sqlancer.common.schema.AbstractRelationalTable;
import sqlancer.common.schema.AbstractRowValue;
import sqlancer.common.schema.AbstractSchema;
import sqlancer.common.schema.AbstractTableColumn;
import sqlancer.common.schema.AbstractTables;
import sqlancer.common.schema.TableIndex;
public class ClickHouseSchema extends AbstractSchema<ClickHouseGlobalState, ClickHouseTable> {
public static class ClickHouseLancerDataType {
private final ClickHouseType typeTerm;
private final ClickHouseDataType clickHouseType;
private final String textRepr;
public ClickHouseLancerDataType(ClickHouseDataType type) {
Optional<Kind> kindOpt = Kind.fromClickHouseDataType(type);
this.typeTerm = kindOpt.isPresent() ? new Primitive(kindOpt.get()) : new Unknown(type.name());
this.clickHouseType = type;
this.textRepr = typeTerm.toString();
}
public ClickHouseLancerDataType(String textRepr) {
this.textRepr = textRepr;
this.typeTerm = ClickHouseTypeParser.parse(textRepr);
this.clickHouseType = rootClickHouseDataType(this.typeTerm);
}
public ClickHouseLancerDataType(ClickHouseType typeTerm) {
this.typeTerm = typeTerm;
this.textRepr = typeTerm.toString();
this.clickHouseType = rootClickHouseDataType(typeTerm);
}
private static ClickHouseDataType rootClickHouseDataType(ClickHouseType t) {
ClickHouseType inner = t.unwrap();
if (inner instanceof Primitive p) {
return p.kind().toClickHouseDataType();
}
if (inner instanceof FixedString) {
return ClickHouseDataType.FixedString;
}
if (inner instanceof Decimal) {
return ClickHouseDataType.Decimal;
}
if (inner instanceof DateTime64Type) {
return ClickHouseDataType.DateTime64;
}
if (inner instanceof Array) {
return ClickHouseDataType.Array;
}
if (inner instanceof ClickHouseType.Tuple) {
return ClickHouseDataType.Tuple;
}
if (inner instanceof ClickHouseType.Enum e) {
return e.width() == 8 ? ClickHouseDataType.Enum8 : ClickHouseDataType.Enum16;
}
if (inner instanceof ClickHouseType.Time) {
return ClickHouseDataType.Time;
}
if (inner instanceof ClickHouseType.Time64) {
return ClickHouseDataType.Time64;
}
if (inner instanceof ClickHouseType.Map) {
return ClickHouseDataType.Map;
}
if (inner instanceof ClickHouseType.Point) {
return ClickHouseDataType.Point;
}
if (inner instanceof ClickHouseType.Ring) {
return ClickHouseDataType.Ring;
}
if (inner instanceof ClickHouseType.Polygon) {
return ClickHouseDataType.Polygon;
}
if (inner instanceof ClickHouseType.MultiPolygon) {
return ClickHouseDataType.MultiPolygon;
}
if (inner instanceof ClickHouseType.Nested) {
return ClickHouseDataType.Nested;
}
if (inner instanceof ClickHouseType.JSON) {
return ClickHouseDataType.JSON;
}
if (inner instanceof ClickHouseType.Variant) {
return ClickHouseDataType.Variant;
}
if (inner instanceof ClickHouseType.Dynamic) {
return ClickHouseDataType.Dynamic;
}
if (inner instanceof ClickHouseType.AggregateFunctionType) {
return ClickHouseDataType.AggregateFunction;
}
if (inner instanceof ClickHouseType.SimpleAggregateFunctionType) {
return ClickHouseDataType.SimpleAggregateFunction;
}
if (inner instanceof ClickHouseType.IntervalType) {
return ClickHouseDataType.IntervalSecond;
}
return ClickHouseDataType.Nothing;
}
public static ClickHouseLancerDataType getRandom() {
return getRandom(null);
}
public static ClickHouseLancerDataType getRandom(ClickHouseGlobalState state) {
ClickHouseOptions opts = state == null ? null : state.getDbmsSpecificOptions();
boolean enableNullable = opts != null && opts.enableNullable;
boolean enableLowCardinality = opts != null && opts.enableLowCardinality;
boolean enableArray = opts != null && opts.enableArrayJoin;
ClickHouseType picked = pickScalarType();
if (enableNullable && Randomly.getBooleanWithSmallProbability() && Nullable.canWrap(picked)) {
picked = new Nullable(picked);
}
if (enableArray && Randomly.getBooleanWithSmallProbability() && Array.canWrap(picked)) {
picked = new Array(picked);
}
if (enableLowCardinality && Randomly.getBooleanWithSmallProbability() && LowCardinality.canWrap(picked)) {
picked = new LowCardinality(picked);
}
return new ClickHouseLancerDataType(picked);
}
private static ClickHouseType pickSimpleAggregateFunctionType() {
String func = Randomly.fromOptions("sum", "min", "max");
Kind kind;
if (func.equals("sum")) {
kind = Randomly.fromOptions(Kind.Int64, Kind.UInt64);
} else {
kind = Randomly.fromOptions(Kind.Int32, Kind.Int64, Kind.UInt32, Kind.UInt64, Kind.Float64);
}
return new ClickHouseType.SimpleAggregateFunctionType(func, new Primitive(kind));
}
private static Kind pickPrimitiveKind() {
int r = (int) Randomly.getNotCachedInteger(0, 5);
switch (r) {
case 0:
return Kind.Int32;
case 1:
return Kind.String;
case 2:
return Kind.UInt64;
case 3:
return Kind.Float64;
default:
return Kind.Date;
}
}
private static ClickHouseType pickScalarType() {
int roll = (int) Randomly.getNotCachedInteger(0, 101);
if (roll < 20) {
return new Primitive(Kind.Int32);
}
if (roll < 35) {
return new Primitive(Kind.String);
}
if (roll < 47) {
return new Primitive(Kind.UInt32);
}
if (roll < 57) {
return new Primitive(Kind.UInt64);
}
if (roll < 63) {
return new Primitive(Kind.Date);
}
if (roll < 69) {
return new Primitive(Kind.DateTime);
}
if (roll < 73) {
return new Primitive(Kind.Int64);
}
if (roll < 77) {
return new Primitive(Kind.Int8);
}
if (roll < 81) {
return new Primitive(Kind.UInt8);
}
if (roll < 84) {
return new Primitive(Kind.Float32);
}
if (roll < 87) {
return new Primitive(Kind.Float64);
}
if (roll < 89) {
return new Primitive(Kind.Bool);
}
if (roll < 91) {
return new FixedString(1 + (int) Randomly.getNotCachedInteger(0, 16));
}
if (roll < 92) {
if (Randomly.getBooleanWithSmallProbability()) {
int p256 = 39 + (int) Randomly.getNotCachedInteger(0, 38);
int s256 = (int) Randomly.getNotCachedInteger(0, p256 + 1);
return new Decimal(p256, s256);
}
int p = 1 + (int) Randomly.getNotCachedInteger(0, Randomly.getBoolean() ? 18 : 38);
int s = (int) Randomly.getNotCachedInteger(0, p + 1);
return new Decimal(p, s);
}
if (roll < 93) {
return new DateTime64Type((int) Randomly.getNotCachedInteger(0, 7));
}
if (roll < 94) {
if (Randomly.getBoolean()) {
return new ClickHouseType.Time();
}
return new ClickHouseType.Time64((int) Randomly.getNotCachedInteger(0, 7));
}
if (roll < 95) {
return new Primitive(Kind.Date32);
}
if (roll < 96) {
return pickSimpleAggregateFunctionType();
}
if (roll < 97) {
return new Primitive(Kind.UInt16);
}
if (roll < 98) {
return new Primitive(Randomly.getBoolean() ? Kind.UInt128 : Kind.UInt256);
}
if (roll < 99) {
return new Primitive(Randomly.fromOptions(Kind.IPv4, Kind.IPv6, Kind.UUID));
}
if (roll < 100) {
return new Primitive(Randomly.getBoolean() ? Kind.Int128 : Kind.Int256);
}
int entryCount = 2 + (int) Randomly.getNotCachedInteger(0, 4);
java.util.List<ClickHouseType.EnumEntry> entries = new java.util.ArrayList<>();
java.util.Set<Integer> usedValues = new java.util.HashSet<>();
int width = Randomly.getBoolean() ? 8 : 16;
int valueBound = width == 8 ? 127 : 32767;
for (int i = 0; i < entryCount; i++) {
int v;
do {
v = (int) Randomly.getNotCachedInteger(0, valueBound + 1);
} while (!usedValues.add(v));
entries.add(new ClickHouseType.EnumEntry("e" + i, v));
}
return new ClickHouseType.Enum(width, entries);
}
public ClickHouseType getTypeTerm() {
return typeTerm;
}
public ClickHouseDataType getType() {
return clickHouseType;
}
@Override
public String toString() {
return textRepr;
}
}
public static class ClickHouseColumn extends AbstractTableColumn<ClickHouseTable, ClickHouseLancerDataType> {
private final boolean isAlias;
private final boolean isMaterialized;
public ClickHouseColumn(String name, ClickHouseLancerDataType columnType, boolean isAlias,
boolean isMaterialized, ClickHouseTable table) {
super(name, table, columnType);
this.isAlias = isAlias;
this.isMaterialized = isMaterialized;
}
public static ClickHouseSchema.ClickHouseColumn createDummy(String name, ClickHouseTable table) {
return createDummy(name, table, null);
}
public static ClickHouseSchema.ClickHouseColumn createDummy(String name, ClickHouseTable table,
ClickHouseGlobalState state) {
return new ClickHouseSchema.ClickHouseColumn(name, ClickHouseLancerDataType.getRandom(state), false, false,
table);
}
public boolean isAlias() {
return isAlias;
}
public boolean isMaterialized() {
return isMaterialized;
}
public ClickHouseColumnReference asColumnReference(String tableAlias) {
return new ClickHouseColumnReference(this, null, tableAlias);
}
}
public static ClickHouseConstant getConstant(ResultSet randomRowValues, int columnIndex,
ClickHouseDataType valueType) throws SQLException {
if (randomRowValues.getString(columnIndex) == null) {
return ClickHouseCreateConstant.createNullConstant();
}
switch (valueType) {
case Int8:
return ClickHouseCreateConstant.createInt8Constant(randomRowValues.getLong(columnIndex));
case Int16:
return ClickHouseCreateConstant.createInt16Constant(randomRowValues.getLong(columnIndex));
case Int32:
return ClickHouseCreateConstant.createInt32Constant(randomRowValues.getLong(columnIndex));
case Int64:
return ClickHouseCreateConstant
.createInt64Constant(java.math.BigInteger.valueOf(randomRowValues.getLong(columnIndex)));
case UInt8:
return ClickHouseCreateConstant.createUInt8Constant(randomRowValues.getLong(columnIndex));
case UInt16:
return ClickHouseCreateConstant.createUInt16Constant(randomRowValues.getLong(columnIndex));
case UInt32:
return ClickHouseCreateConstant.createUInt32Constant(randomRowValues.getLong(columnIndex));
case UInt64:
return ClickHouseCreateConstant
.createUInt64Constant(java.math.BigInteger.valueOf(randomRowValues.getLong(columnIndex)));
case Float32:
return ClickHouseCreateConstant.createFloat32Constant(randomRowValues.getFloat(columnIndex));
case Float64:
return ClickHouseCreateConstant.createFloat64Constant(randomRowValues.getDouble(columnIndex));
case Bool:
return ClickHouseCreateConstant.createBoolean(randomRowValues.getBoolean(columnIndex));
case String:
case FixedString:
return ClickHouseCreateConstant.createStringConstant(randomRowValues.getString(columnIndex));
case Date:
case Date32:
case DateTime:
case DateTime32:
case DateTime64:
return ClickHouseCreateConstant.createStringConstant(randomRowValues.getString(columnIndex));
default:
throw new IgnoreMeException();
}
}
public static class ClickHouseRowValue
extends AbstractRowValue<ClickHouseTables, ClickHouseColumn, ClickHouseConstant> {
public ClickHouseRowValue(ClickHouseSchema.ClickHouseTables tables,
Map<ClickHouseSchema.ClickHouseColumn, ClickHouseConstant> values) {
super(tables, values);
}
}
public static class ClickHouseTables extends AbstractTables<ClickHouseTable, ClickHouseColumn> {
public ClickHouseTables(List<ClickHouseSchema.ClickHouseTable> tables) {
super(tables);
}
}
public ClickHouseSchema(List<ClickHouseTable> databaseTables) {
super(databaseTables);
}
public ClickHouseTables getRandomTableNonEmptyTables() {
List<ClickHouseTable> stable = getTablesStableForRepeatedReads();
if (stable.isEmpty()) {
throw new IgnoreMeException();
}
return new ClickHouseTables(Randomly.nonEmptySubset(stable));
}
public List<ClickHouseTable> getTablesStableForRepeatedReads() {
List<ClickHouseTable> stable = new ArrayList<>();
for (ClickHouseTable t : getDatabaseTables()) {
if (t.isStableForRepeatedReads()) {
stable.add(t);
}
}
return stable;
}
private static ClickHouseLancerDataType getColumnType(String typeString) {
return new ClickHouseLancerDataType(typeString);
}
public static class ClickHouseTable
extends AbstractRelationalTable<ClickHouseColumn, TableIndex, ClickHouseGlobalState> {
private final String engine;
private final String samplingKey;
public ClickHouseTable(String tableName, List<ClickHouseColumn> columns, List<TableIndex> indexes,
boolean isView) {
this(tableName, columns, indexes, isView, "");
}
public ClickHouseTable(String tableName, List<ClickHouseColumn> columns, List<TableIndex> indexes,
boolean isView, String engine) {
this(tableName, columns, indexes, isView, engine, "");
}
public ClickHouseTable(String tableName, List<ClickHouseColumn> columns, List<TableIndex> indexes,
boolean isView, String engine, String samplingKey) {
super(tableName, columns, indexes, isView);
this.engine = engine == null ? "" : engine;
this.samplingKey = samplingKey == null ? "" : samplingKey;
}
public String getEngine() {
return engine;
}
public String getSamplingKey() {
return samplingKey;
}
public boolean hasSamplingKey() {
return !samplingKey.isEmpty();
}
public boolean supportsFinal() {
return engine.equals("ReplacingMergeTree") || engine.equals("SummingMergeTree")
|| engine.equals("AggregatingMergeTree") || engine.equals("CollapsingMergeTree")
|| engine.equals("VersionedCollapsingMergeTree");
}
public boolean isStableForRepeatedReads() {
return !supportsFinal();
}
}
public static ClickHouseSchema fromConnection(SQLConnection con, String databaseName) throws SQLException {
List<ClickHouseTable> databaseTables = new ArrayList<>();
List<String> tableNames = getTableNames(con);
java.util.Map<String, TableMeta> metaByName = getTableMeta(con, databaseName);
for (String tableName : tableNames) {
List<ClickHouseColumn> databaseColumns = getTableColumns(con, tableName);
List<TableIndex> indexes = Collections.emptyList();
boolean isView = matchesViewName(tableName);
TableMeta meta = metaByName.getOrDefault(tableName, TableMeta.EMPTY);
ClickHouseTable t = new ClickHouseTable(tableName, databaseColumns, indexes, isView, meta.engine,
meta.samplingKey);
for (ClickHouseColumn c : databaseColumns) {
c.setTable(t);
}
databaseTables.add(t);
}
return new ClickHouseSchema(databaseTables);
}
private static final class TableMeta {
static final TableMeta EMPTY = new TableMeta("", "");
final String engine;
final String samplingKey;
TableMeta(String engine, String samplingKey) {
this.engine = engine == null ? "" : engine;
this.samplingKey = samplingKey == null ? "" : samplingKey;
}
}
private static java.util.Map<String, TableMeta> getTableMeta(SQLConnection con, String databaseName)
throws SQLException {
java.util.Map<String, TableMeta> meta = new java.util.HashMap<>();
try (Statement s = con.createStatement()) {
String q = "SELECT name, engine, sampling_key FROM system.tables WHERE database = '"
+ databaseName.replace("'", "''") + "'";
try (ResultSet rs = s.executeQuery(q)) {
while (rs.next()) {
meta.put(rs.getString(1), new TableMeta(rs.getString(2), rs.getString(3)));
}
}
}
return meta;
}
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<ClickHouseColumn> getTableColumns(SQLConnection con, String tableName) throws SQLException {
List<ClickHouseColumn> columns = new ArrayList<>();
List<ClickHouseColumn> ephemeral = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("DESCRIBE " + tableName)) {
while (rs.next()) {
String columnName = rs.getString("name");
String dataType = rs.getString("type");
String defaultType = rs.getString("default_type");
boolean isAlias = "ALIAS".compareTo(defaultType) == 0;
boolean isMaterialized = "MATERIALIZED".compareTo(defaultType) == 0;
ClickHouseColumn c = new ClickHouseColumn(columnName, getColumnType(dataType), isAlias,
isMaterialized, null);
if ("EPHEMERAL".compareTo(defaultType) == 0) {
ephemeral.add(c);
} else {
columns.add(c);
}
}
}
}
return columns.isEmpty() ? ephemeral : columns;
}
}