-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathHiveExpressionGenerator.java
More file actions
352 lines (284 loc) · 11.6 KB
/
HiveExpressionGenerator.java
File metadata and controls
352 lines (284 loc) · 11.6 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package sqlancer.hive.gen;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import sqlancer.Randomly;
import sqlancer.common.ast.BinaryOperatorNode.Operator;
import sqlancer.common.ast.newast.NewOrderingTerm.Ordering;
import sqlancer.common.gen.TLPWhereGenerator;
import sqlancer.common.gen.UntypedExpressionGenerator;
import sqlancer.common.schema.AbstractTables;
import sqlancer.hive.HiveGlobalState;
import sqlancer.hive.HiveSchema.HiveColumn;
import sqlancer.hive.HiveSchema.HiveDataType;
import sqlancer.hive.HiveSchema.HiveTable;
import sqlancer.hive.ast.HiveBetweenOperation;
import sqlancer.hive.ast.HiveBinaryOperation;
import sqlancer.hive.ast.HiveCaseOperation;
import sqlancer.hive.ast.HiveCastOperation;
import sqlancer.hive.ast.HiveColumnReference;
import sqlancer.hive.ast.HiveConstant;
import sqlancer.hive.ast.HiveExpression;
import sqlancer.hive.ast.HiveFunction;
import sqlancer.hive.ast.HiveInOperation;
import sqlancer.hive.ast.HiveJoin;
import sqlancer.hive.ast.HiveOrderingTerm;
import sqlancer.hive.ast.HiveSelect;
import sqlancer.hive.ast.HiveTableReference;
import sqlancer.hive.ast.HiveUnaryPostfixOperation;
import sqlancer.hive.ast.HiveUnaryPrefixOperation;
public class HiveExpressionGenerator extends UntypedExpressionGenerator<HiveExpression, HiveColumn>
implements TLPWhereGenerator<HiveSelect, HiveJoin, HiveExpression, HiveTable, HiveColumn> {
private final HiveGlobalState globalState;
private List<HiveTable> tables;
private enum Expression {
// TODO: add or delete expressions.
UNARY_PREFIX, UNARY_POSTFIX, BINARY_COMPARISON, BINARY_LOGICAL, BINARY_ARITHMETIC, CAST, FUNC, BETWEEN, IN,
CASE;
}
public HiveExpressionGenerator(HiveGlobalState globalState) {
this.globalState = globalState;
}
@Override
public HiveExpression negatePredicate(HiveExpression predicate) {
return new HiveUnaryPrefixOperation(predicate, HiveUnaryPrefixOperator.NOT);
}
@Override
public HiveExpression isNull(HiveExpression expr) {
return new HiveUnaryPostfixOperation(expr, HiveUnaryPostfixOperator.IS_NULL);
}
@Override
protected HiveExpression generateExpression(int depth) {
// TODO: randomly cast some types like what PostgresExpressionGenerator does?
return generateExpressionInternal(depth);
}
private HiveExpression generateExpressionInternal(int depth) throws AssertionError {
if (depth >= globalState.getOptions().getMaxExpressionDepth()
|| Randomly.getBooleanWithRatherLowProbability()) {
return generateLeafNode();
}
if (allowAggregates && Randomly.getBooleanWithRatherLowProbability()) {
allowAggregates = false; // aggregate function calls cannot be nested
HiveAggregateFunction aggregate = HiveAggregateFunction.getRandom();
return new HiveFunction<>(generateExpressions(aggregate.getNrArgs(), depth + 1), aggregate);
}
List<Expression> possibleOptions = new ArrayList<>(Arrays.asList(Expression.values()));
// TODO: remove some of the possible expression types according to options.
Expression expr = Randomly.fromList(possibleOptions);
switch (expr) {
case UNARY_PREFIX:
return new HiveUnaryPrefixOperation(generateExpression(depth + 1), HiveUnaryPrefixOperator.getRandom());
case UNARY_POSTFIX:
return new HiveUnaryPostfixOperation(generateExpression(depth + 1), HiveUnaryPostfixOperator.getRandom());
case BINARY_COMPARISON:
Operator op = HiveBinaryComparisonOperator.getRandom();
return new HiveBinaryOperation(generateExpression(depth + 1), generateExpression(depth + 1), op);
case BINARY_LOGICAL:
op = HiveExpressionGenerator.HiveBinaryLogicalOperator.getRandom();
return new HiveBinaryOperation(generateExpression(depth + 1), generateExpression(depth + 1), op);
case BINARY_ARITHMETIC:
return new HiveBinaryOperation(generateExpression(depth + 1), generateExpression(depth + 1),
HiveExpressionGenerator.HiveBinaryArithmeticOperator.getRandom());
case CAST:
return new HiveCastOperation(generateExpression(depth + 1), HiveDataType.getRandomType());
case FUNC:
HiveFunc func = HiveFunc.getRandom();
return new HiveFunction<>(generateExpressions(func.getNrArgs()), func);
case BETWEEN:
return new HiveBetweenOperation(generateExpression(depth + 1), generateExpression(depth + 1),
generateExpression(depth + 1), Randomly.getBoolean());
case IN:
return new HiveInOperation(generateExpression(depth + 1),
generateExpressions(Randomly.smallNumber() + 1, depth + 1), Randomly.getBoolean());
case CASE:
int nr = Randomly.smallNumber() + 1;
return new HiveCaseOperation(generateExpression(depth + 1), generateExpressions(nr, depth + 1),
generateExpressions(nr, depth + 1), generateExpression(depth + 1));
default:
throw new AssertionError(expr);
}
}
@Override
public HiveExpression generateConstant() {
if (Randomly.getBooleanWithRatherLowProbability()) {
return HiveConstant.createNullConstant();
}
HiveDataType[] values = HiveDataType.values();
HiveDataType constantType = Randomly.fromOptions(values);
switch (constantType) {
case STRING:
return HiveConstant.createStringConstant(globalState.getRandomly().getString());
case INT:
return HiveConstant.createIntConstant(globalState.getRandomly().getInteger());
case DOUBLE:
return HiveConstant.createDoubleConstant(globalState.getRandomly().getDouble());
case BOOLEAN:
return HiveConstant.createBooleanConstant(Randomly.getBoolean());
default:
throw new AssertionError(constantType);
}
}
@Override
protected HiveExpression generateColumn() {
HiveColumn column = Randomly.fromList(columns);
return new HiveColumnReference(column);
}
@Override
public List<HiveExpression> generateOrderBys() {
List<HiveExpression> expr = super.generateOrderBys();
List<HiveExpression> newExpr = new ArrayList<>(expr.size());
for (HiveExpression curExpr : expr) {
if (Randomly.getBoolean()) {
curExpr = new HiveOrderingTerm(curExpr, Ordering.getRandom());
}
newExpr.add(curExpr);
}
return newExpr;
}
@Override
public HiveExpressionGenerator setTablesAndColumns(AbstractTables<HiveTable, HiveColumn> tables) {
this.columns = tables.getColumns();
this.tables = tables.getTables();
return this;
}
@Override
public HiveExpression generateBooleanExpression() {
return generateExpression();
}
@Override
public HiveSelect generateSelect() {
return new HiveSelect();
}
@Override
public List<HiveExpression> getTableRefs() {
return tables.stream().map(t -> new HiveTableReference(t)).collect(Collectors.toList());
}
@Override
public List<HiveExpression> generateFetchColumns(boolean allowAggregates) {
if (Randomly.getBoolean()) {
return List.of(new HiveColumnReference(new HiveColumn("*", null, null)));
}
return Randomly.nonEmptySubset(columns).stream().map(c -> new HiveColumnReference(c))
.collect(Collectors.toList());
}
@Override
public List<HiveJoin> getRandomJoinClauses() {
return List.of();
}
public enum HiveUnaryPrefixOperator implements Operator {
// TODO: ~A (bitwise NOT)
NOT("NOT"), PLUS("+"), MINUS("-");
private String textRepr;
HiveUnaryPrefixOperator(String textRepr) {
this.textRepr = textRepr;
}
public static HiveUnaryPrefixOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum HiveUnaryPostfixOperator implements Operator {
// TODO: A IS [NOT] (NULL|TRUE|FALSE)...
IS_NULL("IS NULL"), IS_NOT_NULL("IS NOT NULL");
private String textRepr;
HiveUnaryPostfixOperator(String textRepr) {
this.textRepr = textRepr;
}
public static HiveUnaryPostfixOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum HiveBinaryComparisonOperator implements Operator {
EQUALS("="), GREATER(">"), GREATER_EQUALS(">="), SMALLER("<"), SMALLER_EQUALS("<="), NOT_EQUALS("!="),
LIKE("LIKE"), NOT_LIKE("NOT LIKE"), REGEXP("RLIKE");
private String textRepr;
HiveBinaryComparisonOperator(String textRepr) {
this.textRepr = textRepr;
}
public static HiveBinaryComparisonOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum HiveBinaryLogicalOperator implements Operator {
AND("AND"), OR("OR");
private String textRepr;
HiveBinaryLogicalOperator(String textRepr) {
this.textRepr = textRepr;
}
public static HiveBinaryLogicalOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum HiveBinaryArithmeticOperator implements Operator {
CONCAT("||"), ADD("+"), SUB("-"), MULT("*"), DIV("/"), MOD("%"), BITWISE_AND("&"), BITWISE_OR("|"),
BITWISE_XOR("^");
private String textRepr;
HiveBinaryArithmeticOperator(String textRepr) {
this.textRepr = textRepr;
}
public static HiveBinaryArithmeticOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum HiveAggregateFunction {
COUNT(1), SUM(1), AVG(1), MIN(1), MAX(1), VARIANCE(1), VAR_SAMP(1), STDDEV_POP(1), STDDEV_SAMP(1), COVAR_POP(2),
COVAR_SAMP(2), CORR(2);
private int nrArgs;
HiveAggregateFunction(int nrArgs) {
this.nrArgs = nrArgs;
}
public static HiveAggregateFunction getRandom() {
return Randomly.fromOptions(values());
}
public int getNrArgs() {
return nrArgs;
}
}
// TODO: test all Hive default functions...
public enum HiveFunc {
// mathematical functions
ROUND(2), FLOOR(1);
// collection functions
// date functions
// string functions
private int nrArgs;
private boolean isVariadic;
HiveFunc(int nrArgs) {
this(nrArgs, false);
}
HiveFunc(int nrArgs, boolean isVariadic) {
this.nrArgs = nrArgs;
this.isVariadic = isVariadic;
}
public static HiveFunc getRandom() {
return Randomly.fromOptions(values());
}
public int getNrArgs() {
if (isVariadic) {
return Randomly.smallNumber() + nrArgs;
} else {
return nrArgs;
}
}
}
}