This repository was archived by the owner on Apr 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataFusionConstant.java
More file actions
289 lines (235 loc) · 8.65 KB
/
DataFusionConstant.java
File metadata and controls
289 lines (235 loc) · 8.65 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
package sqlancer.datafusion.ast;
import sqlancer.common.ast.newast.Node;
public class DataFusionConstant implements Node<DataFusionExpression> {
private DataFusionConstant() {
}
public static Node<DataFusionExpression> createIntConstant(long val) {
return new DataFusionIntConstant(val);
}
public static Node<DataFusionExpression> createNullConstant() {
return new DataFusionNullConstant();
}
public static Node<DataFusionExpression> createDateConstant(long daysFromEpoch) {
return new DataFusionDateConstant(daysFromEpoch);
}
public static Node<DataFusionExpression> createTimestampConstant(long secondsSinceEpoch) {
return new DataFusionTimestampConstant(secondsSinceEpoch);
}
public static Node<DataFusionExpression> createTimeConstant(long secondsInDay) {
return new DataFusionTimeConstant(secondsInDay);
}
public static class DataFusionNullConstant extends DataFusionConstant {
@Override
public String toString() {
return "NULL";
}
}
public static class DataFusionIntConstant extends DataFusionConstant {
private final long value;
public DataFusionIntConstant(long value) {
this.value = value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public long getValue() {
return value;
}
}
public static class DataFusionDoubleConstant extends DataFusionConstant {
private final String valueStr;
public DataFusionDoubleConstant(double value) {
if (value == Double.POSITIVE_INFINITY) {
valueStr = "'+Inf'::Double";
} else if (value == Double.NEGATIVE_INFINITY) {
valueStr = "'-Inf'::Double";
} else if (Double.isNaN(value)) {
valueStr = "'NaN'::Double";
} else if (Double.compare(value, -0.0) == 0) {
valueStr = "-0.0";
} else {
valueStr = String.valueOf(value);
}
}
// Make it more convenient to construct special value like -0, NaN, etc.
public DataFusionDoubleConstant(String valueStr) {
this.valueStr = valueStr;
}
@Override
public String toString() {
return valueStr;
}
}
public static class DataFusionBooleanConstant extends DataFusionConstant {
private final boolean value;
public DataFusionBooleanConstant(boolean value) {
this.value = value;
}
public boolean getValue() {
return value;
}
@Override
public String toString() {
if (value) {
return "true";
} else {
return "false";
}
}
}
public static class DataFusionStringConstant extends DataFusionConstant {
private final String value;
public static String cleanString(String input) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
// Check if the character is a high surrogate
if (Character.isHighSurrogate(c)) {
if (i + 1 < input.length() && Character.isLowSurrogate(input.charAt(i + 1))) {
// It's a valid surrogate pair, add both to the string
sb.append(c);
sb.append(input.charAt(i + 1));
i++; // Skip the next character as it's part of the surrogate pair
}
} else if (!Character.isLowSurrogate(c) && !Character.isSurrogate(c)) {
// Add only if it's not a low surrogate or any standalone surrogate
sb.append(c);
}
}
return sb.toString();
}
public DataFusionStringConstant(String value) {
// cleanup invalid Utf8
this.value = cleanString(value.replace("'", "''"));
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "'" + value + "'";
}
}
public static class DataFusionDateConstant extends DataFusionConstant {
private final String value;
public DataFusionDateConstant(long daysFromEpoch) {
// Convert days from epoch to date string
// Epoch is 1970-01-01, so we add/subtract days
long totalDays = daysFromEpoch;
// Simple date calculation (not accounting for all edge cases, but good enough for fuzzing)
// Start from 1970-01-01
int year = 1970;
int month = 1;
int day = 1;
// Approximate calculation for fuzzing purposes
if (totalDays >= 0) {
year += (int) (totalDays / 365);
totalDays = totalDays % 365;
month = (int) (totalDays / 30) + 1;
day = (int) (totalDays % 30) + 1;
} else {
totalDays = -totalDays;
year -= (int) (totalDays / 365);
totalDays = totalDays % 365;
month = (int) (totalDays / 30) + 1;
day = (int) (totalDays % 30) + 1;
}
// Keep values in valid ranges
if (year < 1) {
year = 1;
}
if (year > 9999) {
year = 9999;
}
if (month < 1) {
month = 1;
}
if (month > 12) {
month = 12;
}
if (day < 1) {
day = 1;
}
if (day > 28) {
day = 28; // Safe for all months
}
this.value = String.format("%04d-%02d-%02d", year, month, day);
}
@Override
public String toString() {
return "DATE '" + value + "'";
}
}
public static class DataFusionTimestampConstant extends DataFusionConstant {
private final String value;
public DataFusionTimestampConstant(long secondsSinceEpoch) {
// Convert seconds to timestamp string YYYY-MM-DD HH:MM:SS
// Simple approximation for fuzzing
long totalSeconds = Math.abs(secondsSinceEpoch);
long seconds = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long minutes = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long hours = totalHours % 24;
long totalDays = totalHours / 24;
// Calculate date from days (approximate)
int year = 1970;
int month = 1;
int day = 1;
if (secondsSinceEpoch >= 0) {
year += (int) (totalDays / 365);
totalDays = totalDays % 365;
month = (int) (totalDays / 30) + 1;
day = (int) (totalDays % 30) + 1;
} else {
year -= (int) (totalDays / 365);
totalDays = totalDays % 365;
month = (int) (totalDays / 30) + 1;
day = (int) (totalDays % 30) + 1;
}
// Keep in valid ranges
if (year < 1) {
year = 1;
}
if (year > 9999) {
year = 9999;
}
if (month < 1) {
month = 1;
}
if (month > 12) {
month = 12;
}
if (day < 1) {
day = 1;
}
if (day > 28) {
day = 28;
}
this.value = String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hours, minutes, seconds);
}
@Override
public String toString() {
return "TIMESTAMP '" + value + "'";
}
}
public static class DataFusionTimeConstant extends DataFusionConstant {
private final String value;
public DataFusionTimeConstant(long secondsInDay) {
// Convert seconds to HH:MM:SS
long totalSeconds = secondsInDay % 86400; // Ensure within a day
if (totalSeconds < 0) {
totalSeconds += 86400;
}
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
this.value = String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
@Override
public String toString() {
return "TIME '" + value + "'";
}
}
}