-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathClickHouseCast.java
More file actions
257 lines (233 loc) · 10 KB
/
ClickHouseCast.java
File metadata and controls
257 lines (233 loc) · 10 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
package sqlancer.clickhouse.ast;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Optional;
import java.util.regex.Pattern;
import com.clickhouse.client.ClickHouseDataType;
import sqlancer.clickhouse.ast.constant.ClickHouseCreateConstant;
public final class ClickHouseCast extends ClickHouseExpression {
private static final double MAX_INT_FOR_WHICH_CONVERSION_TO_INT_IS_TRIED = Math.pow(2, 51 - 1) - 1;
private static final double MIN_INT_FOR_WHICH_CONVERSION_TO_INT_IS_TRIED = -Math.pow(2, 51 - 1);
private static final byte FILE_SEPARATOR = 0x1c;
private static final byte GROUP_SEPARATOR = 0x1d;
private static final byte RECORD_SEPARATOR = 0x1e;
private static final byte UNIT_SEPARATOR = 0x1f;
private static final byte SYNCHRONOUS_IDLE = 0x16;
static Connection castDatabase;
private ClickHouseCast() {
}
public static Optional<Boolean> isTrue(ClickHouseConstant value) {
ClickHouseConstant numericValue;
if (value.getDataType() == ClickHouseDataType.Nothing) {
return Optional.empty();
}
if (value.getDataType() == ClickHouseDataType.String) {
numericValue = castToNumeric(value);
} else {
numericValue = value;
}
assert numericValue.getDataType() != ClickHouseDataType.String : numericValue + "should have been converted";
switch (numericValue.getDataType()) {
case Int32:
return Optional.of(numericValue.asInt() != 0);
case Float64:
double doubleVal = numericValue.asDouble();
return Optional.of(doubleVal != 0 && !Double.isNaN(doubleVal));
default:
throw new AssertionError(numericValue);
}
}
// SELECT CAST('-1.370998801E9' AS INTEGER) == -1
public static ClickHouseConstant castToInt(ClickHouseConstant cons) {
switch (cons.getDataType()) {
case Nothing:
return ClickHouseCreateConstant.createNullConstant();
case Int32:
return cons;
case Float64:
return ClickHouseCreateConstant.createInt32Constant((long) cons.asDouble());
case String:
String asString = cons.asString();
while (startsWithWhitespace(asString)) {
asString = asString.substring(1);
}
if (!asString.isEmpty() && unprintAbleCharThatLetsBecomeNumberZero(asString)) {
return ClickHouseCreateConstant.createInt32Constant(0);
}
for (int i = asString.length(); i >= 0; i--) {
try {
String substring = asString.substring(0, i);
Pattern p = Pattern.compile("[+-]?\\d\\d*");
if (p.matcher(substring).matches()) {
BigDecimal bg = new BigDecimal(substring);
long result;
try {
result = bg.longValueExact();
} catch (ArithmeticException e) {
if (substring.startsWith("-")) {
result = Long.MIN_VALUE;
} else {
result = Long.MAX_VALUE;
}
}
return ClickHouseCreateConstant.createInt32Constant(result);
}
} catch (Exception e) {
}
}
return ClickHouseCreateConstant.createInt32Constant(0);
default:
throw new AssertionError();
}
}
public static ClickHouseConstant castToReal(ClickHouseConstant cons) {
ClickHouseConstant numericValue = castToNumeric(cons);
if (numericValue.getDataType() == ClickHouseDataType.Int32) {
return ClickHouseCreateConstant.createFloat64Constant(numericValue.asInt());
} else {
return numericValue;
}
}
public static ClickHouseConstant castToNumericNoNumAsRealZero(ClickHouseConstant value) {
return convertInternal(value, false, true, true);
}
public static ClickHouseConstant castToNumericFromNumOperand(ClickHouseConstant value) {
return convertInternal(value, false, false, false);
}
/*
* Applies numeric affinity to a value.
*/
public static ClickHouseConstant castToNumeric(ClickHouseConstant value) {
return convertInternal(value, true, false, false);
}
private static ClickHouseConstant convertInternal(ClickHouseConstant value, boolean convertRealToInt,
boolean noNumIsRealZero, boolean convertIntToReal) throws AssertionError {
switch (value.getDataType()) {
case Nothing:
return ClickHouseCreateConstant.createNullConstant();
case Int32:
case Float64:
return value;
case String:
String asString = value.asString();
while (startsWithWhitespace(asString)) {
asString = asString.substring(1);
}
if (!asString.isEmpty() && unprintAbleCharThatLetsBecomeNumberZero(asString)) {
return ClickHouseCreateConstant.createInt32Constant(0);
}
if (asString.toLowerCase().startsWith("-infinity") || asString.toLowerCase().startsWith("infinity")
|| asString.startsWith("NaN")) {
return ClickHouseCreateConstant.createInt32Constant(0);
}
for (int i = asString.length(); i >= 0; i--) {
try {
String substring = asString.substring(0, i);
double d = Double.parseDouble(substring);
BigDecimal first = new BigDecimal(substring);
long longValue = first.longValue();
BigDecimal second = BigDecimal.valueOf(longValue);
boolean isWithinConvertibleRange = longValue >= MIN_INT_FOR_WHICH_CONVERSION_TO_INT_IS_TRIED
&& longValue <= MAX_INT_FOR_WHICH_CONVERSION_TO_INT_IS_TRIED && convertRealToInt;
boolean isFloatingPointNumber = substring.contains(".") || substring.toUpperCase().contains("E");
boolean doubleShouldBeConvertedToInt = isFloatingPointNumber && first.compareTo(second) == 0
&& isWithinConvertibleRange;
boolean isInteger = !isFloatingPointNumber && first.compareTo(second) == 0;
if (doubleShouldBeConvertedToInt || isInteger && !convertIntToReal) {
// see https://www.sqlite.org/src/tktview/afdc5a29dc
return ClickHouseCreateConstant.createInt32Constant(first.longValue());
} else {
return ClickHouseCreateConstant.createFloat64Constant(d);
}
} catch (Exception e) {
}
}
if (noNumIsRealZero) {
return ClickHouseCreateConstant.createFloat64Constant(0.0);
} else {
return ClickHouseCreateConstant.createInt32Constant(0);
}
default:
throw new AssertionError(value);
}
}
private static boolean startsWithWhitespace(String asString) {
if (asString.isEmpty()) {
return false;
}
char c = asString.charAt(0);
switch (c) {
case ' ':
case '\t':
case 0x0b:
case '\f':
case '\n':
case '\r':
return true;
default:
return false;
}
}
private static boolean unprintAbleCharThatLetsBecomeNumberZero(String s) {
// non-printable characters are ignored by Double.valueOf
for (int i = 0; i < s.length(); i++) {
char charAt = s.charAt(i);
if (!Character.isISOControl(charAt) && !Character.isWhitespace(charAt)) {
return false;
}
switch (charAt) {
case GROUP_SEPARATOR:
case FILE_SEPARATOR:
case RECORD_SEPARATOR:
case UNIT_SEPARATOR:
case SYNCHRONOUS_IDLE:
return true;
default:
// fall through
}
if (Character.isWhitespace(charAt)) {
continue;
} else {
return true;
}
}
return false;
}
public static ClickHouseConstant castToText(ClickHouseConstant cons) {
if (cons.getDataType() == ClickHouseDataType.String) {
return cons;
}
if (cons.getDataType() == ClickHouseDataType.Nothing) {
return cons;
}
if (cons.getDataType() == ClickHouseDataType.Float64) {
if (cons.asDouble() == Double.POSITIVE_INFINITY) {
return ClickHouseCreateConstant.createStringConstant("Inf");
} else if (cons.asDouble() == Double.NEGATIVE_INFINITY) {
return ClickHouseCreateConstant.createStringConstant("-Inf");
}
return castRealToText(cons);
}
if (cons.getDataType() == ClickHouseDataType.Int32) {
return ClickHouseCreateConstant.createStringConstant(String.valueOf(cons.asInt()));
}
return null;
}
private static synchronized ClickHouseConstant castRealToText(ClickHouseConstant cons) throws AssertionError {
try (Statement s = castDatabase.createStatement()) {
String castResult = s.executeQuery("SELECT CAST(" + cons.asDouble() + " AS TEXT)").getString(1);
return ClickHouseCreateConstant.createStringConstant(castResult);
} catch (Exception e) {
throw new AssertionError(e);
}
}
public static ClickHouseConstant asBoolean(ClickHouseConstant val) {
Optional<Boolean> boolVal = isTrue(val);
if (boolVal.isPresent()) {
return ClickHouseCreateConstant.createBoolean(boolVal.get());
} else {
return ClickHouseCreateConstant.createNullConstant();
}
}
}