forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.java
More file actions
511 lines (420 loc) · 14 KB
/
Function.java
File metadata and controls
511 lines (420 loc) · 14 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*-
* #%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 net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.NamedExpressionList;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.OrderByElement;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* A function as MAX,COUNT...
*/
public class Function extends ASTNodeAccessImpl implements Expression {
private List<String> nameparts;
private ExpressionList<?> parameters;
private ExpressionList<?> chainedParameters;
private NamedExpressionList<?> namedParameters;
private boolean allColumns = false;
private boolean distinct = false;
private boolean unique = false;
private boolean isEscaped = false;
private Expression attributeExpression;
private HavingClause havingClause;
private Column attributeColumn = null;
private List<OrderByElement> orderByElements;
private NullHandling nullHandling = null;
private boolean ignoreNullsOutside = false; // IGNORE NULLS outside function parameters
private Limit limit = null;
private KeepExpression keep = null;
private String onOverflowTruncate = null;
private String extraKeyword = null;
public Function() {}
public Function(String name, Expression... parameters) {
this.nameparts = Collections.singletonList(name);
this.parameters = new ExpressionList<>(parameters);
}
@Override
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
return expressionVisitor.visit(this, context);
}
public String getName() {
return nameparts == null ? null
: String.join(nameparts.get(0).equalsIgnoreCase("APPROXIMATE") ? " " : ".",
nameparts);
}
public void setName(String string) {
nameparts = Arrays.asList(string);
}
public void setName(List<String> string) {
nameparts = string;
}
public List<String> getMultipartName() {
return nameparts;
}
public Function withName(String name) {
this.setName(name);
return this;
}
public Function withName(List<String> nameparts) {
this.nameparts = nameparts;
return this;
}
public boolean isAllColumns() {
return allColumns;
}
public void setAllColumns(boolean b) {
allColumns = b;
}
public NullHandling getNullHandling() {
return nullHandling;
}
public Function setNullHandling(NullHandling nullHandling) {
this.nullHandling = nullHandling;
return this;
}
public boolean isIgnoreNullsOutside() {
return ignoreNullsOutside;
}
public Function setIgnoreNullsOutside(boolean ignoreNullsOutside) {
this.ignoreNullsOutside = ignoreNullsOutside;
return this;
}
public Limit getLimit() {
return limit;
}
public Function setLimit(Limit limit) {
this.limit = limit;
return this;
}
public boolean isIgnoreNulls() {
return nullHandling != null && nullHandling == NullHandling.IGNORE_NULLS;
}
/**
* This is at the moment only necessary for AnalyticExpression initialization and not for normal
* functions. Therefore there is no deparsing for it for normal functions.
*/
public void setIgnoreNulls(boolean ignoreNulls) {
this.nullHandling = ignoreNulls ? NullHandling.IGNORE_NULLS : null;
}
public HavingClause getHavingClause() {
return havingClause;
}
public Function setHavingClause(HavingClause havingClause) {
this.havingClause = havingClause;
return this;
}
public Function setHavingClause(String havingType, Expression expression) {
this.havingClause = new HavingClause(
HavingClause.HavingType.valueOf(havingType.trim().toUpperCase()), expression);
return this;
}
/**
* true if the function is "distinct"
*
* @return true if the function is "distinct"
*/
public boolean isDistinct() {
return distinct;
}
public void setDistinct(boolean b) {
distinct = b;
}
/**
* true if the function is "unique"
*
* @return true if the function is "unique"
*/
public boolean isUnique() {
return unique;
}
public void setUnique(boolean b) {
unique = b;
}
/**
* The list of parameters of the function (if any, else null) If the parameter is "*",
* allColumns is set to true
*
* @return the list of parameters of the function (if any, else null)
*/
public ExpressionList<?> getParameters() {
return parameters;
}
public void setParameters(Expression... expressions) {
if (expressions.length == 1 && expressions[0] instanceof ExpressionList) {
parameters = (ExpressionList<?>) expressions[0];
} else {
parameters = new ExpressionList<>(expressions);
}
}
public void setParameters(ExpressionList<?> list) {
parameters = list;
}
/**
* Additional function-call parameters for dialects that support chained function calls, e.g.
* quantile(0.95)(cost) in ClickHouse.
*
* @return the chained parameters of the function (if any, else null)
*/
public ExpressionList<?> getChainedParameters() {
return chainedParameters;
}
public void setChainedParameters(ExpressionList<?> chainedParameters) {
this.chainedParameters = chainedParameters;
}
/**
* the parameters might be named parameters, e.g. substring('foobar' from 2 for 3)
*
* @return the list of named parameters of the function (if any, else null)
*/
public NamedExpressionList<?> getNamedParameters() {
return namedParameters;
}
public void setNamedParameters(NamedExpressionList<?> list) {
namedParameters = list;
}
/**
* Return true if it's in the form "{fn function_body() }"
*
* @return true if it's java-escaped
*/
public boolean isEscaped() {
return isEscaped;
}
public void setEscaped(boolean isEscaped) {
this.isEscaped = isEscaped;
}
public Object getAttribute() {
return attributeExpression != null ? attributeExpression : attributeColumn;
}
public void setAttribute(Expression attributeExpression) {
this.attributeExpression = attributeExpression;
}
public void setAttribute(Column attributeColumn) {
attributeExpression = null;
this.attributeColumn = attributeColumn;
}
@Deprecated
public String getAttributeName() {
return attributeColumn.toString();
}
public void setAttributeName(String attributeName) {
this.attributeColumn = new Column().withColumnName(attributeName);
}
public Column getAttributeColumn() {
return attributeColumn;
}
public Function withAttribute(Column attributeColumn) {
setAttribute(attributeColumn);
return this;
}
public KeepExpression getKeep() {
return keep;
}
public void setKeep(KeepExpression keep) {
this.keep = keep;
}
public String getExtraKeyword() {
return extraKeyword;
}
public Function setExtraKeyword(String extraKeyword) {
this.extraKeyword = extraKeyword;
return this;
}
@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public String toString() {
String params;
if (parameters != null || namedParameters != null) {
if (parameters != null) {
StringBuilder b = new StringBuilder();
b.append("(");
if (isDistinct()) {
b.append("DISTINCT ");
} else if (isUnique()) {
b.append("UNIQUE ");
}
if (isAllColumns()) {
b.append("ALL ");
}
if (extraKeyword != null) {
b.append(extraKeyword).append(" ");
}
b.append(parameters);
if (havingClause != null) {
havingClause.appendTo(b);
}
if (nullHandling != null && !isIgnoreNullsOutside()) {
switch (nullHandling) {
case IGNORE_NULLS:
b.append(" IGNORE NULLS");
break;
case RESPECT_NULLS:
b.append(" RESPECT NULLS");
break;
}
}
if (orderByElements != null) {
b.append(" ORDER BY ");
boolean comma = false;
for (OrderByElement orderByElement : orderByElements) {
if (comma) {
b.append(", ");
} else {
comma = true;
}
b.append(orderByElement);
}
}
if (limit != null) {
b.append(limit);
}
if (onOverflowTruncate != null) {
b.append(" ON OVERFLOW ").append(onOverflowTruncate);
}
b.append(")");
params = b.toString();
} else {
params = namedParameters.toString();
}
} else {
params = "()";
}
String ans = getName() + params;
if (chainedParameters != null) {
ans += "(" + chainedParameters + ")";
}
if (nullHandling != null && isIgnoreNullsOutside()) {
switch (nullHandling) {
case IGNORE_NULLS:
ans += " IGNORE NULLS";
break;
case RESPECT_NULLS:
ans += " RESPECT NULLS";
break;
}
}
if (attributeExpression != null) {
ans += "." + attributeExpression;
} else if (attributeColumn != null) {
ans += "." + attributeColumn;
}
if (keep != null) {
ans += " " + keep;
}
if (isEscaped) {
ans = "{fn " + ans + "}";
}
return ans;
}
public Function withAttribute(Expression attribute) {
this.setAttribute(attribute);
return this;
}
@Deprecated
public Function withAttributeName(String attributeName) {
this.setAttributeName(attributeName);
return this;
}
public Function withKeep(KeepExpression keep) {
this.setKeep(keep);
return this;
}
public Function withIgnoreNulls(boolean ignoreNulls) {
this.setIgnoreNulls(ignoreNulls);
return this;
}
public Function withParameters(ExpressionList<?> parameters) {
this.setParameters(parameters);
return this;
}
public Function withParameters(Expression... parameters) {
return withParameters(new ExpressionList<>(parameters));
}
public Function withChainedParameters(ExpressionList<?> chainedParameters) {
this.setChainedParameters(chainedParameters);
return this;
}
public Function withNamedParameters(NamedExpressionList<?> namedParameters) {
this.setNamedParameters(namedParameters);
return this;
}
public Function withAllColumns(boolean allColumns) {
this.setAllColumns(allColumns);
return this;
}
public Function withDistinct(boolean distinct) {
this.setDistinct(distinct);
return this;
}
public Function withUnique(boolean unique) {
this.setUnique(unique);
return this;
}
public List<OrderByElement> getOrderByElements() {
return orderByElements;
}
public void setOrderByElements(List<OrderByElement> orderByElements) {
this.orderByElements = orderByElements;
}
public String getOnOverflowTruncate() {
return onOverflowTruncate;
}
public Function setOnOverflowTruncate(String onOverflowTruncate) {
this.onOverflowTruncate = onOverflowTruncate;
return this;
}
public <E extends Expression> E getAttribute(Class<E> type) {
return type.cast(getAttribute());
}
public enum NullHandling {
IGNORE_NULLS, RESPECT_NULLS;
}
public static class HavingClause extends ASTNodeAccessImpl implements Expression {
HavingType havingType;
Expression expression;
public HavingClause(HavingType havingType, Expression expression) {
this.havingType = havingType;
this.expression = expression;
}
public HavingType getHavingType() {
return havingType;
}
public HavingClause setHavingType(HavingType havingType) {
this.havingType = havingType;
return this;
}
public Expression getExpression() {
return expression;
}
public HavingClause setExpression(Expression expression) {
this.expression = expression;
return this;
}
@Override
public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
return expression.accept(expressionVisitor, context);
}
public StringBuilder appendTo(StringBuilder builder) {
builder.append(" HAVING ").append(havingType.name()).append(" ").append(expression);
return builder;
}
@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
enum HavingType {
MAX, MIN;
}
}
}