improved odps sql parser support

This commit is contained in:
高铁
2021-06-28 20:58:45 +08:00
parent 6714647eb3
commit 92a84e9b71
12 changed files with 173 additions and 22 deletions
@@ -675,17 +675,33 @@ public class Lexer {
ch = charAt(++pos);
}
if (ch == '/' && pos + 1 < text.length() && text.charAt(pos + 1) == '*') {
int index = text.indexOf("*/", pos + 2);
if (index == -1) {
return SQLType.UNKNOWN;
}
if (ch == '/') {
if (pos + 1 < text.length() && text.charAt(pos + 1) == '*') {
int index = text.indexOf("*/", pos + 2);
if (index == -1) {
return SQLType.UNKNOWN;
}
pos = index + 2;
ch = charAt(pos);
continue;
pos = index + 2;
ch = charAt(pos);
continue;
} else if (pos + 2 < text.length()
&& text.charAt(pos + 1) == ' '
&& text.charAt(pos + 2) == '*'
&& dbType == DbType.odps
) {
int index = text.indexOf("* /", pos + 3);
if (index == -1) {
return SQLType.UNKNOWN;
}
pos = index + 3;
ch = charAt(pos);
continue;
}
}
if (ch == '-' && pos + 1 < text.length() && text.charAt(pos + 1) == '-') {
int index = text.indexOf('\n', pos + 2);
if (index == -1) {
@@ -1181,6 +1197,12 @@ public class Lexer {
bufPos = 0;
continue;
}
} else if (nextChar == ' ' && charAt(pos + 2) == ' ' && charAt(pos + 3) == '*') {
scanComment();
if ((token == Token.LINE_COMMENT || token == Token.MULTI_LINE_COMMENT) && skipComment) {
bufPos = 0;
continue;
}
} else {
token = Token.SLASH;
scanChar();
@@ -2091,6 +2113,12 @@ public class Lexer {
mark = pos;
bufPos = 0;
scanChar();
if (dbType == DbType.odps && ch == ' ') {
mark = pos;
bufPos = 0;
scanChar();
}
}
// /*+ */
@@ -2114,13 +2142,20 @@ public class Lexer {
for (;;) {
char c2;
if (ch == '*'
&& ((c2 = charAt(pos + 1)) == '/' || (c2 == ' ' && charAt(pos + 2) == '/'))
&& ((c2 = charAt(pos + 1)) == '/'
|| (c2 == ' ' && charAt(pos + 2) == '/')
|| (c2 == ' ' && charAt(pos + 2) == ' ' && charAt(pos + 3) == '/'))
) {
if (c2 == ' ') {
bufPos ++;
scanChar();
}
if (charAt(pos + 1) == ' ') {
bufPos ++;
scanChar();
}
bufPos += 2;
scanChar();
scanChar();
@@ -1271,6 +1271,7 @@ public class SQLExprParser extends SQLParser {
case ORDER:
case THEN:
case END:
case AS:
return new SQLIdentifierExpr(str);
default:
break;
@@ -2398,6 +2399,31 @@ public class SQLExprParser extends SQLParser {
return null;
}
public SQLZOrderBy parseZOrderBy() {
if (lexer.identifierEquals("ZORDER")) {
SQLZOrderBy orderBy = new SQLZOrderBy();
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.SIBLINGS)) {
lexer.nextToken();
orderBy.setSibings(true);
}
accept(Token.BY);
orderBy(orderBy.getItems(), orderBy);
if (lexer.token == Token.ORDER) {
throw new ParserException(lexer.info()); // dual order by
}
return orderBy;
}
return null;
}
public SQLOrderBy parseDistributeBy() {
if (lexer.token == Token.DISTRIBUTE || lexer.identifierEquals("DISTRIBUTE")) {
SQLOrderBy orderBy = new SQLOrderBy();
@@ -83,6 +83,10 @@ public class SQLParser {
|| token == Token.SELECT
|| token == Token.FROM
|| token == Token.WHERE) {
if (token == Token.WHERE && dbType == DbType.odps) {
return null;
}
if (must) {
throw new ParserException("illegal alias. " + lexer.info());
}
@@ -113,8 +117,18 @@ public class SQLParser {
case COMMA:
case WHERE:
case INNER:
case LEFT:
case RIGHT:
case FULL:
case ON:
return ident;
case JOIN:
if (hash != FnvHash.Constants.NATURAL
&& hash != FnvHash.Constants.CROSS) {
return ident;
}
lexer.reset(mark);
break;
default:
lexer.reset(mark);
break;
@@ -509,6 +523,8 @@ public class SQLParser {
case BY:
case EXCEPT:
case TABLESPACE:
case CREATE:
case DELETE:
alias = lexer.stringVal();
lexer.nextToken();
return alias;
@@ -1022,6 +1022,14 @@ public class SQLSelectParser extends SQLParser {
}
} else if (lexer.token == Token.LPAREN) {
tableSource = parseTableSource();
while (lexer.token == Token.UNION && tableSource instanceof SQLUnionQueryTableSource) {
SQLUnionQueryTableSource unionQueryTableSource = (SQLUnionQueryTableSource) tableSource;
SQLUnionQuery union = unionQueryTableSource.getUnion();
unionQueryTableSource.setUnion(
(SQLUnionQuery) queryRest(union)
);
}
accept(Token.RPAREN);
} else {
tableSource = parseTableSource();
@@ -911,6 +911,12 @@ public class SQLStatementParser extends SQLParser {
accept(Token.VIEW);
if (lexer.token == Token.IF) {
lexer.nextToken();
accept(Token.EXISTS);
stmt.setIfExists(true);
}
stmt.setName(this.exprParser.name());
return stmt;
}