Skip to content

Commit

Permalink
Upgrade druid and refactor legacy parser
Browse files Browse the repository at this point in the history
  • Loading branch information
noCharger committed Mar 28, 2024
1 parent e153609 commit a12166d
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 222 deletions.
2 changes: 1 addition & 1 deletion legacy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ test {
}

dependencies {
implementation group: 'com.alibaba', name: 'druid', version:'1.0.15'
implementation group: 'com.alibaba', name: 'druid', version:'1.2.22'
implementation group: 'org.locationtech.spatial4j', name: 'spatial4j', version:'0.7'
implementation group: "org.opensearch.plugin", name: 'parent-join-client', version: "${opensearch_version}"
implementation group: "org.opensearch.plugin", name: 'reindex-client', version: "${opensearch_version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void scanIdentifier() {
this.ch = charAt(pos);

stringVal = subString(mark, bufPos);
Token tok = keywods.getKeyword(stringVal);
Token tok = keywords.getKeyword(stringVal);
if (tok != null) {
token = tok;
} else {
Expand Down Expand Up @@ -79,7 +79,7 @@ public void scanIdentifier() {
this.ch = charAt(pos);

stringVal = addSymbol();
Token tok = keywods.getKeyword(stringVal);
Token tok = keywords.getKeyword(stringVal);
if (tok != null) {
token = tok;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,18 @@

import static org.opensearch.sql.legacy.utils.StringUtils.isQuoted;

import com.alibaba.druid.sql.ast.SQLCommentHint;
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLDefaultExpr;
import com.alibaba.druid.sql.ast.expr.SQLExistsExpr;
import com.alibaba.druid.sql.ast.expr.SQLHexExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.ast.expr.SQLNotExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.expr.SQLUnaryExpr;
import com.alibaba.druid.sql.ast.expr.SQLUnaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalUnit;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSQLColumnDefinition;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLExprParser;
Expand Down Expand Up @@ -92,7 +67,7 @@ protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
methodInvokeExpr = new SQLMethodInvokeExpr(methodName);
}

if (isAggreateFunction(methodName)) {
if (isAggregateFunction(methodName)) {
SQLAggregateExpr aggregateExpr = parseAggregateExpr(methodName);

return aggregateExpr;
Expand Down Expand Up @@ -197,19 +172,6 @@ public SQLExpr primary() {
"AVG", "COUNT", "GROUP_CONCAT", "MAX", "MIN", "STDDEV", "SUM"
};

public SQLExpr relationalRest(SQLExpr expr) {
if (identifierEquals("REGEXP")) {
lexer.nextToken();
SQLExpr rightExp = equality();

rightExp = relationalRest(rightExp);

return new SQLBinaryOpExpr(expr, SQLBinaryOperator.RegExp, rightExp, JdbcConstants.MYSQL);
}

return super.relationalRest(expr);
}

public SQLExpr multiplicativeRest(SQLExpr expr) {
if (lexer.token() == Token.IDENTIFIER && "MOD".equalsIgnoreCase(lexer.stringVal())) {
lexer.nextToken();
Expand All @@ -223,19 +185,6 @@ public SQLExpr multiplicativeRest(SQLExpr expr) {
return super.multiplicativeRest(expr);
}

public SQLExpr notRationalRest(SQLExpr expr) {
if (identifierEquals("REGEXP")) {
lexer.nextToken();
SQLExpr rightExp = primary();

rightExp = relationalRest(rightExp);

return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRegExp, rightExp, JdbcConstants.MYSQL);
}

return super.notRationalRest(expr);
}

public SQLExpr primary2() {
final Token tok = lexer.token();

Expand Down Expand Up @@ -419,14 +368,14 @@ public final SQLExpr primaryRest(SQLExpr expr) {
}

String unitVal = lexer.stringVal();
MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal.toUpperCase());
SQLIntervalUnit unit = SQLIntervalUnit.valueOf(unitVal.toUpperCase());
lexer.nextToken();

accept(Token.FROM);

SQLExpr value = expr();

MySqlExtractExpr extract = new MySqlExtractExpr();
SQLExtractExpr extract = new SQLExtractExpr();
extract.setValue(value);
extract.setUnit(unit);
accept(Token.RPAREN);
Expand Down Expand Up @@ -499,7 +448,7 @@ public final SQLExpr primaryRest(SQLExpr expr) {
return primaryRest(expr);
} else if ("MATCH".equalsIgnoreCase(ident)) {
lexer.nextToken();
MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();
SQLMatchAgainstExpr matchAgainstExpr = new SQLMatchAgainstExpr();

if (lexer.token() == Token.RPAREN) {
lexer.nextToken();
Expand All @@ -525,17 +474,15 @@ public final SQLExpr primaryRest(SQLExpr expr) {
acceptIdentifier("QUERY");
acceptIdentifier("EXPANSION");
matchAgainstExpr.setSearchModifier(
MySqlMatchAgainstExpr.SearchModifier
.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
SQLMatchAgainstExpr.SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
} else {
matchAgainstExpr.setSearchModifier(
MySqlMatchAgainstExpr.SearchModifier.IN_NATURAL_LANGUAGE_MODE);
SQLMatchAgainstExpr.SearchModifier.IN_NATURAL_LANGUAGE_MODE);
}
} else if (identifierEquals("BOOLEAN")) {
lexer.nextToken();
acceptIdentifier("MODE");
matchAgainstExpr.setSearchModifier(
MySqlMatchAgainstExpr.SearchModifier.IN_BOOLEAN_MODE);
matchAgainstExpr.setSearchModifier(SQLMatchAgainstExpr.SearchModifier.IN_BOOLEAN_MODE);
} else {
throw new ParserException("Syntax error: " + lexer.token());
}
Expand Down Expand Up @@ -690,16 +637,16 @@ protected SQLExpr parseInterval() {
String unit = lexer.stringVal();
lexer.nextToken();

MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr();
SQLIntervalExpr intervalExpr = new SQLIntervalExpr();
intervalExpr.setValue(value);
intervalExpr.setUnit(MySqlIntervalUnit.valueOf(unit.toUpperCase()));
intervalExpr.setUnit(SQLIntervalUnit.valueOf(unit.toUpperCase()));

return intervalExpr;
}
}

public SQLColumnDefinition parseColumn() {
MySqlSQLColumnDefinition column = new MySqlSQLColumnDefinition();
SQLColumnDefinition column = new SQLColumnDefinition();
column.setName(name());
column.setDataType(parseDataType());

Expand All @@ -711,13 +658,13 @@ public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
lexer.nextToken();
accept(Token.UPDATE);
SQLExpr expr = this.expr();
((MySqlSQLColumnDefinition) column).setOnUpdate(expr);
column.setOnUpdate(expr);
}

if (identifierEquals("AUTO_INCREMENT")) {
lexer.nextToken();
if (column instanceof MySqlSQLColumnDefinition) {
((MySqlSQLColumnDefinition) column).setAutoIncrement(true);
if (column instanceof SQLColumnDefinition) {
((SQLColumnDefinition) column).setAutoIncrement(true);
}
return parseColumnRest(column);
}
Expand All @@ -734,8 +681,8 @@ public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
if (identifierEquals("STORAGE")) {
lexer.nextToken();
SQLExpr expr = expr();
if (column instanceof MySqlSQLColumnDefinition) {
((MySqlSQLColumnDefinition) column).setStorage(expr);
if (column instanceof SQLColumnDefinition) {
((SQLColumnDefinition) column).setStorage(expr);
}
}

Expand Down Expand Up @@ -862,11 +809,11 @@ public SQLName nameRest(SQLName name) {
return super.nameRest(name);
}

public MySqlSelectQueryBlock.Limit parseLimit() {
public SQLLimit parseLimit() {
if (lexer.token() == Token.LIMIT) {
lexer.nextToken();

MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit();
SQLLimit limit = new SQLLimit();

SQLExpr temp = this.expr();
if (lexer.token() == (Token.COMMA)) {
Expand Down Expand Up @@ -901,7 +848,13 @@ public MySqlPrimaryKey parsePrimaryKey() {

accept(Token.LPAREN);
for (; ; ) {
primaryKey.getColumns().add(this.expr());
SQLExpr expr;
if (lexer.token() == Token.LITERAL_ALIAS) {
expr = this.name();
} else {
expr = this.expr();
}
primaryKey.addColumn(expr);
if (!(lexer.token() == (Token.COMMA))) {
break;
} else {
Expand All @@ -928,12 +881,20 @@ public MySqlUnique parseUnique() {

if (lexer.token() != Token.LPAREN) {
SQLName indexName = name();
unique.setIndexName(indexName);
unique.setName(indexName);
}

accept(Token.LPAREN);
for (; ; ) {
unique.getColumns().add(this.expr());
SQLExpr column = this.expr();
if (lexer.token() == Token.ASC) {
column = new MySqlOrderingExpr(column, SQLOrderingSpecification.ASC);
lexer.nextToken();
} else if (lexer.token() == Token.DESC) {
column = new MySqlOrderingExpr(column, SQLOrderingSpecification.DESC);
lexer.nextToken();
}
unique.addColumn(column);
if (!(lexer.token() == (Token.COMMA))) {
break;
} else {
Expand Down Expand Up @@ -984,38 +945,23 @@ public MysqlForeignKey parseForeignKey() {
}
}

if (lexer.token() == Token.ON) {
while (lexer.token() == Token.ON) {
lexer.nextToken();

if (lexer.token() == Token.DELETE) {
fk.setReferenceOn(MysqlForeignKey.On.DELETE);
lexer.nextToken();

SQLForeignKeyImpl.Option option = parseReferenceOption();
fk.setOnDelete(option);
} else if (lexer.token() == Token.UPDATE) {
fk.setReferenceOn(MysqlForeignKey.On.UPDATE);
lexer.nextToken();

SQLForeignKeyImpl.Option option = parseReferenceOption();
fk.setOnUpdate(option);
} else {
throw new ParserException(
"Syntax error, expect DELETE or UPDATE, actual "
+ lexer.token()
+ " "
+ lexer.stringVal());
"syntax error, expect DELETE or UPDATE, actual " + lexer.token() + " " + lexer.info());
}
lexer.nextToken();

if (lexer.token() == Token.RESTRICT) {
fk.setReferenceOption(MysqlForeignKey.Option.RESTRICT);
} else if (identifierEquals("CASCADE")) {
fk.setReferenceOption(MysqlForeignKey.Option.CASCADE);
} else if (lexer.token() == Token.SET) {
accept(Token.NULL);
fk.setReferenceOption(MysqlForeignKey.Option.SET_NULL);
} else if (identifierEquals("ON")) {
lexer.nextToken();
if (identifierEquals("ACTION")) {
fk.setReferenceOption(MysqlForeignKey.Option.NO_ACTION);
} else {
throw new ParserException(
"Syntax error, expect ACTION, actual " + lexer.token() + " " + lexer.stringVal());
}
}
lexer.nextToken();
}
return fk;
}
Expand All @@ -1035,8 +981,8 @@ protected SQLAggregateExpr parseAggregateExprRest(SQLAggregateExpr aggregateExpr
return aggregateExpr;
}

public MySqlSelectGroupByExpr parseSelectGroupByItem() {
MySqlSelectGroupByExpr item = new MySqlSelectGroupByExpr();
public MySqlOrderingExpr parseSelectGroupByItem() {
MySqlOrderingExpr item = new MySqlOrderingExpr();

item.setExpr(expr());

Expand Down
Loading

0 comments on commit a12166d

Please sign in to comment.