mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #871 from wenshao/master
improved sql parser for odps
This commit is contained in:
@@ -23,6 +23,7 @@ import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
@@ -36,9 +37,11 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
|
||||
protected List<SQLColumnDefinition> partitionColumns = new ArrayList<SQLColumnDefinition>(2);
|
||||
|
||||
protected SQLExpr lifecycle;
|
||||
|
||||
public OdpsCreateTableStatement() {
|
||||
super (JdbcConstants.ODPS);
|
||||
|
||||
protected SQLSelect select;
|
||||
|
||||
public OdpsCreateTableStatement(){
|
||||
super(JdbcConstants.ODPS);
|
||||
}
|
||||
|
||||
public SQLExprTableSource getLike() {
|
||||
@@ -73,6 +76,14 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
|
||||
this.lifecycle = lifecycle;
|
||||
}
|
||||
|
||||
public SQLSelect getSelect() {
|
||||
return select;
|
||||
}
|
||||
|
||||
public void setSelect(SQLSelect select) {
|
||||
this.select = select;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((OdpsASTVisitor) visitor);
|
||||
@@ -84,6 +95,7 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
|
||||
this.acceptChild(visitor, tableElementList);
|
||||
this.acceptChild(visitor, partitionColumns);
|
||||
this.acceptChild(visitor, lifecycle);
|
||||
this.acceptChild(visitor, select);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.odps.parser;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsCreateTableStatement;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
|
||||
@@ -52,11 +53,23 @@ public class OdpsCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
|
||||
stmt.setName(this.exprParser.name());
|
||||
|
||||
if (identifierEquals("LIFECYCLE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setLifecycle(this.exprParser.expr());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LIKE) {
|
||||
lexer.nextToken();
|
||||
SQLName name = this.exprParser.name();
|
||||
stmt.setLike(name);
|
||||
} else if (lexer.token() == Token.AS) {
|
||||
lexer.nextToken();
|
||||
|
||||
OdpsSelectParser selectParser = new OdpsSelectParser(this.exprParser);
|
||||
SQLSelect select = selectParser.select();
|
||||
|
||||
stmt.setSelect(select);
|
||||
} else {
|
||||
accept(Token.LPAREN);
|
||||
|
||||
|
||||
@@ -101,6 +101,11 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
print("LIFECYCLE ");
|
||||
x.getLifecycle().accept(this);
|
||||
}
|
||||
|
||||
if (x.getSelect() != null) {
|
||||
println();
|
||||
x.getSelect().accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alibaba.druid.bvt.sql.odps;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class OdpsCreateTableTest2 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE TABLE sale_detail as select * from dual;";
|
||||
OdpsStatementParser parser = new OdpsStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toOdpsString(stmt);
|
||||
Assert.assertEquals("CREATE TABLE sale_detailsale_detail"
|
||||
+ "\nSELECT *" //
|
||||
+ "\nFROM dual", output);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user