forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresTableGenerator.java
More file actions
309 lines (292 loc) · 13.2 KB
/
PostgresTableGenerator.java
File metadata and controls
309 lines (292 loc) · 13.2 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
package sqlancer.postgres.gen;
import java.util.ArrayList;
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;
import sqlancer.postgres.PostgresSchema.PostgresColumn;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
import sqlancer.postgres.PostgresSchema.PostgresTable;
import sqlancer.postgres.PostgresVisitor;
import sqlancer.postgres.ast.PostgresExpression;
public class PostgresTableGenerator {
private final String tableName;
private boolean columnCanHavePrimaryKey;
private boolean columnHasPrimaryKey;
private final StringBuilder sb = new StringBuilder();
private boolean isTemporaryTable;
private boolean isPartitionedTable;
private final PostgresSchema newSchema;
private final List<PostgresColumn> columnsToBeAdded = new ArrayList<>();
protected final ExpectedErrors errors = new ExpectedErrors();
private final PostgresTable table;
private final boolean generateOnlyKnown;
private final PostgresGlobalState globalState;
public PostgresTableGenerator(String tableName, PostgresSchema newSchema, boolean generateOnlyKnown,
PostgresGlobalState globalState) {
this.tableName = tableName;
this.newSchema = newSchema;
this.generateOnlyKnown = generateOnlyKnown;
this.globalState = globalState;
table = new PostgresTable(tableName, columnsToBeAdded, null, null, null, false, false);
errors.add("invalid input syntax for");
errors.add("is not unique");
errors.add("integer out of range");
errors.add("division by zero");
errors.add("cannot create partitioned table as inheritance child");
errors.add("cannot cast");
errors.add("ERROR: functions in index expression must be marked IMMUTABLE");
errors.add("functions in partition key expression must be marked IMMUTABLE");
errors.add("functions in index predicate must be marked IMMUTABLE");
errors.add("has no default operator class for access method");
errors.add("does not exist for access method");
errors.add("does not accept data type");
errors.add("but default expression is of type text");
errors.add("has pseudo-type unknown");
errors.add("no collation was derived for partition key column");
errors.add("inherits from generated column but specifies identity");
errors.add("inherits from generated column but specifies default");
PostgresCommon.addCommonExpressionErrors(errors);
PostgresCommon.addCommonTableErrors(errors);
}
public static SQLQueryAdapter generate(String tableName, PostgresSchema newSchema, boolean generateOnlyKnown,
PostgresGlobalState globalState) {
return new PostgresTableGenerator(tableName, newSchema, generateOnlyKnown, globalState).generate();
}
protected SQLQueryAdapter generate() {
columnCanHavePrimaryKey = true;
sb.append("CREATE");
if (Randomly.getBoolean()) {
sb.append(" ");
isTemporaryTable = true;
sb.append(Randomly.fromOptions("TEMPORARY", "TEMP"));
} else if (Randomly.getBoolean()) {
sb.append(" UNLOGGED");
}
sb.append(" TABLE");
if (Randomly.getBoolean()) {
sb.append(" IF NOT EXISTS");
}
sb.append(" ");
sb.append(tableName);
if (Randomly.getBoolean() && !newSchema.getDatabaseTables().isEmpty()) {
createLike();
} else {
createStandard();
}
return new SQLQueryAdapter(sb.toString(), errors, true);
}
private void createStandard() throws AssertionError {
sb.append("(");
for (int i = 0; i < Randomly.smallNumber() + 1; i++) {
if (i != 0) {
sb.append(", ");
}
String name = DBMSCommon.createColumnName(i);
createColumn(name);
}
if (Randomly.getBoolean()) {
errors.add("constraints on temporary tables may reference only temporary tables");
errors.add("constraints on unlogged tables may reference only permanent or unlogged tables");
errors.add("constraints on permanent tables may reference only permanent tables");
errors.add("cannot be implemented");
errors.add("there is no unique constraint matching given keys for referenced table");
errors.add("cannot reference partitioned table");
errors.add("unsupported ON COMMIT and foreign key combination");
errors.add("ERROR: invalid ON DELETE action for foreign key constraint containing generated column");
errors.add("exclusion constraints are not supported on partitioned tables");
PostgresCommon.addTableConstraints(columnHasPrimaryKey, sb, table, globalState, errors);
}
sb.append(")");
generateInherits();
generatePartitionBy();
generateUsing();
if (!isPartitionedTable) {
PostgresCommon.generateWith(sb, globalState, errors);
}
if (Randomly.getBoolean() && isTemporaryTable) {
sb.append(" ON COMMIT ");
sb.append(Randomly.fromOptions("PRESERVE ROWS", "DELETE ROWS", "DROP"));
sb.append(" ");
}
}
private void createLike() {
sb.append("(");
sb.append("LIKE ");
sb.append(newSchema.getRandomTable().getName());
if (Randomly.getBoolean()) {
for (int i = 0; i < Randomly.smallNumber(); i++) {
String option = Randomly.fromOptions("DEFAULTS", "CONSTRAINTS", "INDEXES", "STORAGE", "COMMENTS",
"GENERATED", "IDENTITY", "STATISTICS", "STORAGE", "ALL");
sb.append(" ");
sb.append(Randomly.fromOptions("INCLUDING", "EXCLUDING"));
sb.append(" ");
sb.append(option);
}
}
sb.append(")");
}
private void createColumn(String name) throws AssertionError {
sb.append(name);
sb.append(" ");
PostgresDataType type = PostgresDataType.getRandomType();
boolean serial = PostgresCommon.appendDataType(type, sb, true, generateOnlyKnown, globalState.getCollates());
PostgresColumn c = new PostgresColumn(name, type);
c.setTable(table);
columnsToBeAdded.add(c);
sb.append(" ");
if (Randomly.getBoolean()) {
createColumnConstraint(type, serial);
}
}
private void generatePartitionBy() {
if (Randomly.getBoolean()) {
isPartitionedTable = false;
return;
}
isPartitionedTable = true;
sb.append(" PARTITION BY ");
// TODO "RANGE",
String partitionOption = Randomly.fromOptions("RANGE", "LIST", "HASH");
sb.append(partitionOption);
sb.append("(");
errors.add("unrecognized parameter");
errors.add("cannot use constant expression");
errors.add("cannot add NO INHERIT constraint to partitioned table");
errors.add("unrecognized parameter");
errors.add("unsupported PRIMARY KEY constraint with partition key definition");
errors.add("which is part of the partition key.");
errors.add("unsupported UNIQUE constraint with partition key definition");
errors.add("does not accept data type");
int n = partitionOption.contentEquals("LIST") ? 1 : Randomly.smallNumber() + 1;
PostgresCommon.addCommonExpressionErrors(errors);
for (int i = 0; i < n; i++) {
if (i != 0) {
sb.append(", ");
}
sb.append("(");
PostgresExpression expr = PostgresExpressionGenerator.generateExpression(globalState, columnsToBeAdded);
sb.append(PostgresVisitor.asString(expr));
sb.append(")");
if (Randomly.getBoolean()) {
sb.append(globalState.getRandomOpclass());
errors.add("does not exist for access method");
}
}
sb.append(")");
}
private void generateUsing() {
/*
* Postgres does not allow specifying USING clause for partitioned tables since they don't have any storage
* associated with them
*/
if (isPartitionedTable) {
return;
}
if (Randomly.getBoolean()) {
return;
}
sb.append(" USING ");
sb.append(globalState.getRandomTableAccessMethod());
}
private void generateInherits() {
if (Randomly.getBoolean() && !newSchema.getDatabaseTablesWithoutViews().isEmpty()) {
sb.append(" INHERITS(");
sb.append(newSchema.getDatabaseTablesRandomSubsetNotEmpty().stream().map(t -> t.getName())
.collect(Collectors.joining(", ")));
sb.append(")");
errors.add("has a type conflict");
errors.add("has a generation conflict");
errors.add("cannot create partitioned table as inheritance child");
errors.add("cannot inherit from temporary relation");
errors.add("cannot inherit from partitioned table");
errors.add("has a collation conflict");
errors.add("inherits conflicting default values");
errors.add("specifies generation expression");
}
}
private enum ColumnConstraint {
NULL_OR_NOT_NULL, UNIQUE, PRIMARY_KEY, DEFAULT, CHECK, GENERATED
};
private void createColumnConstraint(PostgresDataType type, boolean serial) {
List<ColumnConstraint> constraintSubset = Randomly.nonEmptySubset(ColumnConstraint.values());
if (Randomly.getBoolean()) {
// make checks constraints less likely
constraintSubset.remove(ColumnConstraint.CHECK);
}
if (!columnCanHavePrimaryKey || columnHasPrimaryKey) {
constraintSubset.remove(ColumnConstraint.PRIMARY_KEY);
}
if (constraintSubset.contains(ColumnConstraint.GENERATED)
&& constraintSubset.contains(ColumnConstraint.DEFAULT)) {
// otherwise: ERROR: both default and identity specified for column
constraintSubset.remove(Randomly.fromOptions(ColumnConstraint.GENERATED, ColumnConstraint.DEFAULT));
}
if (constraintSubset.contains(ColumnConstraint.GENERATED) && type != PostgresDataType.INT) {
// otherwise: ERROR: identity column type must be smallint, integer, or bigint
constraintSubset.remove(ColumnConstraint.GENERATED);
}
if (serial) {
constraintSubset.remove(ColumnConstraint.GENERATED);
constraintSubset.remove(ColumnConstraint.DEFAULT);
constraintSubset.remove(ColumnConstraint.NULL_OR_NOT_NULL);
}
for (ColumnConstraint c : constraintSubset) {
sb.append(" ");
switch (c) {
case NULL_OR_NOT_NULL:
sb.append(Randomly.fromOptions("NOT NULL", "NULL"));
errors.add("conflicting NULL/NOT NULL declarations");
break;
case UNIQUE:
sb.append("UNIQUE");
break;
case PRIMARY_KEY:
sb.append("PRIMARY KEY");
columnHasPrimaryKey = true;
break;
case DEFAULT:
sb.append("DEFAULT");
sb.append(" (");
sb.append(PostgresVisitor.asString(PostgresExpressionGenerator.generateExpression(globalState, type)));
sb.append(")");
// CREATE TEMPORARY TABLE t1(c0 smallint DEFAULT ('566963878'));
errors.add("out of range");
errors.add("is a generated column");
break;
case CHECK:
sb.append("CHECK (");
sb.append(PostgresVisitor.asString(PostgresExpressionGenerator.generateExpression(globalState,
columnsToBeAdded, PostgresDataType.BOOLEAN)));
sb.append(")");
if (Randomly.getBoolean()) {
sb.append(" NO INHERIT");
}
errors.add("out of range");
break;
case GENERATED:
sb.append("GENERATED ");
if (Randomly.getBoolean()) {
sb.append(" ALWAYS AS (");
sb.append(PostgresVisitor.asString(
PostgresExpressionGenerator.generateExpression(globalState, columnsToBeAdded, type)));
sb.append(") STORED");
errors.add("A generated column cannot reference another generated column.");
errors.add("cannot use generated column in partition key");
errors.add("generation expression is not immutable");
errors.add("cannot use column reference in DEFAULT expression");
} else {
sb.append(Randomly.fromOptions("ALWAYS", "BY DEFAULT"));
sb.append(" AS IDENTITY");
}
break;
default:
throw new AssertionError(sb);
}
}
}
}