Skip to content

Commit

Permalink
Fix sr index parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
lingo-xp authored and wenshao committed Nov 23, 2024
1 parent 94fef5c commit 33eb051
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static com.alibaba.druid.sql.parser.Token.ALTER;
import static com.alibaba.druid.sql.parser.Token.FALSE;
import static com.alibaba.druid.sql.parser.Token.GROUP;
import static com.alibaba.druid.sql.parser.Token.INDEX;
import static com.alibaba.druid.sql.parser.Token.SEMI;
import static com.alibaba.druid.sql.parser.Token.TABLE;
import static com.alibaba.druid.sql.parser.Token.TRUE;
Expand Down Expand Up @@ -319,7 +320,7 @@ protected SQLStatement createTableRest(Lexer.SavePoint mark) {
}

if (lexer.token() == Token.UNIQUE
|| lexer.token() == Token.INDEX
|| lexer.token() == INDEX
|| lexer.identifierEquals(FnvHash.Constants.SPATIAL)
|| lexer.identifierEquals(FnvHash.Constants.ANN)
|| lexer.identifierEquals(FnvHash.Constants.GLOBAL)
Expand Down Expand Up @@ -358,7 +359,7 @@ public SQLStatement parseCreateFullTextStatement() {
} else if (lexer.identifierEquals(FnvHash.Constants.ANALYZER)) {
lexer.nextToken();
return parseFullTextAnalyzer();
} else if (lexer.token() == Token.INDEX) {
} else if (lexer.token() == INDEX) {
lexer.reset(mark);
return parseCreateIndex();
} else if (lexer.identifierEquals(FnvHash.Constants.DICTIONARY)) {
Expand Down Expand Up @@ -2707,7 +2708,7 @@ public SQLStatement parseShow() {
}

// DRDS GSI syntax.
if (isEnabled(SQLParserFeature.DRDSAsyncDDL) && (Token.INDEX == lexer.token() || lexer.identifierEquals("INDEXES"))) {
if (isEnabled(SQLParserFeature.DRDSAsyncDDL) && (INDEX == lexer.token() || lexer.identifierEquals("INDEXES"))) {
lexer.nextToken();

DrdsShowGlobalIndex stmt = new DrdsShowGlobalIndex();
Expand Down Expand Up @@ -3153,7 +3154,7 @@ public SQLStatement parseShow() {
return stmt;
}

if (lexer.token() == Token.INDEX || lexer.identifierEquals("INDEXES") || lexer.identifierEquals("KEYS")) {
if (lexer.token() == INDEX || lexer.identifierEquals("INDEXES") || lexer.identifierEquals("KEYS")) {
SQLShowIndexesStatement stmt = new SQLShowIndexesStatement();

stmt.setType(lexer.stringVal());
Expand Down Expand Up @@ -6276,7 +6277,7 @@ else if (lexer.identifierEquals(FnvHash.Constants.PARTITIONS)) {

case ALTER: {
lexer.nextToken();
if (lexer.token() == Token.INDEX) {
if (lexer.token() == INDEX) {
// Caution: Not in MySql documents.
lexer.nextToken();

Expand All @@ -6298,7 +6299,7 @@ else if (lexer.identifierEquals(FnvHash.Constants.PARTITIONS)) {
accept(Token.SET);
accept(Token.FULLTEXT);

if (lexer.token() == Token.INDEX) {
if (lexer.token() == INDEX) {
lexer.nextToken();
alterIndex.setAnalyzerType(AnalyzerIndexType.INDEX);
} else if (lexer.identifierEquals(FnvHash.Constants.QUERY)) {
Expand Down Expand Up @@ -7706,7 +7707,7 @@ protected void parseAlterTableAddColumn(SQLAlterTableStatement stmt) {

public void parseAlterDrop(SQLAlterTableStatement stmt) {
lexer.nextToken();
if (lexer.token() == Token.INDEX) {
if (lexer.token() == INDEX) {
lexer.nextToken();
SQLName indexName = this.exprParser.name();
SQLAlterTableDropIndex item = new SQLAlterTableDropIndex();
Expand Down Expand Up @@ -9298,7 +9299,7 @@ protected void parseCreateMaterializedViewRest(SQLCreateMaterializedViewStatemen
lexer.nextToken();
continue;
}
} else if (lexer.token() == Token.INDEX) {
} else if (lexer.token() == INDEX) {
MySqlTableIndex idx = new MySqlTableIndex();
this.exprParser.parseIndex(idx.getIndexDefinition());
idx.setIndexType("CLUSTERED");
Expand All @@ -9325,7 +9326,7 @@ protected void parseCreateMaterializedViewRest(SQLCreateMaterializedViewStatemen
SQLConstraint constraint = this.exprParser.parseConstraint();
constraint.setParent(stmt);
stmt.getTableElementList().add((SQLTableElement) constraint);
} else if (lexer.token() == (Token.INDEX)) {
} else if (lexer.token() == (INDEX)) {
MySqlTableIndex idx = new MySqlTableIndex();
this.exprParser.parseIndex(idx.getIndexDefinition());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alibaba.druid.sql.dialect.starrocks.ast;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
Expand All @@ -17,7 +18,7 @@ public class StarRocksIndexDefinition extends SQLObjectImpl implements SQLTableE
private SQLName indexName;
private List<SQLName> columns = new ArrayList<SQLName>();
private boolean usingBitmap;
private String comment;
private SQLExpr comment;

public SQLName getIndexName() {
return indexName;
Expand All @@ -43,11 +44,11 @@ public void setUsingBitmap(boolean usingBitmap) {
this.usingBitmap = usingBitmap;
}

public String getComment() {
public SQLExpr getComment() {
return comment;
}

public void setComment(String comment) {
public void setComment(SQLExpr comment) {
this.comment = comment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksAggregateKey;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksDuplicateKey;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksIndexDefinition;
import com.alibaba.druid.sql.dialect.starrocks.ast.statement.StarRocksCreateTableStatement;
import com.alibaba.druid.sql.parser.*;
import com.alibaba.druid.util.FnvHash;
Expand Down Expand Up @@ -54,6 +55,37 @@ public void parseUniqueKey(SQLCreateTableStatement stmt) {
accept(Token.RPAREN);
stmt.setUnique(sqlUnique);
}

protected void parseIndex(SQLCreateTableStatement createTable) {
if (lexer.token() == Token.INDEX) {
StarRocksIndexDefinition index = new StarRocksIndexDefinition();
lexer.nextToken();
index.setIndexName(this.exprParser.name());
accept(Token.LPAREN);
for (; ; ) {
index.getColumns().add(this.exprParser.name());
if (!(lexer.token() == (Token.COMMA))) {
break;
} else {
lexer.nextToken();
}
}
accept(Token.RPAREN);
if (lexer.token() == Token.USING) {
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.BITMAP)) {
lexer.nextToken();
index.setUsingBitmap(true);
}
}
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
index.setComment(this.exprParser.expr());
}
index.setParent(createTable);
createTable.getTableElementList().add(index);
}
}
public void parseCreateTableRest(SQLCreateTableStatement stmt) {
StarRocksCreateTableStatement srStmt = (StarRocksCreateTableStatement) stmt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKeyImpl;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksAggregateKey;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksDuplicateKey;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksIndexDefinition;
import com.alibaba.druid.sql.dialect.starrocks.ast.statement.StarRocksCreateResourceStatement;
import com.alibaba.druid.sql.dialect.starrocks.ast.statement.StarRocksCreateTableStatement;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
Expand Down Expand Up @@ -250,4 +251,20 @@ public boolean visit(StarRocksCreateResourceStatement x) {
print(x.getProperties());
return false;
}

public boolean visit(StarRocksIndexDefinition x) {
print0(ucase ? "INDEX " : "index ");
x.getIndexName().accept(this);
print('(');
printAndAccept(x.getColumns(), ", ");
print(')');
if (x.isUsingBitmap()) {
print0(ucase ? " USING BITMAP" : " using bitmap");
}
if (x.getComment() != null) {
print0(ucase ? " COMMENT " : " comment ");
x.getComment().accept(this);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,19 @@ && dialectFeatureEnabled(CreateTableBodySupplemental)) {
acceptIdentifier("PROPERTIES");
tableLike.setExcludeProperties(true);
}
} else if (lexer.token() == Token.INDEX) {
parseIndex(createTable);
} else {
SQLColumnDefinition column = this.exprParser.parseColumn();
createTable.getTableElementList().add(column);
}
}

protected void parseIndex(SQLCreateTableStatement createTable) {
SQLColumnDefinition column = this.exprParser.parseColumn();
createTable.getTableElementList().add(column);
}

protected void createTableBefore(SQLCreateTableStatement createTable) {
if (lexer.nextIfIdentifier("GLOBAL")) {
createTable.config(SQLCreateTableStatement.Feature.Global);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleForStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleFunctionDataType;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleProcedureDataType;
import com.alibaba.druid.sql.dialect.starrocks.ast.StarRocksIndexDefinition;
import com.alibaba.druid.sql.template.SQLSelectQueryTemplate;
import com.alibaba.druid.util.FnvHash;
import com.alibaba.druid.util.JdbcUtils;
Expand Down Expand Up @@ -12125,22 +12124,6 @@ public boolean visit(SQLAlterProcedureStatement x) {
}
return false;
}

public boolean visit(StarRocksIndexDefinition x) {
print0(ucase ? "INDEX " : "index ");
x.getIndexName().accept(this);
print('(');
printAndAccept(x.getColumns(), ", ");
print(')');
if (x.isUsingBitmap()) {
print0(ucase ? " USING BITMAP" : " using bitmap");
}
if (x.getComment() != null) {
print0(ucase ? " COMMENT " : " comment ");
print(x.getComment());
}
return false;
}
protected void tryPrintLparen(SQLExprImpl x) {
if (x.isParenthesized()) {
print('(');
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/com/alibaba/druid/util/FnvHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -1165,5 +1165,6 @@ public static interface Constants {
long NOCOMPRESS = fnv1a_64_lower("NOCOMPRESS");
long COMPRESS_MODE = fnv1a_64_lower("COMPRESS_MODE");
long BROKER = fnv1a_64_lower("BROKER");
long INDEX = fnv1a_64_lower("INDEX");
}
}
Loading

0 comments on commit 33eb051

Please sign in to comment.