-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathTablePartitioning.java
More file actions
322 lines (271 loc) · 9.75 KB
/
Copy pathTablePartitioning.java
File metadata and controls
322 lines (271 loc) · 9.75 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
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.create.table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.PlainSelect;
/**
* MySQL table partitioning options used by {@code CREATE TABLE} and {@code ALTER TABLE}.
*/
public class TablePartitioning implements Serializable {
public enum Type {
HASH, KEY, RANGE, LIST,
/**
* BigQuery partitions by a bare expression, without a partitioning method keyword:
* {@code PARTITION BY DATE(ts)}.
*/
EXPRESSION
}
private Type type;
private boolean linear;
private boolean columnsSyntax;
private Expression expression;
private ExpressionList<Expression> expressionList;
private ExpressionList<Column> columns;
private Integer algorithm;
private boolean algorithmUseEquals;
private Long partitions;
private TablePartitioning subPartitioning;
private List<PartitionDefinition> partitionDefinitions;
private List<String> partitionOptions;
public TablePartitioning() {}
public TablePartitioning(Type type) {
this.type = type;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public boolean isLinear() {
return linear;
}
public void setLinear(boolean linear) {
this.linear = linear;
}
public boolean isColumnsSyntax() {
return columnsSyntax;
}
public void setColumnsSyntax(boolean columnsSyntax) {
this.columnsSyntax = columnsSyntax;
}
public Expression getExpression() {
return expression;
}
public void setExpression(Expression expression) {
this.expression = expression;
}
/**
* Returns the ordered partition key expressions when more than one key was declared.
*
* @return partition key expressions, or {@code null} for the legacy single-expression form
*/
public ExpressionList<Expression> getExpressionList() {
return expressionList;
}
public void setExpressionList(ExpressionList<Expression> expressionList) {
this.expressionList = expressionList;
}
public ExpressionList<Column> getColumns() {
return columns;
}
public void setColumns(ExpressionList<Column> columns) {
this.columns = columns;
}
public Integer getAlgorithm() {
return algorithm;
}
public void setAlgorithm(Integer algorithm) {
this.algorithm = algorithm;
}
public boolean isAlgorithmUseEquals() {
return algorithmUseEquals;
}
public void setAlgorithmUseEquals(boolean algorithmUseEquals) {
this.algorithmUseEquals = algorithmUseEquals;
}
public Long getPartitions() {
return partitions;
}
public void setPartitions(Long partitions) {
this.partitions = partitions;
}
public TablePartitioning getSubPartitioning() {
return subPartitioning;
}
public void setSubPartitioning(TablePartitioning subPartitioning) {
this.subPartitioning = subPartitioning;
}
public List<PartitionDefinition> getPartitionDefinitions() {
return partitionDefinitions;
}
public void setPartitionDefinitions(List<PartitionDefinition> partitionDefinitions) {
this.partitionDefinitions = partitionDefinitions;
}
public List<String> getPartitionOptions() {
return partitionOptions;
}
public void setPartitionOptions(List<String> partitionOptions) {
this.partitionOptions = partitionOptions;
}
public TablePartitioning withType(Type type) {
setType(type);
return this;
}
public TablePartitioning withLinear(boolean linear) {
setLinear(linear);
return this;
}
public TablePartitioning withColumnsSyntax(boolean columnsSyntax) {
setColumnsSyntax(columnsSyntax);
return this;
}
public TablePartitioning withExpression(Expression expression) {
setExpression(expression);
return this;
}
public TablePartitioning withExpressionList(ExpressionList<Expression> expressionList) {
setExpressionList(expressionList);
return this;
}
public TablePartitioning withColumns(ExpressionList<Column> columns) {
setColumns(columns);
return this;
}
public TablePartitioning withAlgorithm(Integer algorithm) {
setAlgorithm(algorithm);
return this;
}
public TablePartitioning withAlgorithmUseEquals(boolean algorithmUseEquals) {
setAlgorithmUseEquals(algorithmUseEquals);
return this;
}
public TablePartitioning withPartitions(Long partitions) {
setPartitions(partitions);
return this;
}
public TablePartitioning withSubPartitioning(TablePartitioning subPartitioning) {
setSubPartitioning(subPartitioning);
return this;
}
public TablePartitioning withPartitionDefinitions(
List<PartitionDefinition> partitionDefinitions) {
setPartitionDefinitions(partitionDefinitions);
return this;
}
public TablePartitioning withPartitionOptions(List<String> partitionOptions) {
setPartitionOptions(partitionOptions);
return this;
}
public TablePartitioning addColumns(Column... columns) {
ExpressionList<Column> collection =
Optional.ofNullable(getColumns()).orElseGet(ExpressionList::new);
Collections.addAll(collection, columns);
return withColumns(collection);
}
public TablePartitioning addColumns(Collection<? extends Column> columns) {
ExpressionList<Column> collection =
Optional.ofNullable(getColumns()).orElseGet(ExpressionList::new);
collection.addAll(columns);
return withColumns(collection);
}
public TablePartitioning addPartitionDefinitions(
PartitionDefinition... partitionDefinitions) {
List<PartitionDefinition> collection = Optional.ofNullable(getPartitionDefinitions())
.orElseGet(ArrayList::new);
Collections.addAll(collection, partitionDefinitions);
return withPartitionDefinitions(collection);
}
public TablePartitioning addPartitionDefinitions(
Collection<? extends PartitionDefinition> partitionDefinitions) {
List<PartitionDefinition> collection = Optional.ofNullable(getPartitionDefinitions())
.orElseGet(ArrayList::new);
collection.addAll(partitionDefinitions);
return withPartitionDefinitions(collection);
}
public TablePartitioning addPartitionOptions(String... partitionOptions) {
List<String> collection =
Optional.ofNullable(getPartitionOptions()).orElseGet(ArrayList::new);
Collections.addAll(collection, partitionOptions);
return withPartitionOptions(collection);
}
public TablePartitioning addPartitionOptions(Collection<String> partitionOptions) {
List<String> collection =
Optional.ofNullable(getPartitionOptions()).orElseGet(ArrayList::new);
collection.addAll(partitionOptions);
return withPartitionOptions(collection);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
appendTo(builder, builder::append);
return builder.toString();
}
public void appendTo(StringBuilder builder, Consumer<Expression> expressionPrinter) {
builder.append("PARTITION BY ");
appendMethod(builder, expressionPrinter);
if (partitions != null) {
builder.append(" PARTITIONS ").append(partitions);
}
if (subPartitioning != null) {
builder.append(" SUBPARTITION BY ");
subPartitioning.appendMethod(builder, expressionPrinter);
if (subPartitioning.getPartitions() != null) {
builder.append(" SUBPARTITIONS ").append(subPartitioning.getPartitions());
}
}
if (partitionDefinitions != null && !partitionDefinitions.isEmpty()) {
builder.append(" ")
.append(PlainSelect.getStringList(partitionDefinitions, true, true));
}
if (partitionOptions != null && !partitionOptions.isEmpty()) {
builder.append(" ").append(PlainSelect.getStringList(partitionOptions, false, false));
}
}
private void appendMethod(StringBuilder builder, Consumer<Expression> expressionPrinter) {
if (type == Type.EXPRESSION) {
expressionPrinter.accept(expression);
return;
}
if (linear) {
builder.append("LINEAR ");
}
builder.append(type);
if (algorithm != null) {
builder.append(" ALGORITHM");
builder.append(algorithmUseEquals ? " = " : " ").append(algorithm);
}
if (columnsSyntax) {
builder.append(" COLUMNS");
}
if (expression != null) {
builder.append(" (");
expressionPrinter.accept(expression);
builder.append(')');
} else if (expressionList != null) {
builder.append(" (");
expressionPrinter.accept(expressionList);
builder.append(')');
} else if (columns != null) {
builder.append(" (");
expressionPrinter.accept(columns);
builder.append(')');
}
}
}