-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathHiveInsertGenerator.java
More file actions
52 lines (40 loc) · 1.73 KB
/
HiveInsertGenerator.java
File metadata and controls
52 lines (40 loc) · 1.73 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.hive.gen;
import java.util.List;
import sqlancer.common.gen.AbstractInsertGenerator;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.hive.HiveErrors;
import sqlancer.hive.HiveGlobalState;
import sqlancer.hive.HiveSchema.HiveColumn;
import sqlancer.hive.HiveSchema.HiveTable;
import sqlancer.hive.HiveToStringVisitor;
public class HiveInsertGenerator extends AbstractInsertGenerator<HiveColumn> {
private final HiveGlobalState globalState;
private final ExpectedErrors errors = new ExpectedErrors();
private final HiveExpressionGenerator gen;
public HiveInsertGenerator(HiveGlobalState globalState) {
this.globalState = globalState;
this.gen = new HiveExpressionGenerator(globalState);
}
public static SQLQueryAdapter getQuery(HiveGlobalState globalState) {
return new HiveInsertGenerator(globalState).generate();
}
@Override
protected void insertValue(HiveColumn column) {
sb.append(HiveToStringVisitor.asString(gen.generateConstant()));
}
private SQLQueryAdapter generate() {
// Inserting values into tables from SQL.
sb.append("INSERT INTO ");
HiveTable table = globalState.getSchema().getRandomTable(t -> !t.isView());
sb.append(table.getName());
// TODO: specify the inserted partition
sb.append(" VALUES ");
// Values must be provided by every column in the Hive table.
// A value is either null or any valid SQL literal.
List<HiveColumn> columns = table.getColumns();
insertColumns(columns);
HiveErrors.addInsertErrors(errors);
return new SQLQueryAdapter(sb.toString(), errors, false, false);
}
}