-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathDorisSchema.java
More file actions
619 lines (558 loc) · 22.4 KB
/
DorisSchema.java
File metadata and controls
619 lines (558 loc) · 22.4 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package sqlancer.doris;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.SQLConnection;
import sqlancer.common.DBMSCommon;
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;
import sqlancer.doris.DorisProvider.DorisGlobalState;
import sqlancer.doris.DorisSchema.DorisTable;
import sqlancer.doris.ast.DorisConstant;
public class DorisSchema extends AbstractSchema<DorisGlobalState, DorisTable> {
public enum DorisTableDataModel {
UNIQUE, AGGREGATE, DUPLICATE;
public static DorisTableDataModel getRandom() {
List<DorisTableDataModel> validOptions = new ArrayList<>(Arrays.asList(values()));
if (DorisBugs.bug36072) {
validOptions.remove(AGGREGATE);
}
if (DorisBugs.bug36343) {
validOptions.remove(UNIQUE);
}
return Randomly.fromList(validOptions);
}
}
public enum DorisColumnAggrType {
SUM, MIN, MAX, REPLACE, REPLCAE_IF_NOT_NULL, BITMAP_UNION, HLL_UNION, NULL;
public static DorisColumnAggrType getRandom(DorisCompositeDataType columnDataType) {
// if (columnDataType.getPrimitiveDataType() == DorisSchema.DorisDataType.BITMAP) {
// return DorisColumnAggrType.BITMAP_UNION;
// }
// if (columnDataType.getPrimitiveDataType() == DorisSchema.DorisDataType.HLL) {
// return DorisColumnAggrType.HLL_UNION;
// }
return Randomly.fromOptions(SUM, MIN, MAX, REPLACE, REPLCAE_IF_NOT_NULL);
}
}
public enum DorisDataType {
INT, FLOAT, DECIMAL, DATE, DATETIME, VARCHAR, BOOLEAN, NULL;
// HLL, BITMAP, ARRAY;
private int decimalScale;
private int decimalPrecision;
private int varcharLength;
public static DorisDataType getRandomWithoutNull() {
DorisDataType dt;
do {
dt = Randomly.fromOptions(values());
} while (dt == DorisDataType.NULL);
return dt;
}
public int getDecimalScale() {
return decimalScale;
}
public void setDecimalScale(int decimalScale) {
this.decimalScale = decimalScale;
}
public int getDecimalPrecision() {
return decimalPrecision;
}
public void setDecimalPrecision(int decimalPrecision) {
this.decimalPrecision = decimalPrecision;
}
public int getVarcharLength() {
return varcharLength;
}
public void setVarcharLength(int varcharLength) {
this.varcharLength = varcharLength;
}
}
public static class DorisCompositeDataType {
private final DorisDataType dataType;
private final int size;
public DorisCompositeDataType(DorisDataType dataType, int size) {
this.dataType = dataType;
this.size = size;
}
public DorisDataType getPrimitiveDataType() {
return dataType;
}
public int getSize() {
if (size == -1) {
throw new AssertionError(this);
}
return size;
}
public static DorisCompositeDataType getRandomWithoutNull() {
DorisDataType type = DorisDataType.getRandomWithoutNull();
int size = -1;
switch (type) {
case INT:
size = Randomly.fromOptions(1, 2, 4, 8, 16);
break;
case FLOAT:
size = Randomly.fromOptions(4, 12);
break;
case DECIMAL:
size = Randomly.fromOptions(1, 3); // DECIMAL or DECIMALV3
break;
case DATE:
case DATETIME:
case VARCHAR:
case BOOLEAN:
// case HLL:
// case BITMAP:
// case ARRAY:
size = 0;
break;
default:
throw new AssertionError(type);
}
return new DorisCompositeDataType(type, size);
}
public void initColumnArgs() {
Randomly r = new Randomly();
int scale;
int precision;
int varcharLength;
switch (getPrimitiveDataType()) {
case DECIMAL:
if (getPrimitiveDataType().getDecimalPrecision() != 0) {
break;
}
if (size == 1) {
scale = r.getInteger(0, 9);
precision = r.getInteger(scale + 1, scale + 18);
getPrimitiveDataType().setDecimalPrecision(precision);
getPrimitiveDataType().setDecimalScale(scale);
} else if (size == 3) {
precision = r.getInteger(1, 38);
scale = r.getInteger(0, precision);
getPrimitiveDataType().setDecimalPrecision(precision);
getPrimitiveDataType().setDecimalScale(scale);
} else {
throw new AssertionError(size);
}
break;
case VARCHAR:
if (getPrimitiveDataType().getVarcharLength() != 0) {
break;
}
varcharLength = r.getInteger(1, 255);
getPrimitiveDataType().setVarcharLength(varcharLength);
break;
default:
// pass
}
}
@Override
public String toString() {
switch (getPrimitiveDataType()) {
case INT:
switch (size) {
case 16:
return "LARGEINT";
case 8:
return "BIGINT";
case 4:
return "INT";
case 2:
return "SMALLINT";
case 1:
return "TINYINT";
default:
throw new AssertionError(size);
}
case FLOAT:
switch (size) {
case 12:
return "DOUBLE";
case 4:
return "FLOAT";
default:
throw new AssertionError(size);
}
case DECIMAL:
switch (size) {
case 1:
return "DECIMAL(" + getPrimitiveDataType().getDecimalPrecision() + ","
+ getPrimitiveDataType().getDecimalScale() + ")";
case 3:
return "DECIMALV3(" + getPrimitiveDataType().getDecimalPrecision() + ","
+ getPrimitiveDataType().getDecimalScale() + ")";
default:
throw new AssertionError(size);
}
case DATE:
return "DATEV2";
case DATETIME:
return Randomly.fromOptions("DATETIME", "DATETIMEV2");
case VARCHAR:
return Randomly.fromOptions("VARCHAR", "CHAR") + "(" + getPrimitiveDataType().getVarcharLength() + ")";
case BOOLEAN:
return "BOOLEAN";
// case HLL:
// return "HLL";
// case BITMAP:
// return "BITMAP";
// case ARRAY:
// return "ARRAY";
case NULL:
return Randomly.fromOptions("NULL");
default:
throw new AssertionError(getPrimitiveDataType());
}
}
public boolean canBeKey() {
switch (dataType) {
// case HLL:
// case BITMAP:
// case ARRAY:
case FLOAT:
return false;
default:
return true;
}
}
}
public static class DorisColumn extends AbstractTableColumn<DorisTable, DorisCompositeDataType> {
private final boolean isKey;
private final boolean isNullable;
private final DorisColumnAggrType aggrType;
private final boolean hasDefaultValue;
private final String defaultValue;
public DorisColumn(String name, DorisCompositeDataType type, boolean isKey, boolean isNullable,
DorisColumnAggrType aggrType, boolean hasDefaultValue, String defaultValue) {
super(name, null, type);
this.isKey = isKey;
this.isNullable = isNullable;
this.aggrType = aggrType;
this.hasDefaultValue = hasDefaultValue;
this.defaultValue = defaultValue;
}
public DorisColumn(String name, DorisCompositeDataType type, boolean isKey, boolean isNullable) {
super(name, null, type);
this.isKey = isKey;
this.isNullable = isNullable;
this.aggrType = DorisColumnAggrType.NULL;
this.hasDefaultValue = false;
this.defaultValue = "";
}
public boolean isKey() {
return isKey;
}
public boolean isNullable() {
return isNullable;
}
public boolean hasDefaultValue() {
return hasDefaultValue;
}
@Override
public String toString() {
String ret = this.getName() + " " + this.getType();
if (aggrType != DorisColumnAggrType.NULL) {
ret += " " + aggrType.name();
}
if (!isNullable) {
ret += " NOT NULL";
}
if (hasDefaultValue) {
ret += " DEFAULT " + defaultValue;
}
return ret;
}
@Override
public int compareTo(AbstractTableColumn<DorisTable, DorisCompositeDataType> o) {
// To sort columns
DorisColumn other = (DorisColumn) o;
if (isKey != other.isKey) {
return isKey ? 1 : -1;
}
return getName().compareTo(other.getName());
}
}
public static class DorisTables extends AbstractTables<DorisTable, DorisColumn> {
public DorisTables(List<DorisTable> tables) {
super(tables);
}
public DorisRowValue getRandomRowValue(SQLConnection con) throws SQLException {
String rowValueQuery = String.format("SELECT %s FROM %s ORDER BY 1 LIMIT 1", columnNamesAsString(
c -> c.getTable().getName() + "." + c.getName() + " AS " + c.getTable().getName() + c.getName()),
tableNamesAsString());
Map<DorisColumn, DorisConstant> values = new HashMap<>();
try (Statement s = con.createStatement()) {
ResultSet rs = s.executeQuery(rowValueQuery);
if (!rs.next()) {
throw new IgnoreMeException();
// throw new AssertionError("could not find random row " + rowValueQuery + "\n");
}
for (int i = 0; i < getColumns().size(); i++) {
DorisColumn column = getColumns().get(i);
int columnIndex = rs.findColumn(column.getTable().getName() + column.getName());
assert columnIndex == i + 1;
DorisConstant constant;
if (rs.getString(columnIndex) == null) {
constant = DorisConstant.createNullConstant();
} else {
switch (column.getType().getPrimitiveDataType()) {
case INT:
constant = DorisConstant.createIntConstant(rs.getLong(columnIndex));
break;
case FLOAT:
case DECIMAL:
constant = DorisConstant.createFloatConstant(rs.getDouble(columnIndex));
break;
case DATE:
constant = DorisConstant.createDateConstant(rs.getString(columnIndex));
break;
case DATETIME:
constant = DorisConstant.createDatetimeConstant(rs.getString(columnIndex));
break;
case VARCHAR:
constant = DorisConstant.createStringConstant(rs.getString(columnIndex));
break;
case BOOLEAN:
constant = DorisConstant.createBooleanConstant(rs.getBoolean(columnIndex));
break;
case NULL:
constant = DorisConstant.createNullConstant();
break;
default:
throw new IgnoreMeException();
}
}
values.put(column, constant);
}
assert !rs.next();
return new DorisRowValue(this, values);
} catch (SQLException e) {
throw new IgnoreMeException();
}
}
}
public static class DorisRowValue extends AbstractRowValue<DorisTables, DorisColumn, DorisConstant> {
DorisRowValue(DorisTables tables, Map<DorisColumn, DorisConstant> values) {
super(tables, values);
}
}
public DorisSchema(List<DorisTable> databaseTables) {
super(databaseTables);
}
public DorisTables getRandomTableNonEmptyTables() {
return new DorisTables(Randomly.nonEmptySubset(getDatabaseTables()));
}
public DorisTables getRandomTableNonEmptyAndViewTables() {
List<DorisTable> tables = getDatabaseTables().stream().filter(t -> !t.isView()).collect(Collectors.toList());
tables = Randomly.nonEmptySubset(tables);
return new DorisTables(tables);
}
public int getIndexCount() {
int count = 0;
for (DorisTable table : getDatabaseTables()) {
count += table.getIndexes().size();
}
return count;
}
private static DorisCompositeDataType getColumnType(String typeString) {
DorisDataType primitiveType;
int size = -1;
if (typeString.startsWith("DECIMALV3")) {
primitiveType = DorisDataType.DECIMAL;
String precisionAndScale = typeString.substring(typeString.indexOf('(') + 1, typeString.indexOf(')'));
String[] split = precisionAndScale.split(",");
assert split.length == 2;
primitiveType.setDecimalPrecision(Integer.parseInt(split[0].trim()));
primitiveType.setDecimalScale(Integer.parseInt(split[1].trim()));
size = 3;
} else if (typeString.startsWith("DECIMAL")) {
primitiveType = DorisDataType.DECIMAL;
String precisionAndScale = typeString.substring(typeString.indexOf('(') + 1, typeString.indexOf(')'));
String[] split = precisionAndScale.split(",");
assert split.length == 2;
primitiveType.setDecimalPrecision(Integer.parseInt(split[0].trim()));
primitiveType.setDecimalScale(Integer.parseInt(split[1].trim()));
size = 1;
} else if (typeString.startsWith("DATEV2")) {
primitiveType = DorisDataType.DATE;
size = 2;
} else if (typeString.startsWith("DATE")) {
primitiveType = DorisDataType.DATE;
size = 1;
} else if (typeString.startsWith("DATETIMEV2")) {
primitiveType = DorisDataType.DATETIME;
size = 2;
} else if (typeString.startsWith("DATETIME")) {
primitiveType = DorisDataType.DATETIME;
size = 1;
} else if (typeString.startsWith("CHAR") || typeString.startsWith("VARCHAR")) {
primitiveType = DorisDataType.VARCHAR;
String varcharLength = typeString.substring(typeString.indexOf('(') + 1, typeString.indexOf(')'));
primitiveType.setVarcharLength(Integer.parseInt(varcharLength.trim()));
} else {
switch (typeString) {
case "LARGEINT":
primitiveType = DorisDataType.INT;
size = 16;
break;
case "BIGINT":
primitiveType = DorisDataType.INT;
size = 8;
break;
case "INT":
primitiveType = DorisDataType.INT;
size = 4;
break;
case "SMALLINT":
primitiveType = DorisDataType.INT;
size = 2;
break;
case "TINYINT":
primitiveType = DorisDataType.INT;
size = 1;
break;
case "DOUBLE":
primitiveType = DorisDataType.FLOAT;
size = 12;
break;
case "FLOAT":
primitiveType = DorisDataType.FLOAT;
size = 4;
break;
case "DECIMAL":
case "DECIMAL(*,*)":
primitiveType = DorisDataType.DECIMAL;
size = 1;
break;
case "DECIMALV3":
case "DECIMALV3(*,*)":
primitiveType = DorisDataType.DECIMAL;
size = 3;
break;
case "CHAR":
case "CHAR(*)":
case "VARCHAR":
case "VARCHAR(*)":
primitiveType = DorisDataType.VARCHAR;
break;
case "DATE":
primitiveType = DorisDataType.DATE;
size = 1;
break;
case "DATEV2":
primitiveType = DorisDataType.DATE;
size = 2;
break;
case "DATETIME":
primitiveType = DorisDataType.DATETIME;
size = 1;
break;
case "DATETIMEV2":
primitiveType = DorisDataType.DATETIME;
size = 2;
break;
case "BOOLEAN":
primitiveType = DorisDataType.BOOLEAN;
break;
// case "HLL":
// primitiveType = DorisDataType.HLL;
// break;
// case "BITMAP":
// primitiveType = DorisDataType.BITMAP;
// break;
case "NULL":
primitiveType = DorisDataType.NULL;
break;
default:
throw new AssertionError(typeString);
}
}
return new DorisCompositeDataType(primitiveType, size);
}
public static class DorisTable extends AbstractRelationalTable<DorisColumn, TableIndex, DorisGlobalState> {
public DorisTable(String tableName, List<DorisColumn> columns, boolean isView) {
super(tableName, columns, Collections.emptyList(), isView);
}
public List<DorisColumn> getRandomNonEmptyInsertColumns() {
List<DorisColumn> columns = getColumns();
List<DorisColumn> retColumns = new ArrayList<>();
List<DorisColumn> remainColumns = new ArrayList<>();
for (DorisColumn column : columns) {
if (!column.hasDefaultValue() && !column.isNullable) {
retColumns.add(column);
} else {
remainColumns.add(column);
}
}
if (retColumns.isEmpty()) {
retColumns.addAll(Randomly.nonEmptySubset(remainColumns));
} else {
retColumns.addAll(Randomly.subset(remainColumns));
}
return retColumns;
}
}
public static DorisSchema fromConnection(SQLConnection con, String databaseName) throws SQLException {
List<DorisTable> databaseTables = new ArrayList<>();
List<String> tableNames = getTableNames(con);
for (String tableName : tableNames) {
if (DBMSCommon.matchesIndexName(tableName)) {
continue;
}
List<DorisColumn> databaseColumns = getTableColumns(con, tableName);
boolean isView = tableName.startsWith("v");
DorisTable t = new DorisTable(tableName, databaseColumns, isView);
for (DorisColumn c : databaseColumns) {
c.setTable(t);
}
databaseTables.add(t);
}
return new DorisSchema(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("SHOW TABLES")) {
while (rs.next()) {
tableNames.add(rs.getString(1));
}
}
}
return tableNames;
}
private static List<DorisColumn> getTableColumns(SQLConnection con, String tableName) throws SQLException {
List<DorisColumn> columns = new ArrayList<>();
try (Statement s = con.createStatement()) {
try (ResultSet rs = s.executeQuery("DESCRIBE " + tableName)) {
while (rs.next()) {
String columnName = rs.getString("Field");
String dataType = rs.getString("Type");
String isNullString = rs.getString("Null");
assert isNullString.contentEquals("Yes") || isNullString.contentEquals("No");
boolean isNullable = isNullString.contentEquals("Yes");
String isKeyString = rs.getString("Key");
assert isKeyString.contentEquals("true") || isKeyString.contentEquals("false");
boolean isKey = isKeyString.contentEquals("true");
String defaultValue = rs.getString("Default");
boolean hasDefaultValue = defaultValue != null;
DorisColumn c = new DorisColumn(columnName, getColumnType(dataType), isKey, isNullable,
DorisColumnAggrType.NULL, hasDefaultValue, defaultValue);
columns.add(c);
}
}
}
return columns;
}
}