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:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.hive.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement;
|
||||
@@ -120,6 +121,11 @@ public class HiveCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.COMMENT) {
|
||||
lexer.nextToken();
|
||||
SQLExpr comment = this.exprParser.expr();
|
||||
stmt.setComment(comment);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.PARTITIONED) {
|
||||
lexer.nextToken();
|
||||
@@ -159,6 +165,23 @@ public class HiveCreateTableParser extends SQLCreateTableParser {
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.ROW) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("FORMAT");
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.DELIMITED)) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("FIELDS");
|
||||
acceptIdentifier("TERMINATED");
|
||||
accept(Token.BY);
|
||||
SQLExternalRecordFormat format = new SQLExternalRecordFormat();
|
||||
format.setTerminatedBy(this.exprParser.expr());
|
||||
stmt.setRowFormat(format);
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.info());
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.SORTED)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.hive.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement;
|
||||
@@ -69,6 +70,13 @@ public class HiveOutputVisitor extends SQLASTOutputVisitor implements HiveASTVis
|
||||
print(')');
|
||||
}
|
||||
|
||||
SQLExpr comment = x.getComment();
|
||||
if (comment != null) {
|
||||
println();
|
||||
print0(ucase ? "COMMENT " : "comment ");
|
||||
comment.accept(this);
|
||||
}
|
||||
|
||||
int partitionSize = x.getPartitionColumns().size();
|
||||
if (partitionSize > 0) {
|
||||
println();
|
||||
@@ -104,6 +112,13 @@ public class HiveOutputVisitor extends SQLASTOutputVisitor implements HiveASTVis
|
||||
print(')');
|
||||
}
|
||||
|
||||
SQLExternalRecordFormat format = x.getRowFormat();
|
||||
if (format != null) {
|
||||
println();
|
||||
print0(ucase ? "ROW FORMAT DELIMITED " : "row format delimited ");
|
||||
visit(format);
|
||||
}
|
||||
|
||||
List<SQLSelectOrderByItem> sortedBy = x.getSortedBy();
|
||||
if (sortedBy.size() > 0) {
|
||||
println();
|
||||
@@ -141,4 +156,20 @@ public class HiveOutputVisitor extends SQLASTOutputVisitor implements HiveASTVis
|
||||
public void endVisit(HiveCreateTableStatement x) {
|
||||
|
||||
}
|
||||
|
||||
public boolean visit(SQLExternalRecordFormat x) {
|
||||
if (x.getDelimitedBy() != null) {
|
||||
println();
|
||||
print0(ucase ? "LINES TERMINATED BY " : "lines terminated by ");
|
||||
x.getDelimitedBy().accept(this);
|
||||
}
|
||||
|
||||
if (x.getTerminatedBy() != null) {
|
||||
println();
|
||||
print0(ucase ? "FIELDS TERMINATED BY " : "fields terminated by ");
|
||||
x.getTerminatedBy().accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user