refactor sql parser for hive & odps support

This commit is contained in:
wenshao
2017-11-13 21:37:45 +08:00
parent 7a2609826a
commit c73191ad41
5 changed files with 64 additions and 81 deletions
@@ -34,30 +34,36 @@ import com.alibaba.druid.util.lang.Consumer;
public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement, SQLCreateStatement {
protected boolean ifNotExiists = false;
protected Type type;
protected SQLExprTableSource tableSource;
protected boolean ifNotExiists = false;
protected Type type;
protected SQLExprTableSource tableSource;
protected List<SQLTableElement> tableElementList = new ArrayList<SQLTableElement>();
protected List<SQLTableElement> tableElementList = new ArrayList<SQLTableElement>();
// for postgresql
protected SQLExprTableSource inherits;
protected SQLExprTableSource inherits;
protected SQLSelect select;
protected SQLSelect select;
protected SQLExpr comment;
protected SQLExpr comment;
protected SQLExprTableSource like;
protected SQLExprTableSource like;
protected Boolean compress;
protected Boolean logging;
protected Boolean compress;
protected Boolean logging;
protected SQLName tablespace;
protected SQLPartitionBy partitioning;
protected SQLName storedAs;
protected SQLName tablespace;
protected SQLPartitionBy partitioning;
protected SQLName storedAs;
protected boolean onCommitPreserveRows;
protected boolean onCommitDeleteRows;
protected boolean onCommitPreserveRows;
protected boolean onCommitDeleteRows;
// for hive & odps
protected final List<SQLColumnDefinition> partitionColumns = new ArrayList<SQLColumnDefinition>(2);
protected final List<SQLName> clusteredBy = new ArrayList<SQLName>();
protected final List<SQLSelectOrderByItem> sortedBy = new ArrayList<SQLSelectOrderByItem>();
protected int buckets;
public SQLCreateTableStatement(){
@@ -1044,4 +1050,35 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
this.onCommitPreserveRows = onCommitPreserveRows;
}
public List<SQLName> getClusteredBy() {
return clusteredBy;
}
public List<SQLSelectOrderByItem> getSortedBy() {
return sortedBy;
}
public void addSortedByItem(SQLSelectOrderByItem item) {
item.setParent(this);
this.sortedBy.add(item);
}
public int getBuckets() {
return buckets;
}
public void setBuckets(int buckets) {
this.buckets = buckets;
}
public List<SQLColumnDefinition> getPartitionColumns() {
return partitionColumns;
}
public void addPartitionColumn(SQLColumnDefinition column) {
if (column != null) {
column.setParent(this);
}
this.partitionColumns.add(column);
}
}
@@ -15,49 +15,20 @@
*/
package com.alibaba.druid.sql.dialect.hive.stmt;
import com.alibaba.druid.sql.ast.SQLExpr;
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.SQLSelectOrderByItem;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.JdbcConstants;
import java.util.ArrayList;
import java.util.List;
public class HiveCreateTableStatement extends SQLCreateTableStatement {
protected List<SQLColumnDefinition> partitionColumns = new ArrayList<SQLColumnDefinition>(2);
protected final List<SQLName> clusteredBy = new ArrayList<SQLName>();
protected final List<SQLSelectOrderByItem> sortedBy = new ArrayList<SQLSelectOrderByItem>();
protected int buckets;
public HiveCreateTableStatement() {
this.dbType = JdbcConstants.HIVE;
}
public List<SQLName> getClusteredBy() {
return clusteredBy;
}
public List<SQLSelectOrderByItem> getSortedBy() {
return sortedBy;
}
public void addSortedByItem(SQLSelectOrderByItem item) {
item.setParent(this);
this.sortedBy.add(item);
}
public int getBuckets() {
return buckets;
}
public void setBuckets(int buckets) {
this.buckets = buckets;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor instanceof HiveASTVisitor) {
@@ -31,13 +31,6 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
private SQLExprTableSource like;
protected List<SQLColumnDefinition> partitionColumns = new ArrayList<SQLColumnDefinition>(2);
protected final List<SQLName> clusteredBy = new ArrayList<SQLName>();
protected final List<SQLName> sortedBy = new ArrayList<SQLName>();
protected int buckets;
protected SQLExpr lifecycle;
public OdpsCreateTableStatement(){
@@ -56,17 +49,6 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
this.like = like;
}
public List<SQLColumnDefinition> getPartitionColumns() {
return partitionColumns;
}
public void addPartitionColumn(SQLColumnDefinition column) {
if (column != null) {
column.setParent(this);
}
this.partitionColumns.add(column);
}
public SQLExpr getLifecycle() {
return lifecycle;
}
@@ -92,20 +74,4 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement {
}
visitor.endVisit(this);
}
public List<SQLName> getClusteredBy() {
return clusteredBy;
}
public List<SQLName> getSortedBy() {
return sortedBy;
}
public int getBuckets() {
return buckets;
}
public void setBuckets(int buckets) {
this.buckets = buckets;
}
}
@@ -19,6 +19,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.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.dialect.odps.ast.OdpsCreateTableStatement;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
@@ -150,7 +151,15 @@ public class OdpsCreateTableParser extends SQLCreateTableParser {
lexer.nextToken();
accept(Token.BY);
accept(Token.LPAREN);
this.exprParser.names(stmt.getSortedBy());
for (; ; ) {
SQLSelectOrderByItem item = this.exprParser.parseSelectOrderByItem();
stmt.addSortedByItem(item);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
}
@@ -139,11 +139,11 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
print(')');
}
List<SQLName> sortedBy = x.getSortedBy();
List<SQLSelectOrderByItem> sortedBy = x.getSortedBy();
if (sortedBy.size() > 0) {
println();
print0(ucase ? "SORTED BY (" : "sorted by (");
printAndAccept(sortedBy, ",");
printAndAccept(sortedBy, ", ");
print(')');
}