-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathSparkExpressionGenerator.java
More file actions
336 lines (279 loc) · 11.6 KB
/
Copy pathSparkExpressionGenerator.java
File metadata and controls
336 lines (279 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
package sqlancer.spark.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.spark.SparkGlobalState;
import sqlancer.spark.SparkSchema.SparkColumn;
import sqlancer.spark.SparkSchema.SparkDataType;
import sqlancer.spark.SparkSchema.SparkTable;
import sqlancer.spark.ast.SparkBetweenOperation;
import sqlancer.spark.ast.SparkBinaryOperation;
import sqlancer.spark.ast.SparkCaseOperation;
import sqlancer.spark.ast.SparkCastOperation;
import sqlancer.spark.ast.SparkColumnReference;
import sqlancer.spark.ast.SparkConstant;
import sqlancer.spark.ast.SparkExpression;
import sqlancer.spark.ast.SparkFunction;
import sqlancer.spark.ast.SparkInOperation;
import sqlancer.spark.ast.SparkJoin;
import sqlancer.spark.ast.SparkOrderingTerm;
import sqlancer.spark.ast.SparkSelect;
import sqlancer.spark.ast.SparkTableReference;
import sqlancer.spark.ast.SparkUnaryPostfixOperation;
import sqlancer.spark.ast.SparkUnaryPrefixOperation;
public class SparkExpressionGenerator extends UntypedExpressionGenerator<SparkExpression, SparkColumn>
implements TLPWhereGenerator<SparkSelect, SparkJoin, SparkExpression, SparkTable, SparkColumn> {
private final SparkGlobalState globalState;
private List<SparkTable> tables;
private enum Expression {
UNARY_PREFIX, UNARY_POSTFIX, BINARY_COMPARISON, BINARY_LOGICAL, BINARY_ARITHMETIC, CAST, FUNC, BETWEEN, IN,
CASE;
}
public SparkExpressionGenerator(SparkGlobalState globalState) {
this.globalState = globalState;
}
@Override
public SparkExpression negatePredicate(SparkExpression predicate) {
return new SparkUnaryPrefixOperation(predicate, SparkUnaryPrefixOperator.NOT);
}
@Override
public SparkExpression isNull(SparkExpression expr) {
return new SparkUnaryPostfixOperation(expr, SparkUnaryPostfixOperator.IS_NULL);
}
@Override
protected SparkExpression generateExpression(int depth) {
return generateExpressionInternal(depth);
}
private SparkExpression 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
SparkAggregateFunction aggregate = SparkAggregateFunction.getRandom();
return new SparkFunction<>(generateExpressions(aggregate.getNrArgs(), depth + 1), aggregate);
}
List<Expression> possibleOptions = new ArrayList<>(Arrays.asList(Expression.values()));
Expression expr = Randomly.fromList(possibleOptions);
switch (expr) {
case UNARY_PREFIX:
return new SparkUnaryPrefixOperation(generateExpression(depth + 1), SparkUnaryPrefixOperator.getRandom());
case UNARY_POSTFIX:
return new SparkUnaryPostfixOperation(generateExpression(depth + 1), SparkUnaryPostfixOperator.getRandom());
case BINARY_COMPARISON:
Operator op = SparkBinaryComparisonOperator.getRandom();
return new SparkBinaryOperation(generateExpression(depth + 1), generateExpression(depth + 1), op);
case BINARY_LOGICAL:
op = SparkBinaryLogicalOperator.getRandom();
return new SparkBinaryOperation(generateExpression(depth + 1), generateExpression(depth + 1), op);
case BINARY_ARITHMETIC:
return new SparkBinaryOperation(generateExpression(depth + 1), generateExpression(depth + 1),
SparkBinaryArithmeticOperator.getRandom());
case CAST:
return new SparkCastOperation(generateExpression(depth + 1), SparkDataType.getRandomType());
case FUNC:
SparkFunc func = SparkFunc.getRandom();
return new SparkFunction<>(generateExpressions(func.getNrArgs()), func);
case BETWEEN:
return new SparkBetweenOperation(generateExpression(depth + 1), generateExpression(depth + 1),
generateExpression(depth + 1), Randomly.getBoolean());
case IN:
return new SparkInOperation(generateExpression(depth + 1),
generateExpressions(Randomly.smallNumber() + 1, depth + 1), Randomly.getBoolean());
case CASE:
int nr = Randomly.smallNumber() + 1;
return new SparkCaseOperation(generateExpression(depth + 1), generateExpressions(nr, depth + 1),
generateExpressions(nr, depth + 1), generateExpression(depth + 1));
default:
throw new AssertionError(expr);
}
}
@Override
public SparkExpression generateConstant() {
if (Randomly.getBooleanWithRatherLowProbability()) {
return SparkConstant.createNullConstant();
}
SparkDataType[] values = SparkDataType.values();
SparkDataType constantType = Randomly.fromOptions(values);
switch (constantType) {
case STRING:
return SparkConstant.createStringConstant(globalState.getRandomly().getString());
case INTEGER:
return SparkConstant.createIntConstant(globalState.getRandomly().getInteger());
case DOUBLE:
return SparkConstant.createDoubleConstant(globalState.getRandomly().getDouble());
case BOOLEAN:
return SparkConstant.createBooleanConstant(Randomly.getBoolean());
case TIMESTAMP:
return SparkConstant.createTimestampConstant(globalState.getRandomly().getInteger());
case DATE:
return SparkConstant.createDateConstant(globalState.getRandomly().getInteger());
default:
throw new AssertionError(constantType);
}
}
@Override
protected SparkExpression generateColumn() {
SparkColumn column = Randomly.fromList(columns);
return new SparkColumnReference(column);
}
@Override
public List<SparkExpression> generateOrderBys() {
List<SparkExpression> expr = super.generateOrderBys();
List<SparkExpression> newExpr = new ArrayList<>(expr.size());
for (SparkExpression curExpr : expr) {
if (Randomly.getBoolean()) {
curExpr = new SparkOrderingTerm(curExpr, Ordering.getRandom());
}
newExpr.add(curExpr);
}
return newExpr;
}
@Override
public SparkExpressionGenerator setTablesAndColumns(AbstractTables<SparkTable, SparkColumn> tables) {
this.columns = tables.getColumns();
this.tables = tables.getTables();
return this;
}
@Override
public SparkExpression generateBooleanExpression() {
return generateExpression();
}
@Override
public SparkSelect generateSelect() {
return new SparkSelect();
}
@Override
public List<SparkExpression> getTableRefs() {
return tables.stream().map(t -> new SparkTableReference(t)).collect(Collectors.toList());
}
@Override
public List<SparkExpression> generateFetchColumns(boolean allowAggregates) {
if (Randomly.getBoolean()) {
return List.of(new SparkColumnReference(new SparkColumn("*", null, null)));
}
return Randomly.nonEmptySubset(columns).stream().map(c -> new SparkColumnReference(c))
.collect(Collectors.toList());
}
@Override
public List<SparkJoin> getRandomJoinClauses() {
return List.of();
}
public enum SparkUnaryPrefixOperator implements Operator {
NOT("NOT"), PLUS("+"), MINUS("-"), BITWISE_NOT("~");
private String textRepr;
SparkUnaryPrefixOperator(String textRepr) {
this.textRepr = textRepr;
}
public static SparkUnaryPrefixOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum SparkUnaryPostfixOperator implements Operator {
IS_NULL("IS NULL"), IS_NOT_NULL("IS NOT NULL");
private String textRepr;
SparkUnaryPostfixOperator(String textRepr) {
this.textRepr = textRepr;
}
public static SparkUnaryPostfixOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum SparkBinaryComparisonOperator implements Operator {
EQUALS("="), GREATER(">"), GREATER_EQUALS(">="), SMALLER("<"), SMALLER_EQUALS("<="), NOT_EQUALS("!="),
LIKE("LIKE"), NOT_LIKE("NOT LIKE"), RLIKE("RLIKE");
private String textRepr;
SparkBinaryComparisonOperator(String textRepr) {
this.textRepr = textRepr;
}
public static SparkBinaryComparisonOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum SparkBinaryLogicalOperator implements Operator {
AND("AND"), OR("OR");
private String textRepr;
SparkBinaryLogicalOperator(String textRepr) {
this.textRepr = textRepr;
}
public static SparkBinaryLogicalOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum SparkBinaryArithmeticOperator implements Operator {
// Spark supports || for concat, and bitwise operators &, |, ^
CONCAT("||"), ADD("+"), SUB("-"), MULT("*"), DIV("/"), MOD("%"), BITWISE_AND("&"), BITWISE_OR("|"),
BITWISE_XOR("^");
private String textRepr;
SparkBinaryArithmeticOperator(String textRepr) {
this.textRepr = textRepr;
}
public static SparkBinaryArithmeticOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return textRepr;
}
}
public enum SparkAggregateFunction {
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;
SparkAggregateFunction(int nrArgs) {
this.nrArgs = nrArgs;
}
public static SparkAggregateFunction getRandom() {
return Randomly.fromOptions(values());
}
public int getNrArgs() {
return nrArgs;
}
}
public enum SparkFunc {
ROUND(2), FLOOR(1), ABS(1), CEIL(1);
private int nrArgs;
private boolean isVariadic;
SparkFunc(int nrArgs) {
this(nrArgs, false);
}
SparkFunc(int nrArgs, boolean isVariadic) {
this.nrArgs = nrArgs;
this.isVariadic = isVariadic;
}
public static SparkFunc getRandom() {
return Randomly.fromOptions(values());
}
public int getNrArgs() {
if (isVariadic) {
return Randomly.smallNumber() + nrArgs;
} else {
return nrArgs;
}
}
}
}