forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrestoUpdateGenerator.java
More file actions
52 lines (43 loc) · 1.83 KB
/
PrestoUpdateGenerator.java
File metadata and controls
52 lines (43 loc) · 1.83 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
package sqlancer.presto.gen;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.common.gen.AbstractUpdateGenerator;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.presto.PrestoErrors;
import sqlancer.presto.PrestoGlobalState;
import sqlancer.presto.PrestoSchema.PrestoColumn;
import sqlancer.presto.PrestoSchema.PrestoTable;
import sqlancer.presto.PrestoToStringVisitor;
import sqlancer.presto.ast.PrestoExpression;
public final class PrestoUpdateGenerator extends AbstractUpdateGenerator<PrestoColumn> {
private final PrestoGlobalState globalState;
private PrestoTypedExpressionGenerator gen;
private PrestoUpdateGenerator(PrestoGlobalState globalState) {
this.globalState = globalState;
}
public static SQLQueryAdapter getQuery(PrestoGlobalState globalState) {
return new PrestoUpdateGenerator(globalState).generate();
}
private SQLQueryAdapter generate() {
PrestoTable table = globalState.getSchema().getRandomTable(t -> !t.isView());
List<PrestoColumn> columns = table.getRandomNonEmptyColumnSubset();
gen = new PrestoTypedExpressionGenerator(globalState).setColumns(table.getColumns());
sb.append("UPDATE ");
sb.append(table.getName());
sb.append(" SET ");
updateColumns(columns);
PrestoErrors.addInsertErrors(errors);
return new SQLQueryAdapter(sb.toString(), errors, false, false);
}
@Override
protected void updateValue(PrestoColumn column) {
PrestoExpression expr;
if (Randomly.getBooleanWithSmallProbability()) {
expr = gen.generateExpression(column.getType());
PrestoErrors.addExpressionErrors(errors);
} else {
expr = gen.generateConstant(column.getType());
}
sb.append(PrestoToStringVisitor.asString(expr));
}
}