-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPostgresCommon.java
More file actions
516 lines (454 loc) · 21 KB
/
PostgresCommon.java
File metadata and controls
516 lines (454 loc) · 21 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package sqlancer.postgres.gen;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.postgres.PostgresGlobalState;
import sqlancer.postgres.PostgresProvider;
import sqlancer.postgres.PostgresSchema.PostgresColumn;
import sqlancer.postgres.PostgresSchema.PostgresDataType;
import sqlancer.postgres.PostgresSchema.PostgresTable;
import sqlancer.postgres.PostgresVisitor;
public final class PostgresCommon {
private PostgresCommon() {
}
public static List<String> getCommonFetchErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions");
errors.add("but it cannot be referenced from this part of the query");
errors.add("missing FROM-clause entry for table");
errors.add("canceling statement due to statement timeout");
errors.add("non-integer constant in GROUP BY");
errors.add("must appear in the GROUP BY clause or be used in an aggregate function");
errors.add("GROUP BY position");
return errors;
}
public static void addCommonFetchErrors(ExpectedErrors errors) {
errors.addAll(getCommonFetchErrors());
}
public static List<String> getCommonTableErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("is not commutative"); // exclude
errors.add("operator requires run-time type coercion"); // exclude
errors.add("partitioned tables cannot be unlogged");
return errors;
}
public static void addCommonTableErrors(ExpectedErrors errors) {
errors.addAll(getCommonTableErrors());
}
public static List<String> getCommonExpressionErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("for encoding \"SQL_ASCII\" does not exist");
errors.add("invalid byte sequence for encoding");
errors.add("You might need to add explicit type casts");
errors.add("invalid regular expression");
errors.add("could not determine which collation to use");
errors.add("invalid regular expression");
errors.add("operator does not exist");
errors.add("quantifier operand invalid");
errors.add("collation mismatch");
errors.add("collations are not supported");
errors.add("operator is not unique");
errors.add("is not a valid binary digit");
errors.add("invalid hexadecimal digit");
errors.add("invalid hexadecimal data: odd number of digits");
errors.add("zero raised to a negative power is undefined");
errors.add("division by zero");
errors.add("invalid input syntax for type money");
errors.add("invalid input syntax for type");
errors.add("cannot cast type");
errors.add("value overflows numeric format");
errors.add("LIKE pattern must not end with escape character");
errors.add("is of type boolean but expression is of type text");
errors.add("a negative number raised to a non-integer power yields a complex result");
errors.add("could not determine polymorphic type because input has type unknown");
errors.add("character number must be positive");
errors.addAll(getToCharFunctionErrors());
errors.addAll(getBitStringOperationErrors());
errors.addAll(getFunctionErrors());
errors.addAll(getCommonRangeExpressionErrors());
errors.addAll(getCommonRegexExpressionErrors());
return errors;
}
public static List<Pattern> getCommonExpressionRegexErrors() {
ArrayList<Pattern> errors = new ArrayList<>();
errors.add(Pattern.compile("cannot convert infinity to \\w+"));
errors.addAll(getFunctionRegexErrors());
return errors;
}
public static void addCommonExpressionErrors(ExpectedErrors errors) {
errors.addAll(getCommonExpressionErrors());
errors.addAllRegexes(getCommonExpressionRegexErrors());
}
private static List<String> getToCharFunctionErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("multiple decimal points");
errors.add("and decimal point together");
errors.add("multiple decimal points");
errors.add("cannot use \"S\" twice");
errors.add("must be ahead of \"PR\"");
errors.add("cannot use \"S\" and \"PL\"/\"MI\"/\"SG\"/\"PR\" together");
errors.add("cannot use \"S\" and \"SG\" together");
errors.add("cannot use \"S\" and \"MI\" together");
errors.add("cannot use \"S\" and \"PL\" together");
errors.add("cannot use \"PR\" and \"S\"/\"PL\"/\"MI\"/\"SG\" together");
errors.add("is not a number");
return errors;
}
private static List<String> getBitStringOperationErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("cannot XOR bit strings of different sizes");
errors.add("cannot AND bit strings of different sizes");
errors.add("cannot OR bit strings of different sizes");
errors.add("must be type boolean, not type text");
return errors;
}
private static List<String> getFunctionErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("out of valid range"); // get_bit/get_byte
errors.add("cannot take logarithm of a negative number");
errors.add("cannot take logarithm of zero");
errors.add("requested character too large for encoding"); // chr
errors.add("null character not permitted"); // chr
errors.add("requested character not valid for encoding"); // chr
errors.add("requested length too large"); // repeat
errors.add("invalid memory alloc request size"); // repeat
errors.add("negative substring length not allowed"); // substr
errors.add("invalid mask length"); // set_masklen
return errors;
}
private static List<Pattern> getFunctionRegexErrors() {
ArrayList<Pattern> errors = new ArrayList<>();
/*
* PostgreSQL support only a few conversion variants to ASCII: LATIN1, LATIN2, LATIN9 and WINDOWS1250. So, it is
* better to skip this error at all.
*/
errors.add(Pattern.compile("encoding conversion from \\w+ to ASCII not supported"));
/*
* In accordance with PostgreSQL code, commit 0ab1a2e, conversions to or from SQL_ASCII is meaningless. So
* disable errors on such an attempt.
*/
errors.add(Pattern.compile("encoding conversion from SQL_ASCII to \\w+ not supported"));
errors.add(Pattern.compile("encoding conversion from \\w+ to SQL_ASCII not supported"));
return errors;
}
private static List<String> getCommonRegexExpressionErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("is not a valid hexadecimal digit");
return errors;
}
public static List<String> getCommonRangeExpressionErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("range lower bound must be less than or equal to range upper bound");
errors.add("result of range difference would not be contiguous");
errors.add("out of range");
errors.add("malformed range literal");
errors.add("result of range union would not be contiguous");
return errors;
}
public static void addCommonRangeExpressionErrors(ExpectedErrors errors) {
errors.addAll(getCommonRangeExpressionErrors());
}
public static List<String> getCommonInsertUpdateErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("value too long for type character");
errors.add("cannot insert a non-DEFAULT value into column");
errors.add("not found in view targetlist");
return errors;
}
public static void addCommonInsertUpdateErrors(ExpectedErrors errors) {
errors.addAll(getCommonInsertUpdateErrors());
}
public static List<String> getGroupingErrors() {
ArrayList<String> errors = new ArrayList<>();
errors.add("non-integer constant in GROUP BY"); // TODO
errors.add("must appear in the GROUP BY clause or be used in an aggregate function");
errors.add("is not in select list");
errors.add("aggregate functions are not allowed in GROUP BY");
return errors;
}
public static void addGroupingErrors(ExpectedErrors errors) {
errors.addAll(getGroupingErrors());
}
public static boolean appendDataType(PostgresDataType type, StringBuilder sb, boolean allowSerial,
boolean generateOnlyKnown, List<String> opClasses) throws AssertionError {
boolean serial = false;
switch (type) {
case BOOLEAN:
sb.append("boolean");
break;
case INT:
if (Randomly.getBoolean() && allowSerial) {
serial = true;
sb.append(Randomly.fromOptions("serial", "bigserial"));
} else {
sb.append(Randomly.fromOptions("smallint", "integer", "bigint"));
}
break;
case TEXT:
if (Randomly.getBoolean()) {
sb.append("TEXT");
} else if (Randomly.getBoolean()) {
// TODO: support CHAR (without VAR)
if (PostgresProvider.generateOnlyKnown || Randomly.getBoolean()) {
sb.append("VAR");
}
sb.append("CHAR");
sb.append("(");
sb.append(ThreadLocalRandom.current().nextInt(1, 500));
sb.append(")");
} else {
sb.append("name");
}
if (Randomly.getBoolean() && !PostgresProvider.generateOnlyKnown) {
sb.append(" COLLATE ");
sb.append('"');
sb.append(Randomly.fromList(opClasses));
sb.append('"');
}
break;
case DECIMAL:
sb.append("DECIMAL");
break;
case FLOAT:
sb.append("REAL");
break;
case REAL:
sb.append("FLOAT");
break;
case RANGE:
sb.append(Randomly.fromOptions("int4range", "int4range")); // , "int8range", "numrange"
break;
case MONEY:
sb.append("money");
break;
case BIT:
sb.append("BIT");
// if (Randomly.getBoolean()) {
sb.append(" VARYING");
// }
sb.append("(");
sb.append(Randomly.getNotCachedInteger(1, 500));
sb.append(")");
break;
case INET:
sb.append("inet");
break;
default:
throw new AssertionError(type);
}
return serial;
}
public enum TableConstraints {
CHECK, UNIQUE, PRIMARY_KEY, FOREIGN_KEY, EXCLUDE
}
private enum StorageParameters {
FILLFACTOR("fillfactor", (r) -> r.getInteger(10, 100)),
// toast_tuple_target
PARALLEL_WORKERS("parallel_workers", (r) -> r.getInteger(0, 1024)),
AUTOVACUUM_ENABLED("autovacuum_enabled", (r) -> Randomly.fromOptions(0, 1)),
AUTOVACUUM_VACUUM_THRESHOLD("autovacuum_vacuum_threshold", (r) -> r.getInteger(0, 2147483647)),
OIDS("oids", (r) -> Randomly.fromOptions(0, 1)),
AUTOVACUUM_VACUUM_SCALE_FACTOR("autovacuum_vacuum_scale_factor",
(r) -> Randomly.fromOptions(0, 0.00001, 0.01, 0.1, 0.2, 0.5, 0.8, 0.9, 1)),
AUTOVACUUM_ANALYZE_THRESHOLD("autovacuum_analyze_threshold", (r) -> r.getLong(0, Integer.MAX_VALUE)),
AUTOVACUUM_ANALYZE_SCALE_FACTOR("autovacuum_analyze_scale_factor",
(r) -> Randomly.fromOptions(0, 0.00001, 0.01, 0.1, 0.2, 0.5, 0.8, 0.9, 1)),
AUTOVACUUM_VACUUM_COST_DELAY("autovacuum_vacuum_cost_delay", (r) -> r.getLong(0, 100)),
AUTOVACUUM_VACUUM_COST_LIMIT("autovacuum_vacuum_cost_limit", (r) -> r.getLong(1, 10000)),
AUTOVACUUM_FREEZE_MIN_AGE("autovacuum_freeze_min_age", (r) -> r.getLong(0, 1000000000)),
AUTOVACUUM_FREEZE_MAX_AGE("autovacuum_freeze_max_age", (r) -> r.getLong(100000, 2000000000)),
AUTOVACUUM_FREEZE_TABLE_AGE("autovacuum_freeze_table_age", (r) -> r.getLong(0, 2000000000));
// TODO
private String parameter;
private Function<Randomly, Object> op;
StorageParameters(String parameter, Function<Randomly, Object> op) {
this.parameter = parameter;
this.op = op;
}
}
public static void generateWith(StringBuilder sb, PostgresGlobalState globalState, ExpectedErrors errors) {
if (Randomly.getBoolean()) {
sb.append(" WITH (");
ArrayList<StorageParameters> values = new ArrayList<>(Arrays.asList(StorageParameters.values()));
values.remove(StorageParameters.OIDS);
errors.add("unrecognized parameter");
errors.add("ALTER TABLE / ADD CONSTRAINT USING INDEX is not supported on partitioned tables");
List<StorageParameters> subset = Randomly.nonEmptySubset(values);
int i = 0;
for (StorageParameters parameter : subset) {
if (i++ != 0) {
sb.append(", ");
}
sb.append(parameter.parameter);
sb.append("=");
sb.append(parameter.op.apply(globalState.getRandomly()));
}
sb.append(")");
}
}
public static void addTableConstraints(boolean excludePrimaryKey, StringBuilder sb, PostgresTable table,
PostgresGlobalState globalState, ExpectedErrors errors) {
// TODO constraint name
List<TableConstraints> tableConstraints = Randomly.nonEmptySubset(TableConstraints.values());
if (excludePrimaryKey) {
tableConstraints.remove(TableConstraints.PRIMARY_KEY);
}
if (globalState.getSchema().getDatabaseTables().isEmpty()) {
tableConstraints.remove(TableConstraints.FOREIGN_KEY);
}
for (TableConstraints t : tableConstraints) {
sb.append(", ");
// TODO add index parameters
addTableConstraint(sb, table, globalState, t, errors);
}
}
public static void addTableConstraint(StringBuilder sb, PostgresTable table, PostgresGlobalState globalState,
ExpectedErrors errors) {
addTableConstraint(sb, table, globalState, Randomly.fromOptions(TableConstraints.values()), errors);
}
private static void addTableConstraint(StringBuilder sb, PostgresTable table, PostgresGlobalState globalState,
TableConstraints t, ExpectedErrors errors) {
List<PostgresColumn> randomNonEmptyColumnSubset = table.getRandomNonEmptyColumnSubset();
List<PostgresColumn> otherColumns;
PostgresCommon.addCommonExpressionErrors(errors);
switch (t) {
case CHECK:
sb.append("CHECK(");
sb.append(PostgresVisitor.getExpressionAsString(globalState, PostgresDataType.BOOLEAN, table.getColumns()));
sb.append(")");
errors.add("constraint must be added to child tables too");
errors.add("missing FROM-clause entry for table");
break;
case UNIQUE:
sb.append("UNIQUE(");
sb.append(randomNonEmptyColumnSubset.stream().map(c -> c.getName()).collect(Collectors.joining(", ")));
sb.append(")");
appendIndexParameters(sb, globalState, errors);
break;
case PRIMARY_KEY:
sb.append("PRIMARY KEY(");
sb.append(randomNonEmptyColumnSubset.stream().map(c -> c.getName()).collect(Collectors.joining(", ")));
sb.append(")");
appendIndexParameters(sb, globalState, errors);
break;
case FOREIGN_KEY:
sb.append("FOREIGN KEY (");
sb.append(randomNonEmptyColumnSubset.stream().map(c -> c.getName()).collect(Collectors.joining(", ")));
sb.append(") REFERENCES ");
PostgresTable randomOtherTable = globalState.getSchema().getRandomTable(tab -> !tab.isView());
sb.append(randomOtherTable.getName());
if (randomOtherTable.getColumns().size() < randomNonEmptyColumnSubset.size()) {
throw new IgnoreMeException();
}
otherColumns = randomOtherTable.getRandomNonEmptyColumnSubset(randomNonEmptyColumnSubset.size());
sb.append("(");
sb.append(otherColumns.stream().map(c -> c.getName()).collect(Collectors.joining(", ")));
sb.append(")");
if (Randomly.getBoolean()) {
sb.append(" ");
sb.append(Randomly.fromOptions("MATCH FULL", "MATCH SIMPLE"));
}
if (Randomly.getBoolean()) {
sb.append(" ON DELETE ");
errors.add("ERROR: invalid ON DELETE action for foreign key constraint containing generated column");
deleteOrUpdateAction(sb);
}
if (Randomly.getBoolean()) {
sb.append(" ON UPDATE ");
errors.add("invalid ON UPDATE action for foreign key constraint containing generated column");
deleteOrUpdateAction(sb);
}
if (Randomly.getBoolean()) {
sb.append(" ");
if (Randomly.getBoolean()) {
sb.append("DEFERRABLE");
if (Randomly.getBoolean()) {
sb.append(" ");
sb.append(Randomly.fromOptions("INITIALLY DEFERRED", "INITIALLY IMMEDIATE"));
}
} else {
sb.append("NOT DEFERRABLE");
}
}
break;
case EXCLUDE:
errors.add("exclusion constraints are not supported on partitioned tables");
errors.add("unsupported EXCLUDE constraint with partition key definition");
sb.append("EXCLUDE ");
sb.append("(");
// TODO [USING index_method ]
for (int i = 0; i < Randomly.smallNumber() + 1; i++) {
if (i != 0) {
sb.append(", ");
}
appendExcludeElement(sb, globalState, table.getColumns());
sb.append(" WITH ");
appendOperator(sb, globalState.getOperators());
}
sb.append(")");
appendIndexParameters(sb, globalState, errors);
errors.add("is not valid");
errors.add("no operator matches");
errors.add("operator does not exist");
errors.add("unknown has no default operator class");
errors.add("exclusion constraints are not supported on partitioned tables");
errors.add("The exclusion operator must be related to the index operator class for the constraint");
errors.add("could not create exclusion constraint");
if (Randomly.getBoolean()) {
sb.append(" WHERE ");
sb.append("(");
sb.append(PostgresVisitor.asString(PostgresExpressionGenerator.generateExpression(globalState,
table.getColumns(), PostgresDataType.BOOLEAN)));
sb.append(")");
}
break;
default:
throw new AssertionError(t);
}
}
private static void appendIndexParameters(StringBuilder sb, PostgresGlobalState globalState,
ExpectedErrors errors) {
if (Randomly.getBoolean()) {
generateWith(sb, globalState, errors);
}
// TODO: [ USING INDEX TABLESPACE tablespace ]
}
private static void appendOperator(StringBuilder sb, List<String> operators) {
sb.append(Randomly.fromList(operators));
}
// complete
private static void appendExcludeElement(StringBuilder sb, PostgresGlobalState globalState,
List<PostgresColumn> columns) {
if (Randomly.getBoolean()) {
// append column name
sb.append(Randomly.fromList(columns).getName());
} else {
// append expression
sb.append("(");
sb.append(PostgresVisitor.asString(PostgresExpressionGenerator.generateExpression(globalState, columns)));
sb.append(")");
}
if (Randomly.getBoolean()) {
sb.append(" ");
sb.append(Randomly.fromList(globalState.getOpClasses()));
}
if (Randomly.getBoolean()) {
sb.append(" ");
sb.append(Randomly.fromOptions("ASC", "DESC"));
}
if (Randomly.getBoolean()) {
sb.append(" NULLS ");
sb.append(Randomly.fromOptions("FIRST", "LAST"));
}
}
private static void deleteOrUpdateAction(StringBuilder sb) {
sb.append(Randomly.fromOptions("NO ACTION", "RESTRICT", "CASCADE", "SET NULL", "SET DEFAULT"));
}
}