Skip to content

Commit

Permalink
Fixs #alibaba#4927
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu2panpan committed Sep 9, 2022
1 parent eb781b6 commit cc829a1
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 1 deletion.
83 changes: 83 additions & 0 deletions src/main/java/com/alibaba/druid/sql/ast/SQLPartitionByRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
*/
package com.alibaba.druid.sql.ast;

import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public class SQLPartitionByRange extends SQLPartitionBy {
protected SQLExpr interval;
protected boolean isColumns = false;
protected SQLExpr startWith;
protected SQLIntegerExpr expireAfter;
protected SQLIntegerExpr preAllocate;
protected SQLExpr pivotDateExpr;
protected boolean disableSchedule = false;

public SQLPartitionByRange() {
}
Expand All @@ -35,11 +42,60 @@ public void setInterval(SQLExpr interval) {
this.interval = interval;
}

public SQLExpr getStartWith() {
return this.startWith;
}

public void setStartWith(final SQLExpr startWith) {
if (startWith != null) {
startWith.setParent(this);
}

this.startWith = startWith;
}

public SQLIntegerExpr getExpireAfter() {
return this.expireAfter;
}

public void setExpireAfter(final SQLIntegerExpr expireAfter) {
if (expireAfter != null) {
expireAfter.setParent(this);
}
this.expireAfter = expireAfter;
}

public SQLIntegerExpr getPreAllocate() {
return this.preAllocate;
}

public void setPreAllocate(final SQLIntegerExpr preAllocate) {
if (preAllocate != null) {
preAllocate.setParent(this);
}
this.preAllocate = preAllocate;
}

public SQLExpr getPivotDateExpr() {
return this.pivotDateExpr;
}

public void setPivotDateExpr(final SQLExpr pivotDateExpr) {
if (pivotDateExpr != null) {
pivotDateExpr.setParent(this);
}
this.pivotDateExpr = pivotDateExpr;
}

@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, columns);
acceptChild(visitor, interval);
acceptChild(visitor, expireAfter);
acceptChild(visitor, preAllocate);
acceptChild(visitor, pivotDateExpr);
acceptChild(visitor, columns);
acceptChild(visitor, storeIn);
acceptChild(visitor, partitions);
acceptChild(visitor, subPartitionBy);
Expand All @@ -56,16 +112,43 @@ public SQLPartitionByRange clone() {
x.setInterval(interval.clone());
}

if (startWith != null) {
x.setStartWith(startWith.clone());
}

if (expireAfter != null) {
x.setExpireAfter(expireAfter.clone());
}

for (SQLExpr column : columns) {
SQLExpr c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}

x.setColumns(this.isColumns);
x.setDisableSchedule(this.disableSchedule);

return x;
}

public void cloneTo(SQLPartitionByRange x) {
super.cloneTo(x);
}

public boolean isColumns() {
return isColumns;
}

public void setColumns(boolean columns) {
isColumns = columns;
}

public boolean isDisableSchedule() {
return this.disableSchedule;
}

public void setDisableSchedule(final boolean disableSchedule) {
this.disableSchedule = disableSchedule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS

protected SQLName tablespace;
protected SQLPartitionBy partitioning;
protected SQLPartitionBy localPartitioning;
protected SQLExpr storedAs;
protected SQLExpr location;

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

this.acceptChild(v, tablespace);
this.acceptChild(v, partitioning);
this.acceptChild(v, localPartitioning);
this.acceptChild(v, storedAs);
this.acceptChild(v, location);

Expand Down Expand Up @@ -348,6 +350,10 @@ public SQLPartitionBy getPartitioning() {
return partitioning;
}

public SQLPartitionBy getLocalPartitioning() {
return this.localPartitioning;
}

public void setPartitioning(SQLPartitionBy partitioning) {
if (partitioning != null) {
partitioning.setParent(this);
Expand All @@ -356,6 +362,14 @@ public void setPartitioning(SQLPartitionBy partitioning) {
this.partitioning = partitioning;
}

public void setLocalPartitioning(SQLPartitionBy localPartitioning) {
if (localPartitioning != null) {
localPartitioning.setParent(this);
}

this.localPartitioning = localPartitioning;
}

@Override
public List<SQLObject> getChildren() {
List<SQLObject> children = new ArrayList<SQLObject>();
Expand Down Expand Up @@ -1224,6 +1238,10 @@ public void cloneTo(SQLCreateTableStatement x) {
x.setPartitioning(partitioning.clone());
}

if (localPartitioning != null) {
x.setLocalPartitioning(localPartitioning.clone());
}

if (storedAs != null) {
x.setStoredAs(storedAs.clone());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ public void cloneTo(MySqlCreateTableStatement x) {
if (partitioning != null) {
x.setPartitioning(partitioning.clone());
}
if (localPartitioning != null) {
x.setLocalPartitioning(localPartitioning.clone());
}
for (SQLCommentHint hint : hints) {
SQLCommentHint h2 = hint.clone();
h2.setParent(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,88 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
}
}

boolean local = false;
if (lexer.identifierEquals(FnvHash.Constants.LOCAL)) {
final Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (lexer.token() == Token.INDEX || lexer.token() == Token.KEY
|| lexer.token() == Token.UNIQUE) {
local = true;
} else if (lexer.token() == Token.FULLTEXT) {
lexer.nextToken();

if (lexer.token() == Token.KEY) {
MySqlKey fulltextKey = new MySqlKey();
this.exprParser.parseIndex(fulltextKey.getIndexDefinition());
fulltextKey.setIndexType("FULLTEXT");
fulltextKey.setParent(stmt);
stmt.getTableElementList().add(fulltextKey);

while (lexer.token() == Token.HINT) {
lexer.nextToken();
}

if (lexer.token() == Token.RPAREN) {
break;
} else if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
} else if (lexer.token() == Token.INDEX) {
MySqlTableIndex idx = new MySqlTableIndex();
this.exprParser.parseIndex(idx.getIndexDefinition());
idx.setIndexType("FULLTEXT");
idx.setParent(stmt);
stmt.getTableElementList().add(idx);

if (lexer.token() == Token.RPAREN) {
break;
} else if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
} else if (lexer.token() == Token.IDENTIFIER && MySqlUtils.isBuiltinDataType(
lexer.stringVal())) {
lexer.reset(mark);
} else {
MySqlTableIndex idx = new MySqlTableIndex();
this.exprParser.parseIndex(idx.getIndexDefinition());
idx.setIndexType("FULLTEXT");
idx.setParent(stmt);
stmt.getTableElementList().add(idx);

if (lexer.token() == Token.RPAREN) {
break;
} else if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
}

} else if (lexer.identifierEquals(FnvHash.Constants.SPATIAL)) {
lexer.nextToken();
if (lexer.token() == Token.INDEX || lexer.token() == Token.KEY ||
lexer.token() != Token.IDENTIFIER || !MySqlUtils.isBuiltinDataType(lexer.stringVal())) {
MySqlTableIndex idx = new MySqlTableIndex();
this.exprParser.parseIndex(idx.getIndexDefinition());
idx.setIndexType("SPATIAL");
idx.setParent(stmt);
stmt.getTableElementList().add(idx);

if (lexer.token() == Token.RPAREN) {
break;
} else if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
} else {
lexer.reset(mark);
}
} else {
lexer.reset(mark);
}
}

if (lexer.token() == Token.FULLTEXT) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
Expand Down Expand Up @@ -286,6 +368,9 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
if (global) {
unique.setGlobal(true);
}
if(local){
unique.setLocal(true);
}
}

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

if (local) {
idx.getIndexDefinition().setLocal(true);
}

idx.setParent(stmt);
stmt.getTableElementList().add(idx);
} else if (lexer.token() == (Token.KEY)) {
Expand Down Expand Up @@ -836,6 +925,12 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
continue;
}

if (lexer.identifierEquals(FnvHash.Constants.LOCAL)) {
SQLPartitionBy localPartitionClause = parseLocalPartitionBy();
stmt.setLocalPartitioning(localPartitionClause);
continue;
}

if (lexer.identifierEquals(FnvHash.Constants.BROADCAST)) {
lexer.nextToken();
stmt.setBroadCast(true);
Expand Down Expand Up @@ -1060,6 +1155,51 @@ public MySqlCreateTableStatement parseCreateTable(boolean acceptCreate) {
return stmt;
}

public SQLPartitionBy parseLocalPartitionBy() {
lexer.nextToken();
accept(Token.PARTITION);
accept(Token.BY);
acceptIdentifier("RANGE");

SQLPartitionByRange partitionClause = new SQLPartitionByRange();

accept(Token.LPAREN);
partitionClause.addColumn(this.exprParser.name());
accept(Token.RPAREN);

if (lexer.identifierEquals(FnvHash.Constants.STARTWITH)) {
lexer.nextToken();
partitionClause.setStartWith(exprParser.expr());
}

partitionClause.setInterval(getExprParser().parseInterval());

if (lexer.identifierEquals("EXPIRE")) {
acceptIdentifier("EXPIRE");
acceptIdentifier("AFTER");
partitionClause.setExpireAfter((SQLIntegerExpr) exprParser.expr());
}

if (lexer.identifierEquals("PRE")) {
acceptIdentifier("PRE");
acceptIdentifier("ALLOCATE");
partitionClause.setPreAllocate((SQLIntegerExpr) exprParser.expr());
}

if (lexer.identifierEquals("PIVOTDATE")) {
acceptIdentifier("PIVOTDATE");
partitionClause.setPivotDateExpr(exprParser.expr());
}

if (lexer.token() == Token.DISABLE) {
lexer.nextToken();
acceptIdentifier("SCHEDULE");
partitionClause.setDisableSchedule(true);
}

return partitionClause;
}

public SQLPartitionBy parsePartitionBy() {
lexer.nextToken();
accept(Token.BY);
Expand Down
Loading

0 comments on commit cc829a1

Please sign in to comment.