mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved hive sql parser support.
This commit is contained in:
@@ -120,6 +120,37 @@ public class HiveCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.PARTITIONED) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
accept(Token.LPAREN);
|
||||
|
||||
for (;;) {
|
||||
if (lexer.token() != Token.IDENTIFIER) {
|
||||
throw new ParserException("expect identifier. " + lexer.info());
|
||||
}
|
||||
|
||||
SQLColumnDefinition column = this.exprParser.parseColumn();
|
||||
stmt.addPartitionColumn(column);
|
||||
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
column.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
if (lexer.token() != Token.COMMA) {
|
||||
break;
|
||||
} else {
|
||||
lexer.nextToken();
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
column.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CLUSTERED)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
@@ -39,6 +39,7 @@ public class HiveLexer extends Lexer {
|
||||
|
||||
map.put("ROW", Token.ROW);
|
||||
map.put("LIMIT", Token.LIMIT);
|
||||
map.put("PARTITIONED", Token.PARTITIONED);
|
||||
|
||||
DEFAULT_HIVE_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ 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;
|
||||
@@ -27,6 +28,7 @@ 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;
|
||||
@@ -65,6 +67,17 @@ public class HiveCreateTableStatement extends SQLCreateTableStatement {
|
||||
}
|
||||
}
|
||||
|
||||
public List<SQLColumnDefinition> getPartitionColumns() {
|
||||
return partitionColumns;
|
||||
}
|
||||
|
||||
public void addPartitionColumn(SQLColumnDefinition column) {
|
||||
if (column != null) {
|
||||
column.setParent(this);
|
||||
}
|
||||
this.partitionColumns.add(column);
|
||||
}
|
||||
|
||||
protected void accept0(HiveASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
this.acceptChild(visitor, tableSource);
|
||||
|
||||
@@ -16,10 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
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.ast.statement.SQLSelectOrderByItem;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
@@ -72,6 +69,33 @@ public class HiveOutputVisitor extends SQLASTOutputVisitor implements HiveASTVis
|
||||
print(')');
|
||||
}
|
||||
|
||||
int partitionSize = x.getPartitionColumns().size();
|
||||
if (partitionSize > 0) {
|
||||
println();
|
||||
print0(ucase ? "PARTITIONED BY (" : "partitioned by (");
|
||||
this.indentCount++;
|
||||
println();
|
||||
for (int i = 0; i < partitionSize; ++i) {
|
||||
SQLColumnDefinition column = x.getPartitionColumns().get(i);
|
||||
column.accept(this);
|
||||
|
||||
if (i != partitionSize - 1) {
|
||||
print(',');
|
||||
}
|
||||
if (this.isPrettyFormat() && column.hasAfterComment()) {
|
||||
print(' ');
|
||||
printlnComment(column.getAfterCommentsDirect());
|
||||
}
|
||||
|
||||
if (i != partitionSize - 1) {
|
||||
println();
|
||||
}
|
||||
}
|
||||
this.indentCount--;
|
||||
println();
|
||||
print(')');
|
||||
}
|
||||
|
||||
List<SQLName> clusteredBy = x.getClusteredBy();
|
||||
if (clusteredBy.size() > 0) {
|
||||
println();
|
||||
|
||||
Reference in New Issue
Block a user