forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiDBViewGenerator.java
More file actions
59 lines (54 loc) · 2.14 KB
/
TiDBViewGenerator.java
File metadata and controls
59 lines (54 loc) · 2.14 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
package sqlancer.tidb.gen;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.tidb.TiDBBugs;
import sqlancer.tidb.TiDBErrors;
import sqlancer.tidb.TiDBProvider.TiDBGlobalState;
import sqlancer.tidb.ast.TiDBSelect;
public final class TiDBViewGenerator {
private TiDBViewGenerator() {
}
public static SQLQueryAdapter getQuery(TiDBGlobalState globalState) {
if (globalState.getSchema().getDatabaseTables().size() > globalState.getDbmsSpecificOptions().maxNumTables) {
throw new IgnoreMeException();
}
int nrColumns = Randomly.smallNumber() + 1;
StringBuilder sb = new StringBuilder("CREATE ");
if (Randomly.getBoolean()) {
sb.append("OR REPLACE ");
}
if (Randomly.getBoolean()) {
sb.append("ALGORITHM=");
sb.append(Randomly.fromOptions("UNDEFINED", "MERGE", "TEMPTABLE"));
sb.append(" ");
}
sb.append("VIEW ");
sb.append(globalState.getSchema().getFreeViewName());
sb.append("(");
for (int i = 0; i < nrColumns; i++) {
if (i != 0) {
sb.append(", ");
}
sb.append("c");
sb.append(i);
}
sb.append(") AS ");
TiDBSelect select = TiDBRandomQuerySynthesizer.generateSelect(globalState, nrColumns);
if (TiDBBugs.bug38319 && !select.getGroupByExpressions().isEmpty()) {
throw new IgnoreMeException();
}
sb.append(select.asString());
ExpectedErrors errors = new ExpectedErrors();
TiDBErrors.addExpressionErrors(errors);
errors.add(
"references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them");
errors.add("Unknown column ");
if (sb.toString().contains("\\\\")) {
// TODO: CREATE VIEW v0(c0) AS SELECT '\\' FROM t0; causes an unexpected failure
throw new IgnoreMeException();
}
return new SQLQueryAdapter(sb.toString(), errors, true);
}
}