forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrestoIndexGenerator.java
More file actions
56 lines (50 loc) · 2.11 KB
/
PrestoIndexGenerator.java
File metadata and controls
56 lines (50 loc) · 2.11 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
package sqlancer.presto.gen;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.presto.PrestoGlobalState;
import sqlancer.presto.PrestoSchema;
import sqlancer.presto.PrestoSchema.PrestoColumn;
import sqlancer.presto.PrestoSchema.PrestoTable;
import sqlancer.presto.PrestoToStringVisitor;
import sqlancer.presto.ast.PrestoExpression;
public final class PrestoIndexGenerator {
private PrestoIndexGenerator() {
}
public static SQLQueryAdapter getQuery(PrestoGlobalState globalState) {
ExpectedErrors errors = new ExpectedErrors();
StringBuilder sb = new StringBuilder();
sb.append("CREATE ");
if (Randomly.getBoolean()) {
errors.add("Cant create unique index, table contains duplicate data on indexed column(s)");
sb.append("UNIQUE ");
}
sb.append("INDEX ");
sb.append(Randomly.fromOptions("i0", "i1", "i2", "i3", "i4")); // cannot query this information
sb.append(" ON ");
PrestoTable table = globalState.getSchema().getRandomTable(t -> !t.isView());
sb.append(table.getName());
sb.append("(");
List<PrestoColumn> columns = table.getRandomNonEmptyColumnSubset();
for (int i = 0; i < columns.size(); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(columns.get(i).getName());
sb.append(" ");
if (Randomly.getBooleanWithRatherLowProbability()) {
sb.append(Randomly.fromOptions("ASC", "DESC"));
}
}
sb.append(")");
if (Randomly.getBoolean()) {
sb.append(" WHERE ");
PrestoExpression expr = new PrestoTypedExpressionGenerator(globalState).setColumns(table.getColumns())
.generateExpression(PrestoSchema.PrestoCompositeDataType.getRandomWithoutNull());
sb.append(PrestoToStringVisitor.asString(expr));
}
errors.add("already exists!");
return new SQLQueryAdapter(sb.toString(), errors, true, false);
}
}