-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathSQLite3Function.java
More file actions
355 lines (314 loc) · 12.2 KB
/
SQLite3Function.java
File metadata and controls
355 lines (314 loc) · 12.2 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
353
354
355
package sqlancer.sqlite3.ast;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.sqlite3.ast.SQLite3Constant.SQLite3TextConstant;
import sqlancer.sqlite3.schema.SQLite3DataType;
import sqlancer.sqlite3.schema.SQLite3Schema.SQLite3Column.SQLite3CollateSequence;
public class SQLite3Function extends SQLite3Expression {
private final ComputableFunction func;
private final SQLite3Expression[] args;
public SQLite3Function(ComputableFunction func, SQLite3Expression... args) {
this.func = func;
this.args = args.clone();
}
public enum ComputableFunction {
ABS(1, "ABS") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
SQLite3Constant castValue;
if (args[0].getDataType() == SQLite3DataType.BINARY) {
throw new IgnoreMeException(); // TODO
// implement
}
if (args[0].getDataType() == SQLite3DataType.INT) {
castValue = SQLite3Cast.castToInt(args[0]);
} else {
castValue = SQLite3Cast.castToNumericNoNumAsRealZero(args[0]);
}
if (castValue.isNull()) {
return castValue;
} else if (castValue.getDataType() == SQLite3DataType.INT) {
long absVal = Math.abs(castValue.asInt());
return SQLite3Constant.createIntConstant(absVal);
} else {
assert castValue.getDataType() == SQLite3DataType.REAL;
double absVal = Math.abs(castValue.asDouble());
return SQLite3Constant.createRealConstant(absVal);
}
}
},
COALESCE(2, "COALESCE") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
for (SQLite3Constant arg : args) {
if (!arg.isNull()) {
return arg;
}
}
return SQLite3Constant.createNullConstant();
}
@Override
public boolean isVariadic() {
return true;
}
},
HEX(1, "HEX") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return null;
// SQLite3Constant binaryValue = SQLite3Cast.castToBlob(args[0]);
// return
// SQLite3Constant.createTextConstant(binaryValue.getStringRepresentation());
}
},
LOWER(1, "LOWER") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
args[0] = SQLite3Cast.castToText(args[0]);
if (args[0] == null) {
return null;
}
if (args[0].getDataType() == SQLite3DataType.TEXT) {
StringBuilder text = new StringBuilder(args[0].asString());
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c >= 'A' && c <= 'Z') {
text.setCharAt(i, Character.toLowerCase(c));
}
}
return SQLite3Constant.createTextConstant(text.toString());
} else {
return SQLite3Constant.createNullConstant();
}
}
},
LIKELY(1, "LIKELY") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return args[0];
}
},
LIKELIHOOD(2, "LIKELIHOOD") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return args[0];
}
},
IFNULL(2, "IFNULL") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
for (SQLite3Expression arg : args) {
if (!arg.getExpectedValue().isNull()) {
return arg.getExpectedValue();
}
}
return SQLite3Constant.createNullConstant();
}
},
UPPER(1, "UPPER") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
args[0] = SQLite3Cast.castToText(args[0]);
if (args[0] == null) {
return null;
}
if (args[0].getDataType() == SQLite3DataType.TEXT) {
String string = SQLite3TextConstant.toUpper(args[0].asString());
return SQLite3Constant.createTextConstant(string);
} else {
return SQLite3Constant.createNullConstant();
}
}
},
NULLIF(2, "NULLIF") {
@Override
public SQLite3Constant apply(SQLite3Constant[] args, SQLite3CollateSequence collateSequence) {
SQLite3Constant equals = args[0].applyEquals(args[1],
collateSequence == null ? SQLite3CollateSequence.BINARY : collateSequence);
if (SQLite3Cast.isTrue(equals).isPresent() && SQLite3Cast.isTrue(equals).get()) {
return SQLite3Constant.createNullConstant();
} else {
return args[0];
}
}
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
SQLite3CollateSequence collateSequence = null;
for (SQLite3Constant con : args) {
if (con.getExplicitCollateSequence() != null) {
collateSequence = con.getExplicitCollateSequence();
break;
}
}
if (collateSequence == null) {
for (SQLite3Constant con : args) {
if (con.getImplicitCollateSequence() != null) {
collateSequence = con.getImplicitCollateSequence();
break;
}
}
}
return apply(args, collateSequence);
}
},
TRIM(1, "TRIM") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
SQLite3Constant str = SQLite3Cast.castToText(args[0]);
if (args[0].getDataType() == SQLite3DataType.TEXT) {
String text = str.asString();
return SQLite3Constant.createTextConstant(SQLite3TextConstant.trim(text));
} else {
return str;
}
}
},
TRIM_TWO_ARGS(2, "TRIM") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
if (args[0].isNull() || args[1].isNull()) {
return SQLite3Constant.createNullConstant();
}
SQLite3Constant str = SQLite3Cast.castToText(args[0]);
SQLite3Constant castToText = SQLite3Cast.castToText(args[1]);
if (str == null || castToText == null) {
return null;
}
String remove = castToText.asString();
StringBuilder text = new StringBuilder(str.asString());
int i = 0;
while (i < text.length()) {
boolean shouldRemoveChar = false;
char c = text.charAt(i);
for (char charToRemove : remove.toCharArray()) {
if (charToRemove == c) {
shouldRemoveChar = true;
break;
}
}
if (shouldRemoveChar) {
text.deleteCharAt(i);
} else {
break;
}
}
i = text.length() - 1;
while (i >= 0) {
boolean shouldRemoveChar = false;
char c = text.charAt(i);
for (char charToRemove : remove.toCharArray()) {
if (charToRemove == c) {
shouldRemoveChar = true;
break;
}
}
if (shouldRemoveChar) {
text.deleteCharAt(i);
i--;
} else {
break;
}
}
String string = text.toString();
assert string != null;
return SQLite3Constant.createTextConstant(string);
}
},
TYPEOF(1, "TYPEOF") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
switch (args[0].getDataType()) {
case BINARY:
return SQLite3Constant.createTextConstant("blob");
case INT:
return SQLite3Constant.createTextConstant("integer");
case NULL:
return SQLite3Constant.createTextConstant("null");
case REAL:
return SQLite3Constant.createTextConstant("real");
case TEXT:
return SQLite3Constant.createTextConstant("text");
default:
throw new AssertionError(args[0]);
}
}
},
UNLIKELY(1, "UNLIKELY") {
@Override
public SQLite3Constant apply(SQLite3Constant... args) {
return args[0];
}
};
private String functionName;
final int nrArgs;
ComputableFunction(int nrArgs, String functionName) {
this.nrArgs = nrArgs;
this.functionName = functionName;
}
/**
* Gets the number of arguments if the function is non-variadic. If the function is variadic, the minimum number
* of arguments is returned.
*
* @return the number of arguments
*/
public int getNrArgs() {
return nrArgs;
}
public abstract SQLite3Constant apply(SQLite3Constant... args);
public SQLite3Constant apply(SQLite3Constant[] evaluatedArgs, SQLite3CollateSequence collate) {
return apply(evaluatedArgs);
}
public static ComputableFunction getRandomFunction() {
return Randomly.fromOptions(ComputableFunction.values());
}
@Override
public String toString() {
return functionName;
}
public boolean isVariadic() {
return false;
}
public TypeAffinity getAffinity(SQLite3Expression... args) {
return TypeAffinity.NONE;
}
}
@Override
public SQLite3CollateSequence getExplicitCollateSequence() {
for (SQLite3Expression expr : args) {
if (expr.getExplicitCollateSequence() != null) {
return expr.getExplicitCollateSequence();
}
}
return null;
}
public SQLite3Expression[] getArgs() {
return args.clone();
}
public ComputableFunction getFunc() {
return func;
}
@Override
public SQLite3Constant getExpectedValue() {
SQLite3Constant[] constants = new SQLite3Constant[args.length];
for (int i = 0; i < constants.length; i++) {
constants[i] = args[i].getExpectedValue();
if (constants[i] == null) {
return null;
}
}
SQLite3CollateSequence collate = getExplicitCollateSequence();
if (collate == null) {
for (SQLite3Expression arg : args) {
if (arg.getImplicitCollateSequence() != null) {
collate = arg.getImplicitCollateSequence();
break;
}
}
}
return func.apply(constants, collate);
};
@Override
public TypeAffinity getAffinity() {
return func.getAffinity(args);
}
}