Skip to content

Commit

Permalink
improved oracle sql parser
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Nov 3, 2019
1 parent 1cdd3f2 commit bd8e80b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Expand Up @@ -829,7 +829,8 @@ protected SQLTableSource parseTableSourceRest(OracleSelectTableSource tableSourc
tableSource.setAlias(tableAlias(true));
} else if ((tableSource.getAlias() == null) || (tableSource.getAlias().length() == 0)) {
if (lexer.token() != Token.LEFT && lexer.token() != Token.RIGHT && lexer.token() != Token.FULL) {
tableSource.setAlias(tableAlias());
final String tableAlias = tableAlias();
tableSource.setAlias(tableAlias);
}
}

Expand Down Expand Up @@ -866,10 +867,19 @@ protected SQLTableSource parseTableSourceRest(OracleSelectTableSource tableSourc
joinType = SQLJoinTableSource.JoinType.FULL_OUTER_JOIN;
}

boolean natural = lexer.identifierEquals(FnvHash.Constants.NATURAL);
if (natural) {
lexer.nextToken();
}

if (lexer.token() == Token.INNER) {
lexer.nextToken();
accept(Token.JOIN);
joinType = SQLJoinTableSource.JoinType.INNER_JOIN;
if (natural) {
joinType = SQLJoinTableSource.JoinType.NATURAL_INNER_JOIN;
} else {
joinType = SQLJoinTableSource.JoinType.INNER_JOIN;
}
}
if (lexer.token() == Token.CROSS) {
lexer.nextToken();
Expand All @@ -879,7 +889,11 @@ protected SQLTableSource parseTableSourceRest(OracleSelectTableSource tableSourc

if (lexer.token() == Token.JOIN) {
lexer.nextToken();
joinType = SQLJoinTableSource.JoinType.JOIN;
if (natural) {
joinType = SQLJoinTableSource.JoinType.NATURAL_JOIN;
} else {
joinType = SQLJoinTableSource.JoinType.JOIN;
}
}

if (lexer.token() == (Token.COMMA)) {
Expand All @@ -894,7 +908,8 @@ protected SQLTableSource parseTableSourceRest(OracleSelectTableSource tableSourc

SQLTableSource right;
right = parseTableSourcePrimary();
right.setAlias(this.tableAlias());
String tableAlias = tableAlias();
right.setAlias(tableAlias);
join.setRight(right);

if (lexer.token() == Token.ON) {
Expand All @@ -918,6 +933,10 @@ protected SQLTableSource parseTableSourceRest(OracleSelectTableSource tableSourc
parsePivot(join);

return parseTableSourceRest(join);
} else {
if (lexer.identifierEquals(FnvHash.Constants.PIVOT)) {
parsePivot(tableSource);
}
}

return tableSource;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/alibaba/druid/sql/parser/SQLParser.java
Expand Up @@ -135,7 +135,8 @@ protected String tableAlias(boolean must) {
return ident;
} else if (hash == FnvHash.Constants.DISTRIBUTE
|| hash == FnvHash.Constants.SORT
|| hash == FnvHash.Constants.CLUSTER) {
|| hash == FnvHash.Constants.CLUSTER
) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (lexer.token == Token.BY) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/alibaba/druid/util/FnvHash.java
Expand Up @@ -693,5 +693,6 @@ public static interface Constants {
long MODEL = fnv1a_64_lower("MODEL");
long DIMENSION = fnv1a_64_lower("DIMENSION");
long KEEP = fnv1a_64_lower("KEEP");
long PIVOT = fnv1a_64_lower("PIVOT");
}
}

0 comments on commit bd8e80b

Please sign in to comment.