-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathHiveTableGenerator.java
More file actions
122 lines (106 loc) · 4.19 KB
/
HiveTableGenerator.java
File metadata and controls
122 lines (106 loc) · 4.19 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package sqlancer.hive.gen;
import java.util.ArrayList;
import java.util.List;
import sqlancer.Randomly;
import sqlancer.common.DBMSCommon;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLQueryAdapter;
import sqlancer.hive.HiveErrors;
import sqlancer.hive.HiveGlobalState;
import sqlancer.hive.HiveSchema;
import sqlancer.hive.HiveSchema.HiveColumn;
import sqlancer.hive.HiveSchema.HiveDataType;
import sqlancer.hive.HiveSchema.HiveTable;
import sqlancer.hive.HiveToStringVisitor;
public class HiveTableGenerator {
// TODO: support various file formats? e.g. JSONFILE, SEQUENCEFILE, TEXTFILE, RCFILE, ORC, PARQUET, AVRO.
private enum ColumnConstraints {
PRIMARY_KEY_DISABLE, UNIQUE_DISABLE, NOT_NULL, DEFAULT, CHECK
// ENABLE_OR_DISABLE, NOVALIDATE, RELY_OR_NORELY
}
private final HiveGlobalState globalState;
private final String tableName;
private final boolean allowPrimaryKey = Randomly.getBoolean();
private final StringBuilder sb = new StringBuilder();
private final HiveExpressionGenerator gen;
private final HiveTable table;
private final List<HiveColumn> columnsToBeAdded = new ArrayList<>();
private boolean setPrimaryKey;
public HiveTableGenerator(HiveGlobalState globalState, String tableName) {
this.tableName = tableName;
this.globalState = globalState;
this.table = new HiveTable(tableName, columnsToBeAdded, false);
this.gen = new HiveExpressionGenerator(globalState).setColumns(columnsToBeAdded);
}
public static SQLQueryAdapter generate(HiveGlobalState globalState, String tableName) {
HiveTableGenerator generator = new HiveTableGenerator(globalState, tableName);
return generator.create();
}
private SQLQueryAdapter create() {
ExpectedErrors errors = new ExpectedErrors();
sb.append("CREATE TABLE ");
sb.append(globalState.getDatabaseName());
sb.append(".");
sb.append(tableName);
sb.append(" (");
for (int i = 0; i < Randomly.smallNumber() + 1; i++) {
if (i != 0) {
sb.append(", ");
}
appendColumn(i);
}
sb.append(")");
// TODO: implement PARTITION BY clause
// TODO: implement CLUSTERED BY, SKEWED BY clauses
// TODO: implement ROW FORMAT and STORED AS clauses
// TODO: randomly add some predefined TABLEPROPERTIES
// TODO: implement CTAS (AS clause)
HiveErrors.addExpressionErrors(errors);
return new SQLQueryAdapter(sb.toString(), errors, true, false);
}
private void appendColumn(int columnId) {
String columnName = DBMSCommon.createColumnName(columnId);
sb.append(columnName);
sb.append(" ");
HiveDataType randType = HiveSchema.HiveDataType.getRandomType();
sb.append(randType);
columnsToBeAdded.add(new HiveColumn(columnName, table, randType));
appendColumnConstraint();
}
private void appendColumnConstraint() {
/*
* column_constraint_specification: : [ PRIMARY KEY|UNIQUE|NOT NULL|DEFAULT [default_value]|CHECK
* [check_expression] ENABLE|DISABLE NOVALIDATE RELY/NORELY ]
*/
if (Randomly.getBoolean()) {
// no column constraint
return;
}
ColumnConstraints constraint = Randomly.fromOptions(ColumnConstraints.values());
switch (constraint) {
case PRIMARY_KEY_DISABLE:
if (allowPrimaryKey && !setPrimaryKey) {
sb.append(" PRIMARY KEY DISABLE");
setPrimaryKey = true;
}
break;
case UNIQUE_DISABLE:
sb.append(" UNIQUE DISABLE");
break;
case NOT_NULL:
sb.append(" NOT NULL");
break;
case DEFAULT:
sb.append(" DEFAULT (");
sb.append(HiveToStringVisitor.asString(gen.generateConstant()));
sb.append(")");
case CHECK:
sb.append(" CHECK (");
sb.append(HiveToStringVisitor.asString(gen.generateExpression()));
sb.append(")");
break;
default:
throw new AssertionError(constraint);
}
}
}