forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrestoInsertGenerator.java
More file actions
51 lines (41 loc) · 1.81 KB
/
PrestoInsertGenerator.java
File metadata and controls
51 lines (41 loc) · 1.81 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
package sqlancer.presto.gen;
import java.util.List;
import java.util.stream.Collectors;
import sqlancer.common.gen.AbstractInsertGenerator;
import sqlancer.common.query.ExpectedErrors;
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 class PrestoInsertGenerator extends AbstractInsertGenerator<PrestoColumn> {
private final PrestoGlobalState globalState;
public PrestoInsertGenerator(PrestoGlobalState globalState) {
this.globalState = globalState;
}
public static SQLQueryAdapter getQuery(PrestoGlobalState globalState) {
return new PrestoInsertGenerator(globalState).generate();
}
private SQLQueryAdapter generate() {
sb.append("INSERT INTO ");
PrestoTable table = globalState.getSchema().getRandomTable(t -> !t.isView());
List<PrestoColumn> columns = table.getRandomNonEmptyColumnSubset();
sb.append(table.getName());
sb.append("(");
sb.append(columns.stream().map(c -> c.getName()).collect(Collectors.joining(", ")));
sb.append(")");
sb.append(" VALUES ");
insertColumns(columns);
ExpectedErrors errors = new ExpectedErrors();
PrestoErrors.addInsertErrors(errors);
return new SQLQueryAdapter(sb.toString(), errors, false, false);
}
@Override
protected void insertValue(PrestoColumn prestoColumn) {
PrestoExpression constant = new PrestoTypedExpressionGenerator(globalState)
.generateInsertConstant(prestoColumn.getType());
sb.append(PrestoToStringVisitor.asString(constant));
}
}