improved sql paser for aliyun analyticdb.

This commit is contained in:
wenshao
2018-05-31 13:51:32 +08:00
parent 9c8b14bd4a
commit ca38876d1e
10 changed files with 444 additions and 1 deletions
@@ -0,0 +1,85 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLDumpStatement extends SQLStatementImpl {
private boolean overwrite;
private SQLExprTableSource into;
private SQLSelect select;
public SQLDumpStatement() {
}
public SQLSelect getSelect() {
return select;
}
public void setSelect(SQLSelect x) {
if (x != null) {
x.setParent(this);
}
this.select = x;
}
public SQLExprTableSource getInto() {
return into;
}
public void setInto(SQLExpr x) {
if (x == null) {
return;
}
setInto(new SQLExprTableSource(x));
}
public void setInto(SQLExprTableSource x) {
if (x != null) {
x.setParent(this);
}
this.into = x;
}
public boolean isOverwrite() {
return overwrite;
}
public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
if (into != null) {
into.accept(visitor);
}
if (select != null) {
select.accept(visitor);
}
}
visitor.endVisit(this);
}
}
@@ -370,6 +370,13 @@ public class SQLStatementParser extends SQLParser {
continue;
}
if (lexer.identifierEquals("DUMP")) {
SQLStatement stmt = parseDump();
statementList.add(stmt);
continue;
}
if (lexer.identifierEquals(FnvHash.Constants.COMMIT)) {
SQLStatement stmt = parseCommit();
@@ -427,6 +434,26 @@ public class SQLStatementParser extends SQLParser {
}
}
public SQLStatement parseDump() {
SQLDumpStatement stmt = new SQLDumpStatement();
acceptIdentifier("DUMP");
acceptIdentifier("DATA");
if (lexer.identifierEquals(FnvHash.Constants.OVERWRITE)) {
lexer.nextToken();
stmt.setOverwrite(true);
}
if (lexer.token == Token.INTO) {
lexer.nextToken();
stmt.setInto(this.exprParser.expr());
}
SQLSelect select = createSQLSelectParser().select();
stmt.setSelect(select);
return stmt;
}
public SQLStatement parseDrop() {
List<String> beforeComments = null;
@@ -6579,6 +6579,32 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
return false;
}
@Override
public boolean visit(SQLDumpStatement x) {
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
if (headHints != null) {
for (SQLCommentHint hint : headHints) {
hint.accept(this);
println();
}
}
print0(ucase ? "DUMP DATA " : "dump data ");
if (x.isOverwrite()) {
print0(ucase ? "OVERWRITE " : "overwrite ");
}
SQLExprTableSource into = x.getInto();
if (into != null) {
into.accept(this);
}
x.getSelect().accept(this);
return false;
}
public void print(float value) {
if (this.appender == null) {
return;
@@ -842,4 +842,7 @@ public interface SQLASTVisitor {
boolean visit(SQLWindow x);
void endVisit(SQLWindow x);
boolean visit(SQLDumpStatement x);
void endVisit(SQLDumpStatement x);
}
@@ -1990,7 +1990,6 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
}
@Override
public boolean visit(SQLWindow x) {
return true;
@@ -2000,6 +1999,14 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
}
@Override
public boolean visit(SQLDumpStatement x) {
return true;
}
public void endVisit(SQLDumpStatement x) {
}
public final boolean isEnabled(VisitorFeature feature) {
return VisitorFeature.isEnabled(this.features, feature);
@@ -2515,4 +2515,23 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
return false;
}
public boolean visit(SQLDumpStatement x) {
if (repository != null
&& x.getParent() == null) {
repository.resolve(x);
}
final SQLExprTableSource into = x.getInto();
if (into != null) {
into.accept(this);
}
final SQLSelect select = x.getSelect();
if (select != null) {
select.accept(this);
}
return false;
}
}
@@ -649,5 +649,7 @@ public final class FnvHash {
long GENERATED = fnv1a_64_lower("GENERATED");
long ALWAYS = fnv1a_64_lower("ALWAYS");
long INCREMENT = fnv1a_64_lower("INCREMENT");
long OVERWRITE = fnv1a_64_lower("OVERWRITE");
}
}