forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiDBAlterTableGenerator.java
More file actions
118 lines (111 loc) · 5.18 KB
/
TiDBAlterTableGenerator.java
File metadata and controls
118 lines (111 loc) · 5.18 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
package sqlancer.tidb.gen;
import java.util.stream.Collectors;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
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;
public final class TiDBAlterTableGenerator {
private TiDBAlterTableGenerator() {
}
private enum Action {
MODIFY_COLUMN, ENABLE_DISABLE_KEYS, DROP_PRIMARY_KEY, ADD_PRIMARY_KEY, CHANGE, DROP_COLUMN, ORDER_BY
}
public static SQLQueryAdapter getQuery(TiDBGlobalState globalState) {
ExpectedErrors errors = new ExpectedErrors();
errors.add(
"Information schema is changed during the execution of the statement(for example, table definition may be updated by other DDL ran in parallel)");
errors.add("Data truncat");
errors.add("without a key length");
errors.add("supported");
errors.add("SQL syntax");
errors.add("can't drop");
errors.add("A PRIMARY must include all columns in the table's partitioning function");
errors.add("key was too long");
errors.add("Duplicate entry");
errors.add("has a partitioning function dependency and cannot be dropped or renamed");
StringBuilder sb = new StringBuilder("ALTER TABLE ");
TiDBTable table = globalState.getSchema().getRandomTable(t -> !t.isView());
TiDBColumn column = table.getRandomColumn();
sb.append(table.getName());
Action a = Randomly.fromOptions(Action.values());
sb.append(" ");
switch (a) {
case MODIFY_COLUMN:
sb.append("MODIFY ");
sb.append(column.getName());
sb.append(" ");
sb.append(TiDBCompositeDataType.getRandom().toString());
break;
case DROP_COLUMN:
sb.append(" DROP ");
if (table.getColumns().size() <= 1) {
throw new IgnoreMeException();
}
sb.append(column.getName());
errors.add("with composite index covered or Primary Key covered now");
errors.add("Unsupported drop integer primary key");
errors.add("has a generated column dependency");
errors.add(
"references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them");
break;
case ENABLE_DISABLE_KEYS:
sb.append(Randomly.fromOptions("ENABLE", "DISABLE"));
sb.append(" KEYS");
break;
case DROP_PRIMARY_KEY:
if (!column.isPrimaryKey()) {
throw new IgnoreMeException();
}
errors.add("Unsupported drop integer primary key");
errors.add("Unsupported drop primary key when alter-primary-key is false");
errors.add("Unsupported drop primary key when the table's pkIsHandle is true");
errors.add("Incorrect table definition; there can be only one auto column and it must be defined as a key");
sb.append(" DROP PRIMARY KEY");
break;
case ADD_PRIMARY_KEY:
sb.append("ADD PRIMARY KEY(");
sb.append(table.getRandomNonEmptyColumnSubset().stream().map(c -> {
StringBuilder colName = new StringBuilder(c.getName());
if (c.getType().getPrimitiveDataType() == TiDBDataType.TEXT
|| c.getType().getPrimitiveDataType() == TiDBDataType.BLOB) {
TiDBTableGenerator.appendSpecifiers(colName, c.getType().getPrimitiveDataType());
}
return colName;
}).collect(Collectors.joining(", ")));
sb.append(")");
errors.add("Unsupported add primary key, alter-primary-key is false");
errors.add("Information schema is changed during the execution of the statement");
errors.add("Multiple primary key defined");
errors.add("Invalid use of NULL value");
errors.add("Duplicate entry");
errors.add("'Defining a virtual generated column as primary key' is not supported for generated columns");
break;
case CHANGE:
sb.append(" CHANGE ");
sb.append(column.getName());
sb.append(" ");
sb.append(column.getName());
sb.append(" ");
sb.append(column.getType().toString());
sb.append(" NOT NULL ");
errors.add("Invalid use of NULL value");
errors.add("Unsupported modify column:");
errors.add("Invalid integer format for value");
break;
case ORDER_BY:
sb.append(" ORDER BY ");
sb.append(table.getRandomNonEmptyColumnSubset().stream()
.map(c -> c.getName() + Randomly.fromOptions("", " ASC", " DESC"))
.collect(Collectors.joining(", ")));
break;
default:
throw new AssertionError(a);
}
return new SQLQueryAdapter(sb.toString(), errors, true);
}
}