forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresIndexGenerator.java
More file actions
153 lines (142 loc) · 6.33 KB
/
PostgresIndexGenerator.java
File metadata and controls
153 lines (142 loc) · 6.33 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
package sqlancer.postgres.gen;
import java.util.List;
import java.util.stream.Collectors;
import sqlancer.Randomly;
import sqlancer.common.DBMSCommon;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.postgres.PostgresGlobalState;
import sqlancer.postgres.PostgresSchema.PostgresColumn;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
import sqlancer.postgres.PostgresSchema.PostgresIndex;
import sqlancer.postgres.PostgresSchema.PostgresTable;
import sqlancer.postgres.PostgresVisitor;
import sqlancer.postgres.ast.PostgresExpression;
public final class PostgresIndexGenerator {
private PostgresIndexGenerator() {
}
public enum IndexType {
BTREE, HASH, GIST, GIN
}
public static SQLQueryAdapter generate(PostgresGlobalState globalState) {
ExpectedErrors errors = new ExpectedErrors();
StringBuilder sb = new StringBuilder();
sb.append("CREATE");
if (Randomly.getBoolean()) {
sb.append(" UNIQUE");
}
sb.append(" INDEX ");
/*
* Commented out as a workaround for https://www.postgresql.org/message-id/CA%2Bu7OA4XYhc-
* qyCgJqwwgMGZDWAyeH821oa5oMzm_HEifZ4BeA%40mail.gmail.com
*/
// if (Randomly.getBoolean()) {
// sb.append("CONCURRENTLY ");
// }
PostgresTable randomTable = globalState.getSchema().getRandomTable(t -> !t.isView()); // TODO: materialized
// views
String indexName = getNewIndexName(randomTable);
sb.append(indexName);
sb.append(" ON ");
if (Randomly.getBoolean()) {
sb.append("ONLY ");
}
sb.append(randomTable.getName());
IndexType method;
if (Randomly.getBoolean()) {
sb.append(" USING ");
method = Randomly.fromOptions(IndexType.values());
sb.append(method);
} else {
method = IndexType.BTREE;
}
sb.append("(");
if (method == IndexType.HASH) {
sb.append(randomTable.getRandomColumn().getName());
} else {
for (int i = 0; i < Randomly.smallNumber() + 1; i++) {
if (i != 0) {
sb.append(", ");
}
if (Randomly.getBoolean()) {
sb.append(randomTable.getRandomColumn().getName());
} else {
sb.append("(");
PostgresExpression expression = PostgresExpressionGenerator.generateExpression(globalState,
randomTable.getColumns());
sb.append(PostgresVisitor.asString(expression));
sb.append(")");
}
// if (Randomly.getBoolean()) {
// sb.append(" ");
// sb.append("COLLATE ");
// sb.append(Randomly.fromOptions("C", "POSIX"));
// }
if (Randomly.getBooleanWithRatherLowProbability()) {
sb.append(" ");
sb.append(globalState.getRandomOpclass());
errors.add("does not accept");
errors.add("does not exist for access method");
}
if (Randomly.getBoolean()) {
sb.append(" ");
sb.append(Randomly.fromOptions("ASC", "DESC"));
}
if (Randomly.getBooleanWithRatherLowProbability()) {
sb.append(" NULLS ");
sb.append(Randomly.fromOptions("FIRST", "LAST"));
}
}
}
sb.append(")");
if (Randomly.getBoolean() && method != IndexType.HASH) {
sb.append(" INCLUDE(");
List<PostgresColumn> columns = randomTable.getRandomNonEmptyColumnSubset();
sb.append(columns.stream().map(c -> c.getName()).collect(Collectors.joining(", ")));
sb.append(")");
}
if (Randomly.getBoolean()) {
sb.append(" WHERE ");
PostgresExpression expr = new PostgresExpressionGenerator(globalState).setColumns(randomTable.getColumns())
.setGlobalState(globalState).generateExpression(PostgresDataType.BOOLEAN);
sb.append(PostgresVisitor.asString(expr));
}
errors.add("already contains data"); // CONCURRENT INDEX failed
errors.add("You might need to add explicit type casts");
errors.add(" collations are not supported"); // TODO check
errors.add("because it has pending trigger events");
errors.add("could not determine which collation to use for index expression");
errors.add("could not determine which collation to use for string comparison");
errors.add("is duplicated");
errors.add("access method \"gin\" does not support unique indexes");
errors.add("access method \"hash\" does not support unique indexes");
errors.add("already exists");
errors.add("could not create unique index");
errors.add("has no default operator class");
errors.add("does not support");
errors.add("cannot cast");
errors.add("unsupported UNIQUE constraint with partition key definition");
errors.add("insufficient columns in UNIQUE constraint definition");
errors.add("invalid input syntax for");
errors.add("must be type ");
errors.add("integer out of range");
errors.add("division by zero");
errors.add("out of range");
errors.add("functions in index predicate must be marked IMMUTABLE");
errors.add("functions in index expression must be marked IMMUTABLE");
errors.add("result of range difference would not be contiguous");
errors.add("which is part of the partition key");
PostgresCommon.addCommonExpressionErrors(errors);
return new SQLQueryAdapter(sb.toString(), errors);
}
private static String getNewIndexName(PostgresTable randomTable) {
List<PostgresIndex> indexes = randomTable.getIndexes();
int indexI = 0;
while (true) {
String indexName = DBMSCommon.createIndexName(indexI++);
if (indexes.stream().noneMatch(i -> i.getIndexName().equals(indexName))) {
return indexName;
}
}
}
}