Skip to content

Commit cc829a1

Browse files
committed
1 parent eb781b6 commit cc829a1

File tree

5 files changed

+276
-1
lines changed

5 files changed

+276
-1
lines changed

src/main/java/com/alibaba/druid/sql/ast/SQLPartitionByRange.java

+83
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@
1515
*/
1616
package com.alibaba.druid.sql.ast;
1717

18+
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
1819
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
1920

2021
public class SQLPartitionByRange extends SQLPartitionBy {
2122
protected SQLExpr interval;
23+
protected boolean isColumns = false;
24+
protected SQLExpr startWith;
25+
protected SQLIntegerExpr expireAfter;
26+
protected SQLIntegerExpr preAllocate;
27+
protected SQLExpr pivotDateExpr;
28+
protected boolean disableSchedule = false;
2229

2330
public SQLPartitionByRange() {
2431
}
@@ -35,11 +42,60 @@ public void setInterval(SQLExpr interval) {
3542
this.interval = interval;
3643
}
3744

45+
public SQLExpr getStartWith() {
46+
return this.startWith;
47+
}
48+
49+
public void setStartWith(final SQLExpr startWith) {
50+
if (startWith != null) {
51+
startWith.setParent(this);
52+
}
53+
54+
this.startWith = startWith;
55+
}
56+
57+
public SQLIntegerExpr getExpireAfter() {
58+
return this.expireAfter;
59+
}
60+
61+
public void setExpireAfter(final SQLIntegerExpr expireAfter) {
62+
if (expireAfter != null) {
63+
expireAfter.setParent(this);
64+
}
65+
this.expireAfter = expireAfter;
66+
}
67+
68+
public SQLIntegerExpr getPreAllocate() {
69+
return this.preAllocate;
70+
}
71+
72+
public void setPreAllocate(final SQLIntegerExpr preAllocate) {
73+
if (preAllocate != null) {
74+
preAllocate.setParent(this);
75+
}
76+
this.preAllocate = preAllocate;
77+
}
78+
79+
public SQLExpr getPivotDateExpr() {
80+
return this.pivotDateExpr;
81+
}
82+
83+
public void setPivotDateExpr(final SQLExpr pivotDateExpr) {
84+
if (pivotDateExpr != null) {
85+
pivotDateExpr.setParent(this);
86+
}
87+
this.pivotDateExpr = pivotDateExpr;
88+
}
89+
3890
@Override
3991
protected void accept0(SQLASTVisitor visitor) {
4092
if (visitor.visit(this)) {
4193
acceptChild(visitor, columns);
4294
acceptChild(visitor, interval);
95+
acceptChild(visitor, expireAfter);
96+
acceptChild(visitor, preAllocate);
97+
acceptChild(visitor, pivotDateExpr);
98+
acceptChild(visitor, columns);
4399
acceptChild(visitor, storeIn);
44100
acceptChild(visitor, partitions);
45101
acceptChild(visitor, subPartitionBy);
@@ -56,16 +112,43 @@ public SQLPartitionByRange clone() {
56112
x.setInterval(interval.clone());
57113
}
58114

115+
if (startWith != null) {
116+
x.setStartWith(startWith.clone());
117+
}
118+
119+
if (expireAfter != null) {
120+
x.setExpireAfter(expireAfter.clone());
121+
}
122+
59123
for (SQLExpr column : columns) {
60124
SQLExpr c2 = column.clone();
61125
c2.setParent(x);
62126
x.columns.add(c2);
63127
}
64128

129+
x.setColumns(this.isColumns);
130+
x.setDisableSchedule(this.disableSchedule);
131+
65132
return x;
66133
}
67134

68135
public void cloneTo(SQLPartitionByRange x) {
69136
super.cloneTo(x);
70137
}
138+
139+
public boolean isColumns() {
140+
return isColumns;
141+
}
142+
143+
public void setColumns(boolean columns) {
144+
isColumns = columns;
145+
}
146+
147+
public boolean isDisableSchedule() {
148+
return this.disableSchedule;
149+
}
150+
151+
public void setDisableSchedule(final boolean disableSchedule) {
152+
this.disableSchedule = disableSchedule;
153+
}
71154
}

src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateTableStatement.java

+18
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
5050

5151
protected SQLName tablespace;
5252
protected SQLPartitionBy partitioning;
53+
protected SQLPartitionBy localPartitioning;
5354
protected SQLExpr storedAs;
5455
protected SQLExpr location;
5556

@@ -99,6 +100,7 @@ protected void acceptChild(SQLASTVisitor v) {
99100

100101
this.acceptChild(v, tablespace);
101102
this.acceptChild(v, partitioning);
103+
this.acceptChild(v, localPartitioning);
102104
this.acceptChild(v, storedAs);
103105
this.acceptChild(v, location);
104106

@@ -348,6 +350,10 @@ public SQLPartitionBy getPartitioning() {
348350
return partitioning;
349351
}
350352

353+
public SQLPartitionBy getLocalPartitioning() {
354+
return this.localPartitioning;
355+
}
356+
351357
public void setPartitioning(SQLPartitionBy partitioning) {
352358
if (partitioning != null) {
353359
partitioning.setParent(this);
@@ -356,6 +362,14 @@ public void setPartitioning(SQLPartitionBy partitioning) {
356362
this.partitioning = partitioning;
357363
}
358364

365+
public void setLocalPartitioning(SQLPartitionBy localPartitioning) {
366+
if (localPartitioning != null) {
367+
localPartitioning.setParent(this);
368+
}
369+
370+
this.localPartitioning = localPartitioning;
371+
}
372+
359373
@Override
360374
public List<SQLObject> getChildren() {
361375
List<SQLObject> children = new ArrayList<SQLObject>();
@@ -1224,6 +1238,10 @@ public void cloneTo(SQLCreateTableStatement x) {
12241238
x.setPartitioning(partitioning.clone());
12251239
}
12261240

1241+
if (localPartitioning != null) {
1242+
x.setLocalPartitioning(localPartitioning.clone());
1243+
}
1244+
12271245
if (storedAs != null) {
12281246
x.setStoredAs(storedAs.clone());
12291247
}

src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java

+3
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ public void cloneTo(MySqlCreateTableStatement x) {
486486
if (partitioning != null) {
487487
x.setPartitioning(partitioning.clone());
488488
}
489+
if (localPartitioning != null) {
490+
x.setLocalPartitioning(localPartitioning.clone());
491+
}
489492
for (SQLCommentHint hint : hints) {
490493
SQLCommentHint h2 = hint.clone();
491494
h2.setParent(x);

src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlCreateTableParser.java

+140
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,88 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
119119
}
120120
}
121121

122+
boolean local = false;
123+
if (lexer.identifierEquals(FnvHash.Constants.LOCAL)) {
124+
final Lexer.SavePoint mark = lexer.mark();
125+
lexer.nextToken();
126+
if (lexer.token() == Token.INDEX || lexer.token() == Token.KEY
127+
|| lexer.token() == Token.UNIQUE) {
128+
local = true;
129+
} else if (lexer.token() == Token.FULLTEXT) {
130+
lexer.nextToken();
131+
132+
if (lexer.token() == Token.KEY) {
133+
MySqlKey fulltextKey = new MySqlKey();
134+
this.exprParser.parseIndex(fulltextKey.getIndexDefinition());
135+
fulltextKey.setIndexType("FULLTEXT");
136+
fulltextKey.setParent(stmt);
137+
stmt.getTableElementList().add(fulltextKey);
138+
139+
while (lexer.token() == Token.HINT) {
140+
lexer.nextToken();
141+
}
142+
143+
if (lexer.token() == Token.RPAREN) {
144+
break;
145+
} else if (lexer.token() == Token.COMMA) {
146+
lexer.nextToken();
147+
continue;
148+
}
149+
} else if (lexer.token() == Token.INDEX) {
150+
MySqlTableIndex idx = new MySqlTableIndex();
151+
this.exprParser.parseIndex(idx.getIndexDefinition());
152+
idx.setIndexType("FULLTEXT");
153+
idx.setParent(stmt);
154+
stmt.getTableElementList().add(idx);
155+
156+
if (lexer.token() == Token.RPAREN) {
157+
break;
158+
} else if (lexer.token() == Token.COMMA) {
159+
lexer.nextToken();
160+
continue;
161+
}
162+
} else if (lexer.token() == Token.IDENTIFIER && MySqlUtils.isBuiltinDataType(
163+
lexer.stringVal())) {
164+
lexer.reset(mark);
165+
} else {
166+
MySqlTableIndex idx = new MySqlTableIndex();
167+
this.exprParser.parseIndex(idx.getIndexDefinition());
168+
idx.setIndexType("FULLTEXT");
169+
idx.setParent(stmt);
170+
stmt.getTableElementList().add(idx);
171+
172+
if (lexer.token() == Token.RPAREN) {
173+
break;
174+
} else if (lexer.token() == Token.COMMA) {
175+
lexer.nextToken();
176+
continue;
177+
}
178+
}
179+
180+
} else if (lexer.identifierEquals(FnvHash.Constants.SPATIAL)) {
181+
lexer.nextToken();
182+
if (lexer.token() == Token.INDEX || lexer.token() == Token.KEY ||
183+
lexer.token() != Token.IDENTIFIER || !MySqlUtils.isBuiltinDataType(lexer.stringVal())) {
184+
MySqlTableIndex idx = new MySqlTableIndex();
185+
this.exprParser.parseIndex(idx.getIndexDefinition());
186+
idx.setIndexType("SPATIAL");
187+
idx.setParent(stmt);
188+
stmt.getTableElementList().add(idx);
189+
190+
if (lexer.token() == Token.RPAREN) {
191+
break;
192+
} else if (lexer.token() == Token.COMMA) {
193+
lexer.nextToken();
194+
continue;
195+
}
196+
} else {
197+
lexer.reset(mark);
198+
}
199+
} else {
200+
lexer.reset(mark);
201+
}
202+
}
203+
122204
if (lexer.token() == Token.FULLTEXT) {
123205
Lexer.SavePoint mark = lexer.mark();
124206
lexer.nextToken();
@@ -286,6 +368,9 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
286368
if (global) {
287369
unique.setGlobal(true);
288370
}
371+
if(local){
372+
unique.setLocal(true);
373+
}
289374
}
290375

291376
stmt.getTableElementList().add(constraint);
@@ -297,6 +382,10 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
297382
idx.getIndexDefinition().setGlobal(true);
298383
}
299384

385+
if (local) {
386+
idx.getIndexDefinition().setLocal(true);
387+
}
388+
300389
idx.setParent(stmt);
301390
stmt.getTableElementList().add(idx);
302391
} else if (lexer.token() == (Token.KEY)) {
@@ -836,6 +925,12 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
836925
continue;
837926
}
838927

928+
if (lexer.identifierEquals(FnvHash.Constants.LOCAL)) {
929+
SQLPartitionBy localPartitionClause = parseLocalPartitionBy();
930+
stmt.setLocalPartitioning(localPartitionClause);
931+
continue;
932+
}
933+
839934
if (lexer.identifierEquals(FnvHash.Constants.BROADCAST)) {
840935
lexer.nextToken();
841936
stmt.setBroadCast(true);
@@ -1060,6 +1155,51 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
10601155
return stmt;
10611156
}
10621157

1158+
public SQLPartitionBy parseLocalPartitionBy() {
1159+
lexer.nextToken();
1160+
accept(Token.PARTITION);
1161+
accept(Token.BY);
1162+
acceptIdentifier("RANGE");
1163+
1164+
SQLPartitionByRange partitionClause = new SQLPartitionByRange();
1165+
1166+
accept(Token.LPAREN);
1167+
partitionClause.addColumn(this.exprParser.name());
1168+
accept(Token.RPAREN);
1169+
1170+
if (lexer.identifierEquals(FnvHash.Constants.STARTWITH)) {
1171+
lexer.nextToken();
1172+
partitionClause.setStartWith(exprParser.expr());
1173+
}
1174+
1175+
partitionClause.setInterval(getExprParser().parseInterval());
1176+
1177+
if (lexer.identifierEquals("EXPIRE")) {
1178+
acceptIdentifier("EXPIRE");
1179+
acceptIdentifier("AFTER");
1180+
partitionClause.setExpireAfter((SQLIntegerExpr) exprParser.expr());
1181+
}
1182+
1183+
if (lexer.identifierEquals("PRE")) {
1184+
acceptIdentifier("PRE");
1185+
acceptIdentifier("ALLOCATE");
1186+
partitionClause.setPreAllocate((SQLIntegerExpr) exprParser.expr());
1187+
}
1188+
1189+
if (lexer.identifierEquals("PIVOTDATE")) {
1190+
acceptIdentifier("PIVOTDATE");
1191+
partitionClause.setPivotDateExpr(exprParser.expr());
1192+
}
1193+
1194+
if (lexer.token() == Token.DISABLE) {
1195+
lexer.nextToken();
1196+
acceptIdentifier("SCHEDULE");
1197+
partitionClause.setDisableSchedule(true);
1198+
}
1199+
1200+
return partitionClause;
1201+
}
1202+
10631203
public SQLPartitionBy parsePartitionBy() {
10641204
lexer.nextToken();
10651205
accept(Token.BY);

0 commit comments

Comments
 (0)