forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFelderaConstant.java
More file actions
315 lines (253 loc) · 9.49 KB
/
FelderaConstant.java
File metadata and controls
315 lines (253 loc) · 9.49 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
package sqlancer.feldera.ast;
import sqlancer.Randomly;
import sqlancer.feldera.FelderaGlobalState;
import sqlancer.feldera.FelderaSchema;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
public abstract class FelderaConstant implements FelderaExpression {
private FelderaConstant() {
}
private static double round(double number, int places) {
BigDecimal decimal = new BigDecimal(number);
decimal = decimal.setScale(places, RoundingMode.HALF_UP);
return decimal.doubleValue();
}
public static FelderaConstant getRandomConstant(FelderaGlobalState globalState,
FelderaSchema.FelderaDataType type) {
switch (type) {
case BOOLEAN:
return new FelderaBooleanConstant(Randomly.getBoolean());
case TINYINT:
return FelderaIntConstant.getRandom(globalState, 8);
case SMALLINT:
return FelderaIntConstant.getRandom(globalState, 16);
case INT:
return FelderaIntConstant.getRandom(globalState, 32);
case BIGINT:
return FelderaIntConstant.getRandom(globalState, 64);
case VARCHAR:
return FelderaVarcharConstant.getRandom(globalState);
case CHAR:
return FelderaCharConstant.getRandom(globalState);
case NULL:
return new FelderaNullConstant();
case TIME:
return FelderaTimeConstant.getRandom(globalState);
case DATE:
return FelderaDateConstant.getRandom(globalState);
case TIMESTAMP:
return FelderaTimestampConstant.getRandom(globalState);
case REAL:
return FelderaRealConstant.getRandom(globalState);
case DOUBLE:
return FelderaDoubleConstant.getRandom(globalState);
default:
throw new AssertionError(type);
}
}
public static class FelderaTimeConstant extends FelderaConstant {
private final String value;
FelderaTimeConstant(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "'" + value + "'";
}
public static FelderaTimeConstant getRandom(FelderaGlobalState globalState) {
Randomly r = globalState.getRandomly();
int h = r.getInteger(0, 23);
int m = r.getInteger(0, 59);
int s = r.getInteger(0, 59);
return new FelderaTimeConstant(h + ":" + m + ":" + s);
}
}
public static class FelderaDateConstant extends FelderaConstant {
private final String value;
FelderaDateConstant(String value) {
this.value = value;
}
@Override
public String toString() {
return "'" + value + "'";
}
public static FelderaDateConstant getRandom(FelderaGlobalState globalState) {
Randomly r = globalState.getRandomly();
int year = r.getInteger(0, 9999);
int month = r.getInteger(1, 12);
int day = r.getInteger(1, 31);
return new FelderaDateConstant(year + "-" + month + "-" + day);
}
}
public static class FelderaTimestampConstant extends FelderaConstant {
private final String value;
FelderaTimestampConstant(String value) {
this.value = value;
}
@Override
public String toString() {
return "'" + value + "'";
}
public static FelderaTimestampConstant getRandom(FelderaGlobalState globalState) {
String date = FelderaDateConstant.getRandom(globalState).value;
String time = FelderaTimeConstant.getRandom(globalState).value;
return new FelderaTimestampConstant(date + " " + time);
}
}
private static class FelderaNullConstant extends FelderaConstant {
@Override
public String toString() {
return "NULL";
}
}
public static class FelderaIntConstant extends FelderaConstant {
private final long value;
public FelderaIntConstant(long value) {
this.value = value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public long getValue() {
return value;
}
public static FelderaIntConstant getRandom(FelderaGlobalState globalState) {
return new FelderaIntConstant(globalState.getRandomly().getInteger());
}
public static FelderaIntConstant getRandom(FelderaGlobalState globalState, int bitLength) {
// int left = -(1 << (bitLength - 1));
// int right = (1 << (bitLength - 1)) - 1;
// return new FelderaIntConstant(globalState.getRandomly().getLong(left, right));
// HACK: for now, generate just a small random number that isn't 0
return new FelderaIntConstant(Randomly.smallNumber() + 1);
}
}
public static class FelderaDoubleConstant extends FelderaConstant {
private final double value;
public FelderaDoubleConstant(double value) {
this.value = value;
}
public double getValue() {
return value;
}
@Override
public String toString() {
if (value == Double.POSITIVE_INFINITY) {
return "'+Inf'";
} else if (value == Double.NEGATIVE_INFINITY) {
return "'-Inf'";
}
return String.valueOf(value);
}
public static FelderaDoubleConstant getRandom(FelderaGlobalState globalState) {
return new FelderaDoubleConstant(
FelderaConstant.round(globalState.getRandomly().getFiniteDouble() + 1.0, 10));
}
}
public static class FelderaRealConstant extends FelderaConstant {
private final float value;
public FelderaRealConstant(float value) {
this.value = value;
}
public float getValue() {
return value;
}
@Override
public String toString() {
if (value == Float.POSITIVE_INFINITY) {
return "'+Inf'";
} else if (value == Float.NEGATIVE_INFINITY) {
return "'-Inf'";
}
return String.valueOf(value);
}
public static FelderaRealConstant getRandom(FelderaGlobalState globalState) {
return new FelderaRealConstant(
((float) FelderaConstant.round(globalState.getRandomly().getFiniteDouble() + 1.0, 5)));
}
}
public static class FelderaVarcharConstant extends FelderaConstant {
private final String value;
public FelderaVarcharConstant(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "'" + value.replace("'", "''") + "'";
}
private static String getRandomString(FelderaGlobalState globalState) {
return globalState.getRandomly().getString().replaceAll("[^a-zA-Z0-9]", "");
}
public static FelderaVarcharConstant getRandom(FelderaGlobalState globalState) {
String randomString = getRandomString(globalState);
// retry for 10 times, but if it's still empty, just use a default string
for (int i = 0; i < 10; i++) {
if (!randomString.isBlank()) {
break;
}
randomString = getRandomString(globalState);
}
if (randomString.isBlank()) {
randomString = "DEFAULT STRING";
}
return new FelderaVarcharConstant(randomString);
}
}
public static class FelderaCharConstant extends FelderaConstant {
private final char value;
public FelderaCharConstant(char value) {
this.value = value;
}
public char getValue() {
return value;
}
@Override
public String toString() {
return "'" + this.value + "'";
}
public static FelderaCharConstant getRandom(FelderaGlobalState globalState) {
char ch = globalState.getRandomly().getAlphabeticChar().charAt(0);
while (true) {
if (StandardCharsets.ISO_8859_1.newEncoder().canEncode(ch)) {
return new FelderaCharConstant(ch);
}
}
}
}
public static class FelderaBooleanConstant extends FelderaConstant {
private final boolean value;
public FelderaBooleanConstant(boolean value) {
this.value = value;
}
public boolean getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
}
public static FelderaExpression createNullConstant() {
return new FelderaNullConstant();
}
public static FelderaExpression createVarcharConstant(String text) {
return new FelderaVarcharConstant(text);
}
public static FelderaExpression createDoubleConstant(double val) {
return new FelderaDoubleConstant(val);
}
public static FelderaExpression createIntConstant(long val) {
return new FelderaIntConstant(val);
}
public static FelderaExpression createBooleanConstant(boolean val) {
return new FelderaBooleanConstant(val);
}
}