mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add hive sql parser support
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.ast;
|
||||
|
||||
public abstract class HiveStatementImpl extends HiveSQLObjectImpl implements HiveStatement {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
||||
+4
-1
@@ -60,7 +60,10 @@ public class HiveCreateTableStatement extends SQLCreateTableStatement implements
|
||||
|
||||
@Override
|
||||
public void accept0(HiveASTVisitor visitor) {
|
||||
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, type);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.ast.stmt;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.HiveStatementImpl;
|
||||
import com.alibaba.druid.sql.dialect.hive.visitor.HiveASTVisitor;
|
||||
|
||||
public class HiveShowTablesStatement extends HiveStatementImpl {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private SQLCharExpr pattern;
|
||||
|
||||
|
||||
public SQLCharExpr getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
public void setPattern(SQLCharExpr pattern) {
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept0(HiveASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, pattern);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement.PartitionedBy;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class HiveCreateTableParser extends SQLCreateTableParser {
|
||||
|
||||
public HiveCreateTableParser(String sql){
|
||||
super(new HiveLexer(sql));
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
public HiveCreateTableParser(Lexer lexer){
|
||||
super(lexer);
|
||||
}
|
||||
|
||||
protected HiveCreateTableStatement newCreateStatement() {
|
||||
return new HiveCreateTableStatement();
|
||||
}
|
||||
|
||||
public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
|
||||
HiveCreateTableStatement stmt = (HiveCreateTableStatement) super.parseCrateTable(acceptCreate);
|
||||
|
||||
if (lexer.token() == Token.PARTITIONED) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
accept(Token.LPAREN);
|
||||
|
||||
PartitionedBy partitionedBy = new PartitionedBy();
|
||||
partitionedBy.setName(exprParser.name().toString());
|
||||
partitionedBy.setType(exprParser.parseDataType());
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
stmt.setPartitionedBy(partitionedBy);
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
}
|
||||
@@ -83,8 +83,9 @@ public class HiveLexer extends Lexer {
|
||||
map.put("ANY", Token.ANY);
|
||||
|
||||
map.put("WITH", Token.WITH);
|
||||
map.put("CURSOR", Token.CURSOR);
|
||||
map.put("TOP", Token.TOP);
|
||||
map.put("PARTITIONED", Token.PARTITIONED);
|
||||
map.put("SHOW", Token.SHOW);
|
||||
map.put("TABLES", Token.TABLES);
|
||||
|
||||
DEFAULT_SQL_SERVER_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.parser;
|
||||
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class HiveStatementParser extends SQLStatementParser {
|
||||
|
||||
@@ -15,4 +21,29 @@ public class HiveStatementParser extends SQLStatementParser {
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
public HiveCreateTableParser getSQLCreateTableParser() {
|
||||
return new HiveCreateTableParser(lexer);
|
||||
}
|
||||
|
||||
public boolean parseStatementListDialect(List<SQLStatement> statementList) {
|
||||
if (lexer.token() == Token.SHOW) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.TABLES) {
|
||||
lexer.nextToken();
|
||||
|
||||
HiveShowTablesStatement stmt = new HiveShowTablesStatement();
|
||||
|
||||
if (lexer.token() == Token.LITERAL_CHARS) {
|
||||
stmt.setPattern((SQLCharExpr) exprParser.primary());
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public interface HiveASTVisitor extends SQLASTVisitor {
|
||||
|
||||
void endVisit(HiveCreateTableStatement x);
|
||||
void endVisit(HiveShowTablesStatement x);
|
||||
|
||||
boolean visit(HiveShowTablesStatement x);
|
||||
|
||||
void endVisit(HiveCreateTableStatement x);
|
||||
|
||||
boolean visit(HiveCreateTableStatement x);
|
||||
|
||||
void endVisit(HiveCreateTableStatement.PartitionedBy x);
|
||||
|
||||
boolean visit(HiveCreateTableStatement.PartitionedBy x);
|
||||
}
|
||||
|
||||
+22
-2
@@ -1,14 +1,14 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
|
||||
|
||||
|
||||
public class HiveASTVisitorAdapter extends SQLASTVisitorAdapter implements HiveASTVisitor {
|
||||
|
||||
@Override
|
||||
public void endVisit(HiveCreateTableStatement x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -16,4 +16,24 @@ public class HiveASTVisitorAdapter extends SQLASTVisitorAdapter implements HiveA
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(HiveCreateTableStatement.PartitionedBy x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(HiveCreateTableStatement.PartitionedBy x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(HiveShowTablesStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(HiveShowTablesStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.alibaba.druid.sql.dialect.hive.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement.PartitionedBy;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
public class HiveOutputVisitor extends SQLASTOutputVisitor implements HiveASTVisitor {
|
||||
@@ -18,6 +20,42 @@ public class HiveOutputVisitor extends SQLASTOutputVisitor implements HiveASTVis
|
||||
@Override
|
||||
public boolean visit(HiveCreateTableStatement x) {
|
||||
visit((SQLCreateTableStatement) x);
|
||||
|
||||
if (x.getPartitionedBy() != null) {
|
||||
println();
|
||||
x.getPartitionedBy().accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(PartitionedBy x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(PartitionedBy x) {
|
||||
print("PARTITIONED BY (");
|
||||
print(x.getName());
|
||||
print(" ");
|
||||
x.getType().accept(this);
|
||||
print(")");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(HiveShowTablesStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(HiveShowTablesStatement x) {
|
||||
print("SHOW TABLES");
|
||||
if (x.getPattern() != null) {
|
||||
print(" ");
|
||||
x.getPattern().accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.alibaba.druid.sql.dialect.hive.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
|
||||
public class HiveSchemaStatVisitor extends SchemaStatVisitor implements HiveASTVisitor {
|
||||
@@ -12,7 +14,26 @@ public class HiveSchemaStatVisitor extends SchemaStatVisitor implements HiveASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(HiveCreateTableStatement x) {
|
||||
return visit((SQLCreateTableStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(HiveCreateTableStatement.PartitionedBy x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(HiveCreateTableStatement.PartitionedBy x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(HiveShowTablesStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(HiveShowTablesStatement x) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class SQLCreateTableParser extends SQLDDLParser {
|
||||
accept(Token.CREATE);
|
||||
}
|
||||
|
||||
SQLCreateTableStatement createTable = new SQLCreateTableStatement();
|
||||
SQLCreateTableStatement createTable = newCreateStatement();
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("GLOBAL")) {
|
||||
lexer.nextToken();
|
||||
@@ -97,4 +97,8 @@ public class SQLCreateTableParser extends SQLDDLParser {
|
||||
|
||||
return createTable;
|
||||
}
|
||||
|
||||
protected SQLCreateTableStatement newCreateStatement() {
|
||||
return new SQLCreateTableStatement();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,6 +154,11 @@ public enum Token {
|
||||
|
||||
// transact-sql
|
||||
TOP("TOP"),
|
||||
|
||||
// hive
|
||||
PARTITIONED("PARTITIONED"),
|
||||
SHOW("SHOW"),
|
||||
TABLES("TABLES"),
|
||||
|
||||
EOF,
|
||||
ERROR,
|
||||
|
||||
Reference in New Issue
Block a user