-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathTiDBExpressionGenerator.java
More file actions
379 lines (345 loc) · 15.1 KB
/
TiDBExpressionGenerator.java
File metadata and controls
379 lines (345 loc) · 15.1 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
package sqlancer.tidb;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.common.gen.CERTGenerator;
import sqlancer.common.gen.TLPWhereGenerator;
import sqlancer.common.gen.UntypedExpressionGenerator;
import sqlancer.common.schema.AbstractTables;
import sqlancer.tidb.TiDBProvider.TiDBGlobalState;
import sqlancer.tidb.TiDBSchema.TiDBColumn;
import sqlancer.tidb.TiDBSchema.TiDBCompositeDataType;
import sqlancer.tidb.TiDBSchema.TiDBDataType;
import sqlancer.tidb.TiDBSchema.TiDBTable;
import sqlancer.tidb.ast.TiDBAggregate;
import sqlancer.tidb.ast.TiDBAggregate.TiDBAggregateFunction;
import sqlancer.tidb.ast.TiDBBinaryBitOperation;
import sqlancer.tidb.ast.TiDBBinaryBitOperation.TiDBBinaryBitOperator;
import sqlancer.tidb.ast.TiDBBinaryComparisonOperation;
import sqlancer.tidb.ast.TiDBBinaryComparisonOperation.TiDBComparisonOperator;
import sqlancer.tidb.ast.TiDBBinaryLogicalOperation;
import sqlancer.tidb.ast.TiDBBinaryLogicalOperation.TiDBBinaryLogicalOperator;
import sqlancer.tidb.ast.TiDBCase;
import sqlancer.tidb.ast.TiDBCastOperation;
import sqlancer.tidb.ast.TiDBColumnReference;
import sqlancer.tidb.ast.TiDBConstant;
import sqlancer.tidb.ast.TiDBExpression;
import sqlancer.tidb.ast.TiDBFunctionCall;
import sqlancer.tidb.ast.TiDBFunctionCall.TiDBFunction;
import sqlancer.tidb.ast.TiDBJoin;
import sqlancer.tidb.ast.TiDBJoin.JoinType;
import sqlancer.tidb.ast.TiDBOrderingTerm;
import sqlancer.tidb.ast.TiDBRegexOperation;
import sqlancer.tidb.ast.TiDBRegexOperation.TiDBRegexOperator;
import sqlancer.tidb.ast.TiDBSelect;
import sqlancer.tidb.ast.TiDBTableReference;
import sqlancer.tidb.ast.TiDBUnaryPostfixOperation;
import sqlancer.tidb.ast.TiDBUnaryPostfixOperation.TiDBUnaryPostfixOperator;
import sqlancer.tidb.ast.TiDBUnaryPrefixOperation;
import sqlancer.tidb.ast.TiDBUnaryPrefixOperation.TiDBUnaryPrefixOperator;
public class TiDBExpressionGenerator extends UntypedExpressionGenerator<TiDBExpression, TiDBColumn>
implements TLPWhereGenerator<TiDBSelect, TiDBJoin, TiDBExpression, TiDBTable, TiDBColumn>,
CERTGenerator<TiDBSelect, TiDBJoin, TiDBExpression, TiDBTable, TiDBColumn> {
private enum Gen {
UNARY_PREFIX, //
UNARY_POSTFIX, //
CONSTANT, //
COLUMN, //
COMPARISON, REGEX, FUNCTION, BINARY_LOGICAL, BINARY_BIT, CAST, DEFAULT, CASE
// BINARY_ARITHMETIC
}
private final TiDBGlobalState globalState;
private List<TiDBTable> tables;
public TiDBExpressionGenerator(TiDBGlobalState globalState) {
this.globalState = globalState;
}
@Override
public TiDBExpression generateConstant() {
TiDBDataType type = TiDBDataType.getRandom();
if (Randomly.getBooleanWithRatherLowProbability()) {
return TiDBConstant.createNullConstant();
}
switch (type) {
case INT:
return TiDBConstant.createIntConstant(globalState.getRandomly().getInteger());
case BLOB:
case TEXT:
return TiDBConstant.createStringConstant(globalState.getRandomly().getString());
case BOOL:
return TiDBConstant.createBooleanConstant(Randomly.getBoolean());
case FLOATING:
return TiDBConstant.createFloatConstant(globalState.getRandomly().getDouble());
case CHAR:
return TiDBConstant.createStringConstant(globalState.getRandomly().getChar());
case DECIMAL:
case NUMERIC:
return TiDBConstant.createIntConstant(globalState.getRandomly().getInteger());
default:
throw new AssertionError();
}
}
@Override
public List<TiDBExpression> generateOrderBys() {
List<TiDBExpression> expressions = super.generateOrderBys();
List<TiDBExpression> newExpressions = new ArrayList<>();
for (TiDBExpression expr : expressions) {
TiDBExpression newExpr = expr;
if (Randomly.getBoolean()) {
newExpr = new TiDBOrderingTerm(expr, Randomly.getBoolean());
}
newExpressions.add(newExpr);
}
return newExpressions;
}
@Override
public TiDBExpression negatePredicate(TiDBExpression predicate) {
return new TiDBUnaryPrefixOperation(predicate, TiDBUnaryPrefixOperator.NOT);
}
@Override
public TiDBExpression isNull(TiDBExpression expr) {
return new TiDBUnaryPostfixOperation(expr, TiDBUnaryPostfixOperator.IS_NULL);
}
public TiDBExpression generateConstant(TiDBDataType type) {
if (Randomly.getBooleanWithRatherLowProbability()) {
return TiDBConstant.createNullConstant();
}
switch (type) {
case INT:
return TiDBConstant.createIntConstant(globalState.getRandomly().getInteger());
case BLOB:
case TEXT:
return TiDBConstant.createStringConstant(globalState.getRandomly().getString());
case BOOL:
return TiDBConstant.createBooleanConstant(Randomly.getBoolean());
case FLOATING:
return TiDBConstant.createFloatConstant(globalState.getRandomly().getDouble());
case CHAR:
return TiDBConstant.createStringConstant(globalState.getRandomly().getChar());
case DECIMAL:
case NUMERIC:
return TiDBConstant.createIntConstant(globalState.getRandomly().getInteger());
default:
throw new AssertionError();
}
}
@Override
public TiDBExpressionGenerator setTablesAndColumns(AbstractTables<TiDBTable, TiDBColumn> tables) {
this.columns = tables.getColumns();
this.tables = tables.getTables();
return this;
}
@Override
public TiDBExpression generateBooleanExpression() {
return generateExpression();
}
@Override
public TiDBSelect generateSelect() {
return new TiDBSelect();
}
@Override
public List<TiDBJoin> getRandomJoinClauses() {
List<TiDBExpression> tableList = tables.stream().map(t -> new TiDBTableReference(t))
.collect(Collectors.toList());
List<TiDBJoin> joins = TiDBJoin.getJoins(tableList, globalState);
tables = tableList.stream().map(t -> ((TiDBTableReference) t).getTable()).collect(Collectors.toList());
return joins;
}
@Override
public List<TiDBExpression> getTableRefs() {
return tables.stream().map(t -> new TiDBTableReference(t)).collect(Collectors.toList());
}
@Override
public List<TiDBExpression> generateFetchColumns(boolean shouldCreateDummy) {
if (shouldCreateDummy && Randomly.getBoolean()) {
return List.of(new TiDBColumnReference(
new TiDBColumn("*", new TiDBCompositeDataType(TiDBDataType.INT), false, false, false)));
}
return Randomly.nonEmptySubset(this.columns).stream().map(c -> new TiDBColumnReference(c))
.collect(Collectors.toList());
}
@Override
protected TiDBExpression generateExpression(int depth) {
if (depth >= globalState.getOptions().getMaxExpressionDepth() || Randomly.getBoolean()) {
return generateLeafNode();
}
if (allowAggregates && Randomly.getBoolean()) {
allowAggregates = false;
TiDBAggregateFunction func = TiDBAggregateFunction.getRandom();
List<TiDBExpression> args = generateExpressions(func.getNrArgs());
return new TiDBAggregate(args, func);
}
switch (Randomly.fromOptions(Gen.values())) {
case DEFAULT:
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
throw new IgnoreMeException();
}
TiDBColumn column = Randomly.fromList(columns);
if (column.hasDefault()) {
return new TiDBFunctionCall(TiDBFunction.DEFAULT, Arrays.asList(new TiDBColumnReference(column)));
}
throw new IgnoreMeException();
case UNARY_POSTFIX:
return new TiDBUnaryPostfixOperation(generateExpression(depth + 1), TiDBUnaryPostfixOperator.getRandom());
case UNARY_PREFIX:
TiDBUnaryPrefixOperator rand = TiDBUnaryPrefixOperator.getRandom();
return new TiDBUnaryPrefixOperation(generateExpression(depth + 1), rand);
case COLUMN:
return generateColumn();
case CONSTANT:
return generateConstant();
case COMPARISON:
return new TiDBBinaryComparisonOperation(generateExpression(depth + 1), generateExpression(depth + 1),
TiDBComparisonOperator.getRandom());
case REGEX:
return new TiDBRegexOperation(generateExpression(depth + 1), generateExpression(depth + 1),
TiDBRegexOperator.getRandom());
case FUNCTION:
TiDBFunction func = TiDBFunction.getRandom();
return new TiDBFunctionCall(func, generateExpressions(func.getNrArgs(), depth));
case BINARY_BIT:
return new TiDBBinaryBitOperation(generateExpression(depth + 1), generateExpression(depth + 1),
TiDBBinaryBitOperator.getRandom());
case BINARY_LOGICAL:
return new TiDBBinaryLogicalOperation(generateExpression(depth + 1), generateExpression(depth + 1),
TiDBBinaryLogicalOperator.getRandom());
case CAST:
return new TiDBCastOperation(generateExpression(depth + 1), Randomly.fromOptions("BINARY", // https://github.com/tidb-challenge-program/bug-hunting-issue/issues/52
"CHAR", "DATE", "DATETIME", "TIME", // https://github.com/tidb-challenge-program/bug-hunting-issue/issues/13
"DECIMAL", "SIGNED", "UNSIGNED" /* https://github.com/pingcap/tidb/issues/16028 */));
case CASE:
int nr = Randomly.fromOptions(1, 2);
return new TiDBCase(generateExpression(depth + 1), generateExpressions(nr, depth + 1),
generateExpressions(nr, depth + 1), generateExpression(depth + 1));
default:
throw new AssertionError();
}
}
@Override
protected TiDBExpression generateColumn() {
TiDBColumn column = Randomly.fromList(columns);
return new TiDBColumnReference(column);
}
@Override
public String generateExplainQuery(TiDBSelect select) {
return "EXPLAIN " + select.asString();
}
@Override
public boolean mutate(TiDBSelect select) {
List<Function<TiDBSelect, Boolean>> mutators = new ArrayList<>();
mutators.add(this::mutateJoin);
mutators.add(this::mutateWhere);
if (!TiDBBugs.bug38319) {
mutators.add(this::mutateGroupBy);
mutators.add(this::mutateHaving);
}
mutators.add(this::mutateAnd);
if (!TiDBBugs.bug51525) {
mutators.add(this::mutateOr);
}
mutators.add(this::mutateLimit);
// mutators.add(this::mutateDistinct);
return Randomly.fromList(mutators).apply(select);
}
boolean mutateJoin(TiDBSelect select) {
if (select.getJoinList().isEmpty()) {
return false;
}
TiDBJoin join = (TiDBJoin) Randomly.fromList(select.getJoinList());
if (join.getJoinType() == JoinType.NATURAL) {
return false;
}
// CROSS does not need ON Condition, while other joins do
// To avoid Null pointer, generating a new new condition when mutating CROSS to
// other joins
if (join.getJoinType() == JoinType.CROSS) {
List<TiDBColumn> columns = new ArrayList<>();
columns.addAll(((TiDBTableReference) join.getLeftTable()).getTable().getColumns());
columns.addAll(((TiDBTableReference) join.getRightTable()).getTable().getColumns());
TiDBExpressionGenerator joinGen2 = new TiDBExpressionGenerator(globalState).setColumns(columns);
join.setOnCondition(joinGen2.generateExpression());
}
JoinType newJoinType = TiDBJoin.JoinType.INNER;
if (join.getJoinType() == JoinType.LEFT || join.getJoinType() == JoinType.RIGHT) { // No invarient relation
// between LEFT and RIGHT
// join
newJoinType = JoinType.getRandomExcept(JoinType.NATURAL, JoinType.LEFT, JoinType.RIGHT);
} else {
newJoinType = JoinType.getRandomExcept(JoinType.NATURAL, join.getJoinType());
}
assert newJoinType != JoinType.NATURAL; // Natural Join is not supported for CERT
boolean increase = join.getJoinType().ordinal() < newJoinType.ordinal();
join.setJoinType(newJoinType);
if (newJoinType == JoinType.CROSS) {
join.setOnCondition(null);
}
return increase;
}
boolean mutateWhere(TiDBSelect select) {
boolean increase = select.getWhereClause() != null;
if (increase) {
select.setWhereClause(null);
} else {
select.setWhereClause(generateExpression());
}
return increase;
}
boolean mutateHaving(TiDBSelect select) {
if (select.getGroupByExpressions().isEmpty()) {
select.setGroupByExpressions(select.getFetchColumns());
select.setHavingClause(generateExpression());
return false;
} else {
if (select.getHavingClause() == null) {
select.setHavingClause(generateExpression());
return false;
} else {
select.setHavingClause(null);
return true;
}
}
}
boolean mutateAnd(TiDBSelect select) {
if (select.getWhereClause() == null) {
select.setWhereClause(generateExpression());
} else {
TiDBExpression newWhere = new TiDBBinaryLogicalOperation(select.getWhereClause(), generateExpression(),
TiDBBinaryLogicalOperator.AND);
select.setWhereClause(newWhere);
}
return false;
}
boolean mutateOr(TiDBSelect select) {
if (select.getWhereClause() == null) {
select.setWhereClause(generateExpression());
return false;
} else {
TiDBExpression newWhere = new TiDBBinaryLogicalOperation(select.getWhereClause(), generateExpression(),
TiDBBinaryLogicalOperator.OR);
select.setWhereClause(newWhere);
return true;
}
}
boolean mutateLimit(TiDBSelect select) {
boolean increase = select.getLimitClause() != null;
if (increase) {
select.setLimitClause(null);
} else {
select.setLimitClause(generateConstant(TiDBDataType.INT));
}
return increase;
}
private boolean mutateGroupBy(TiDBSelect select) {
boolean increase = !select.getGroupByExpressions().isEmpty();
if (increase) {
select.clearGroupByExpressions();
select.clearHavingClause();
} else {
select.setGroupByExpressions(select.getFetchColumns());
}
return increase;
}
}