-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCastExpression.java
More file actions
297 lines (243 loc) · 9.88 KB
/
Copy pathCastExpression.java
File metadata and controls
297 lines (243 loc) · 9.88 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
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;
import java.util.Locale;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.statement.create.table.ColDataType;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.select.Select;
import java.util.ArrayList;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CastExpression extends ASTNodeAccessImpl implements Expression {
private final static Pattern PATTERN =
Pattern.compile("(^[a-z0-9_]*){1}", Pattern.CASE_INSENSITIVE);
public String keyword;
private Expression leftExpression;
private ColDataType colDataType = null;
private ArrayList<ColumnDefinition> columnDefinitions = new ArrayList<>();
private boolean isImplicitCast = false;
// BigQuery specific FORMAT clause:
// https://cloud.google.com/bigquery/docs/reference/standard-sql/conversion_functions#cast_as_date
private String format = null;
public CastExpression(String keyword, Expression leftExpression, String dataType) {
this.keyword = keyword;
this.leftExpression = leftExpression;
this.colDataType = new ColDataType(dataType);
}
// Implicit Cast
public CastExpression(String dataType, String value) {
this.keyword = null;
this.isImplicitCast = true;
this.colDataType = new ColDataType(dataType);
this.leftExpression = new StringValue(value);
}
public CastExpression(ColDataType colDataType, String value) {
this.keyword = null;
this.isImplicitCast = true;
this.colDataType = colDataType;
this.leftExpression = new StringValue(value);
}
public CastExpression(ColDataType colDataType, Long value) {
this.keyword = null;
this.isImplicitCast = true;
this.colDataType = colDataType;
this.leftExpression = new LongValue(value);
}
public CastExpression(ColDataType colDataType, Double value) {
this.keyword = null;
this.isImplicitCast = true;
this.colDataType = colDataType;
this.leftExpression = new DoubleValue(value);
}
public CastExpression(Expression leftExpression, String dataType) {
this.keyword = null;
this.leftExpression = leftExpression;
this.colDataType = new ColDataType(dataType);
}
public CastExpression(String keyword) {
this.keyword = keyword;
}
public CastExpression() {
this("CAST");
}
public static boolean isOf(ColDataType colDataType, DataType... types) {
return Set.of(types).contains(DataType.from(colDataType.getDataType()));
}
public static boolean isTime(ColDataType colDataType) {
return isOf(colDataType, DataType.TIME, DataType.TIME_WITH_TIME_ZONE,
DataType.TIME_WITHOUT_TIME_ZONE);
}
public static boolean isTimeStamp(ColDataType colDataType) {
return isOf(colDataType, DataType.TIMESTAMP_NS, DataType.TIMESTAMP,
DataType.TIMESTAMP_WITHOUT_TIME_ZONE,
DataType.DATETIME, DataType.TIMESTAMP_MS, DataType.TIMESTAMP_S,
DataType.TIMESTAMPTZ, DataType.TIMESTAMP_WITH_TIME_ZONE);
}
public static boolean isDate(ColDataType colDataType) {
return isOf(colDataType, DataType.DATE);
}
public static boolean isBLOB(ColDataType colDataType) {
return isOf(colDataType, DataType.BLOB, DataType.BYTEA, DataType.BINARY, DataType.VARBINARY,
DataType.BYTES, DataType.VARBYTE);
}
public static boolean isFloat(ColDataType colDataType) {
return isOf(colDataType, DataType.REAL, DataType.FLOAT4, DataType.FLOAT, DataType.DOUBLE,
DataType.DOUBLE_PRECISION, DataType.FLOAT8);
}
public static boolean isInteger(ColDataType colDataType) {
return isOf(colDataType, DataType.TINYINT, DataType.INT1, DataType.SMALLINT, DataType.INT2,
DataType.SHORT, DataType.INTEGER, DataType.INT4, DataType.INT, DataType.SIGNED,
DataType.BIGINT, DataType.INT8, DataType.LONG, DataType.HUGEINT, DataType.UTINYINT,
DataType.USMALLINT, DataType.UINTEGER, DataType.UBIGINT, DataType.UHUGEINT);
}
public static boolean isDecimal(ColDataType colDataType) {
return isOf(colDataType, DataType.DECIMAL, DataType.NUMBER, DataType.NUMERIC);
}
public static boolean isText(ColDataType colDataType) {
return isOf(colDataType, DataType.VARCHAR, DataType.NVARCHAR, DataType.CHAR, DataType.NCHAR,
DataType.BPCHAR, DataType.STRING, DataType.TEXT, DataType.CLOB);
}
public ColDataType getColDataType() {
return colDataType;
}
public void setColDataType(ColDataType colDataType) {
this.colDataType = colDataType;
}
public ArrayList<ColumnDefinition> getColumnDefinitions() {
return columnDefinitions;
}
public void addColumnDefinition(ColumnDefinition columnDefinition) {
this.columnDefinitions.add(columnDefinition);
}
public Expression getLeftExpression() {
return leftExpression;
}
public void setLeftExpression(Expression expression) {
leftExpression = expression;
}
public boolean isImplicitCast() {
return isImplicitCast;
}
public CastExpression setImplicitCast(boolean implicitCast) {
isImplicitCast = implicitCast;
return this;
}
@Override
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
return expressionVisitor.visit(this, context);
}
@Deprecated
public boolean isUseCastKeyword() {
return keyword != null && !keyword.isEmpty();
}
@Deprecated
public void setUseCastKeyword(boolean useCastKeyword) {
if (useCastKeyword) {
if (keyword == null || keyword.isEmpty()) {
keyword = "CAST";
}
} else {
keyword = null;
}
}
public String getFormat() {
return format;
}
public CastExpression setFormat(String format) {
this.format = format;
return this;
}
@Override
public String toString() {
String formatStr = format != null && !format.isEmpty()
? " FORMAT " + format
: "";
if (isImplicitCast) {
return colDataType + " " + leftExpression;
} else if (keyword != null && !keyword.isEmpty()) {
return columnDefinitions.size() > 1
? keyword + "(" + leftExpression + " AS ROW("
+ Select.getStringList(columnDefinitions) + ")" + formatStr + ")"
: keyword + "(" + leftExpression + " AS " + colDataType.toString() + formatStr
+ ")";
} else {
return leftExpression + "::" + colDataType.toString();
}
}
public CastExpression withType(ColDataType type) {
this.setColDataType(type);
return this;
}
public CastExpression withUseCastKeyword(boolean useCastKeyword) {
this.setUseCastKeyword(useCastKeyword);
return this;
}
public CastExpression withLeftExpression(Expression leftExpression) {
this.setLeftExpression(leftExpression);
return this;
}
public <E extends Expression> E getLeftExpression(Class<E> type) {
return type.cast(getLeftExpression());
}
public boolean isOf(CastExpression anotherCast) {
return this.colDataType.equals(anotherCast.colDataType);
}
public boolean isOf(DataType... types) {
return Set.of(types).contains(DataType.from(colDataType.getDataType()));
}
public boolean isTime() {
return isTime(this.colDataType);
}
public boolean isTimeStamp() {
return isTimeStamp(this.colDataType);
}
public boolean isDate() {
return isDate(this.colDataType);
}
public boolean isBLOB() {
return isBLOB(this.colDataType);
}
public boolean isFloat() {
return isFloat(this.colDataType);
}
public boolean isInteger() {
return isInteger(this.colDataType);
}
public boolean isDecimal() {
return isDecimal(this.colDataType);
}
public boolean isText() {
return isText(this.colDataType);
}
public enum DataType {
ARRAY, BIT, BITSTRING, BLOB, BYTEA, BINARY, VARBINARY, BYTES, BOOLEAN, BOOL, ENUM, INTERVAL, LIST, MAP, STRUCT, TINYINT, INT1, SMALLINT, INT2, SHORT, INTEGER, INT4, INT, SIGNED, BIGINT, INT8, LONG, HUGEINT, UTINYINT, USMALLINT, UINTEGER, UBIGINT, UHUGEINT, DECIMAL, NUMBER, NUMERIC, REAL, FLOAT4, FLOAT, DOUBLE, DOUBLE_PRECISION, FLOAT8, FLOAT64, UUID, VARCHAR, NVARCHAR, CHAR, NCHAR, BPCHAR, STRING, TEXT, CLOB, DATE, TIME, TIME_WITHOUT_TIME_ZONE, TIMETZ, TIME_WITH_TIME_ZONE, TIMESTAMP_NS, TIMESTAMP, TIMESTAMP_WITHOUT_TIME_ZONE, DATETIME, TIMESTAMP_MS, TIMESTAMP_S, TIMESTAMPTZ, TIMESTAMP_WITH_TIME_ZONE, UNKNOWN, VARBYTE, JSON;
public static DataType from(String typeStr) {
Matcher matcher = PATTERN.matcher(
typeStr.trim().replaceAll("\\s+", "_").toUpperCase(Locale.ROOT));
if (matcher.find()) {
try {
return Enum.valueOf(DataType.class, matcher.group(0));
} catch (Exception ex) {
Logger.getLogger(CastExpression.class.getName()).log(Level.FINE,
"Type " + typeStr + " unknown", ex);
return DataType.UNKNOWN;
}
} else {
Logger.getLogger(CastExpression.class.getName()).log(Level.FINE,
"Type " + typeStr + " unknown");
return DataType.UNKNOWN;
}
}
}
}