-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathPrestoIndexGenerator.java
More file actions
61 lines (53 loc) · 2.25 KB
/
Copy pathPrestoIndexGenerator.java
File metadata and controls
61 lines (53 loc) · 2.25 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
package sqlancer.presto.gen;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.common.gen.AbstractIndexGenerator;
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 class PrestoIndexGenerator extends AbstractIndexGenerator<PrestoColumn> {
private final PrestoGlobalState globalState;
public PrestoIndexGenerator(PrestoGlobalState globalState) {
this.globalState = globalState;
this.canAffectSchema = true;
this.canonicalizeString = false;
}
public static SQLQueryAdapter getQuery(PrestoGlobalState globalState) {
return new PrestoIndexGenerator(globalState).getStatement();
}
@Override
public void buildStatement() {
boolean unique = Randomly.getBoolean();
if (unique) {
errors.add("Cant create unique index, table contains duplicate data on indexed column(s)");
}
appendCreateIndex(unique);
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()) {
PrestoExpression expr = new PrestoTypedExpressionGenerator(globalState).setColumns(table.getColumns())
.generateExpression(PrestoSchema.PrestoCompositeDataType.getRandomWithoutNull());
appendWhereClause(PrestoToStringVisitor.asString(expr));
}
errors.add("already exists!");
}
}