From c73191ad41dcf88bcd4ec69f81b2cb85530b9c0c Mon Sep 17 00:00:00 2001 From: wenshao Date: Mon, 13 Nov 2017 21:37:45 +0800 Subject: [PATCH] refactor sql parser for hive & odps support --- .../statement/SQLCreateTableStatement.java | 67 ++++++++++++++----- .../hive/stmt/HiveCreateTableStatement.java | 29 -------- .../odps/ast/OdpsCreateTableStatement.java | 34 ---------- .../odps/parser/OdpsCreateTableParser.java | 11 ++- .../odps/visitor/OdpsOutputVisitor.java | 4 +- 5 files changed, 64 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateTableStatement.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateTableStatement.java index 1816a51ecb..3fff253dbc 100644 --- a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateTableStatement.java +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateTableStatement.java @@ -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 tableElementList = new ArrayList(); + protected List tableElementList = new ArrayList(); // 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 partitionColumns = new ArrayList(2); + protected final List clusteredBy = new ArrayList(); + protected final List sortedBy = new ArrayList(); + protected int buckets; public SQLCreateTableStatement(){ @@ -1044,4 +1050,35 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS this.onCommitPreserveRows = onCommitPreserveRows; } + public List getClusteredBy() { + return clusteredBy; + } + + public List 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 getPartitionColumns() { + return partitionColumns; + } + + public void addPartitionColumn(SQLColumnDefinition column) { + if (column != null) { + column.setParent(this); + } + this.partitionColumns.add(column); + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/hive/stmt/HiveCreateTableStatement.java b/src/main/java/com/alibaba/druid/sql/dialect/hive/stmt/HiveCreateTableStatement.java index e557b81c34..b33ed2b1c6 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/hive/stmt/HiveCreateTableStatement.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/hive/stmt/HiveCreateTableStatement.java @@ -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 partitionColumns = new ArrayList(2); - protected final List clusteredBy = new ArrayList(); - protected final List sortedBy = new ArrayList(); - protected int buckets; public HiveCreateTableStatement() { this.dbType = JdbcConstants.HIVE; } - public List getClusteredBy() { - return clusteredBy; - } - - public List 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) { diff --git a/src/main/java/com/alibaba/druid/sql/dialect/odps/ast/OdpsCreateTableStatement.java b/src/main/java/com/alibaba/druid/sql/dialect/odps/ast/OdpsCreateTableStatement.java index 20d55a4fac..a33624693e 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/odps/ast/OdpsCreateTableStatement.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/odps/ast/OdpsCreateTableStatement.java @@ -31,13 +31,6 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement { private SQLExprTableSource like; - protected List partitionColumns = new ArrayList(2); - - protected final List clusteredBy = new ArrayList(); - protected final List sortedBy = new ArrayList(); - - protected int buckets; - protected SQLExpr lifecycle; public OdpsCreateTableStatement(){ @@ -56,17 +49,6 @@ public class OdpsCreateTableStatement extends SQLCreateTableStatement { this.like = like; } - public List 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 getClusteredBy() { - return clusteredBy; - } - - public List getSortedBy() { - return sortedBy; - } - - public int getBuckets() { - return buckets; - } - - public void setBuckets(int buckets) { - this.buckets = buckets; - } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/odps/parser/OdpsCreateTableParser.java b/src/main/java/com/alibaba/druid/sql/dialect/odps/parser/OdpsCreateTableParser.java index a246edb231..9382d1f851 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/odps/parser/OdpsCreateTableParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/odps/parser/OdpsCreateTableParser.java @@ -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); } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/odps/visitor/OdpsOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/odps/visitor/OdpsOutputVisitor.java index 72475b00ae..afda1ccfaa 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/odps/visitor/OdpsOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/odps/visitor/OdpsOutputVisitor.java @@ -139,11 +139,11 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis print(')'); } - List sortedBy = x.getSortedBy(); + List sortedBy = x.getSortedBy(); if (sortedBy.size() > 0) { println(); print0(ucase ? "SORTED BY (" : "sorted by ("); - printAndAccept(sortedBy, ","); + printAndAccept(sortedBy, ", "); print(')'); }