forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClickHouseTypeParser.java
More file actions
284 lines (261 loc) · 8.87 KB
/
Copy pathClickHouseTypeParser.java
File metadata and controls
284 lines (261 loc) · 8.87 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
package sqlancer.clickhouse;
import java.util.HashMap;
import java.util.Map;
import sqlancer.clickhouse.ClickHouseType.Array;
import sqlancer.clickhouse.ClickHouseType.DateTime64Type;
import sqlancer.clickhouse.ClickHouseType.Decimal;
import sqlancer.clickhouse.ClickHouseType.FixedString;
import sqlancer.clickhouse.ClickHouseType.Kind;
import sqlancer.clickhouse.ClickHouseType.LowCardinality;
import sqlancer.clickhouse.ClickHouseType.Nullable;
import sqlancer.clickhouse.ClickHouseType.Primitive;
import sqlancer.clickhouse.ClickHouseType.Unknown;
public final class ClickHouseTypeParser {
private static final Map<String, Kind> PRIMITIVES = new HashMap<>();
static {
for (Kind k : Kind.values()) {
PRIMITIVES.put(k.name(), k);
}
}
private ClickHouseTypeParser() {
}
public static ClickHouseType parse(String typeString) {
if (typeString == null) {
return new Unknown("");
}
String trimmed = typeString.trim();
ClickHouseType parsed = tryParseRecognised(trimmed);
return parsed != null ? parsed : new Unknown(typeString);
}
private static ClickHouseType tryParseRecognised(String s) {
if (s.isEmpty()) {
return null;
}
String inner = stripSingleArgWrapper(s, "Nullable");
if (inner != null) {
ClickHouseType t = tryParseRecognised(inner.trim());
return t != null ? new Nullable(t) : null;
}
inner = stripSingleArgWrapper(s, "LowCardinality");
if (inner != null) {
ClickHouseType t = tryParseRecognised(inner.trim());
return t != null ? new LowCardinality(t) : null;
}
inner = stripSingleArgWrapper(s, "Array");
if (inner != null) {
ClickHouseType t = tryParseRecognised(inner.trim());
return t != null ? new Array(t) : null;
}
ClickHouseType fs = tryParseFixedString(s);
if (fs != null) {
return fs;
}
ClickHouseType dec = tryParseDecimal(s);
if (dec != null) {
return dec;
}
ClickHouseType dt64 = tryParseDateTime64(s);
if (dt64 != null) {
return dt64;
}
ClickHouseType agg = tryParseAggregateFunction(s);
if (agg != null) {
return agg;
}
ClickHouseType interval = tryParseInterval(s);
if (interval != null) {
return interval;
}
if (s.equals("DateTime")) {
return new Primitive(Kind.DateTime);
}
if (s.startsWith("DateTime(") && s.endsWith(")")) {
return new Primitive(Kind.DateTime);
}
Kind kind = PRIMITIVES.get(s);
if (kind != null) {
return new Primitive(kind);
}
return null;
}
private static String stripSingleArgWrapper(String s, String wrapperName) {
String prefix = wrapperName + "(";
if (!s.startsWith(prefix) || !s.endsWith(")")) {
return null;
}
int depth = 0;
boolean inString = false;
for (int i = prefix.length() - 1; i < s.length(); i++) {
char c = s.charAt(i);
if (inString) {
if (c == '\'') {
inString = false;
}
continue;
}
if (c == '\'') {
inString = true;
continue;
}
if (c == '(') {
depth++;
} else if (c == ')') {
depth--;
if (depth == 0 && i != s.length() - 1) {
return null;
}
}
}
return depth == 0 ? s.substring(prefix.length(), s.length() - 1) : null;
}
private static ClickHouseType tryParseFixedString(String s) {
if (!s.startsWith("FixedString(") || !s.endsWith(")")) {
return null;
}
String body = s.substring("FixedString(".length(), s.length() - 1).trim();
try {
int n = Integer.parseInt(body);
if (n < 1 || n > 256) {
return null;
}
return new FixedString(n);
} catch (NumberFormatException e) {
return null;
}
}
private static ClickHouseType tryParseDecimal(String s) {
if (!s.startsWith("Decimal") || !s.endsWith(")")) {
return null;
}
int parenIdx = s.indexOf('(');
if (parenIdx < 0) {
return null;
}
String head = s.substring(0, parenIdx);
String body = s.substring(parenIdx + 1, s.length() - 1).trim();
try {
if (head.equals("Decimal")) {
String[] parts = body.split(",");
if (parts.length != 2) {
return null;
}
int p = Integer.parseInt(parts[0].trim());
int sc = Integer.parseInt(parts[1].trim());
return new Decimal(p, sc);
}
int p;
switch (head) {
case "Decimal32":
p = 9;
break;
case "Decimal64":
p = 18;
break;
case "Decimal128":
p = 38;
break;
case "Decimal256":
p = 76;
break;
default:
return null;
}
int sc = Integer.parseInt(body);
return new Decimal(p, sc);
} catch (NumberFormatException e) {
return null;
}
}
private static ClickHouseType tryParseInterval(String s) {
if (!s.startsWith("Interval")) {
return null;
}
String kindName = s.substring("Interval".length());
if (kindName.isEmpty()) {
return null;
}
for (ClickHouseType.IntervalKind k : ClickHouseType.IntervalKind.values()) {
if (k.name().equals(kindName)) {
return new ClickHouseType.IntervalType(k);
}
}
return null;
}
private static ClickHouseType tryParseAggregateFunction(String s) {
boolean simple = s.startsWith("SimpleAggregateFunction(");
boolean full = !simple && s.startsWith("AggregateFunction(");
if (!simple && !full || !s.endsWith(")")) {
return null;
}
String wrapper = simple ? "SimpleAggregateFunction" : "AggregateFunction";
String body = s.substring(wrapper.length() + 1, s.length() - 1);
java.util.List<String> parts = splitTopLevel(body);
if (parts.size() < 2) {
return null;
}
String funcName = parts.get(0).trim();
if (funcName.isEmpty()) {
return null;
}
if (simple) {
if (parts.size() != 2) {
return null;
}
ClickHouseType inner = tryParseRecognised(parts.get(1).trim());
return inner != null ? new ClickHouseType.SimpleAggregateFunctionType(funcName, inner) : null;
}
java.util.List<ClickHouseType> args = new java.util.ArrayList<>();
for (int i = 1; i < parts.size(); i++) {
ClickHouseType a = tryParseRecognised(parts.get(i).trim());
if (a == null) {
return null;
}
args.add(a);
}
return new ClickHouseType.AggregateFunctionType(funcName, args);
}
private static java.util.List<String> splitTopLevel(String body) {
java.util.List<String> out = new java.util.ArrayList<>();
int depth = 0;
boolean inString = false;
int start = 0;
for (int i = 0; i < body.length(); i++) {
char c = body.charAt(i);
if (inString) {
if (c == '\'') {
inString = false;
}
continue;
}
if (c == '\'') {
inString = true;
} else if (c == '(') {
depth++;
} else if (c == ')') {
depth--;
} else if (c == ',' && depth == 0) {
out.add(body.substring(start, i));
start = i + 1;
}
}
out.add(body.substring(start));
return out;
}
private static ClickHouseType tryParseDateTime64(String s) {
if (!s.startsWith("DateTime64(") || !s.endsWith(")")) {
return null;
}
String body = s.substring("DateTime64(".length(), s.length() - 1).trim();
int comma = body.indexOf(',');
String precPart = comma < 0 ? body : body.substring(0, comma).trim();
try {
int prec = Integer.parseInt(precPart);
if (prec < 0 || prec > 9) {
return null;
}
return new DateTime64Type(prec);
} catch (NumberFormatException e) {
return null;
}
}
}