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:
@@ -36,8 +36,11 @@ public class SQLUpdateSetItem extends SQLObjectImpl implements SQLReplaceable {
|
||||
return column;
|
||||
}
|
||||
|
||||
public void setColumn(SQLExpr column) {
|
||||
this.column = column;
|
||||
public void setColumn(SQLExpr x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.column = x;
|
||||
}
|
||||
|
||||
public SQLExpr getValue() {
|
||||
|
||||
@@ -120,6 +120,41 @@ public class HiveCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CLUSTERED)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.names(stmt.getClusteredBy());
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.SORTED)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
accept(Token.LPAREN);
|
||||
for (; ; ) {
|
||||
SQLSelectOrderByItem item = this.exprParser.parseSelectOrderByItem();
|
||||
stmt.addSortedByItem(item);
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
if (stmt.getClusteredBy().size() > 0 || stmt.getSortedBy().size() > 0) {
|
||||
accept(Token.INTO);
|
||||
if (lexer.token() == Token.LITERAL_INT) {
|
||||
stmt.setBuckets(lexer.integerValue().intValue());
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
throw new ParserException("into buckets must be integer. " + lexer.info());
|
||||
}
|
||||
acceptIdentifier("BUCKETS");
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.STORED)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.AS);
|
||||
@@ -132,7 +167,6 @@ public class HiveCreateTableParser extends SQLCreateTableParser {
|
||||
SQLSelect select = this.createSQLSelectParser().select();
|
||||
stmt.setSelect(select);
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,12 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.hive.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -54,4 +57,14 @@ public class HiveExprParser extends SQLExprParser {
|
||||
this.aggregateFunctions = AGGREGATE_FUNCTIONS;
|
||||
this.aggregateFunctionHashCodes = AGGREGATE_FUNCTIONS_CODES;
|
||||
}
|
||||
|
||||
public SQLExpr primaryRest(SQLExpr expr) {
|
||||
if (lexer.token() == Token.COLONCOLON) {
|
||||
lexer.nextToken();
|
||||
String propertyName = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
expr = new SQLPropertyExpr(expr, propertyName);
|
||||
}
|
||||
return super.primaryRest(expr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,11 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.hive.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectListCache;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class HiveSelectParser extends SQLSelectParser {
|
||||
|
||||
|
||||
@@ -15,11 +15,65 @@
|
||||
*/
|
||||
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.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 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) {
|
||||
accept0((HiveASTVisitor) visitor);
|
||||
} else {
|
||||
super.accept0(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
protected void accept0(HiveASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
this.acceptChild(visitor, tableSource);
|
||||
this.acceptChild(visitor, tableElementList);
|
||||
this.acceptChild(visitor, inherits);
|
||||
this.acceptChild(visitor, clusteredBy);
|
||||
this.acceptChild(visitor, sortedBy);
|
||||
this.acceptChild(visitor, select);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,10 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.hive.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public interface HiveASTVisitor extends SQLASTVisitor {
|
||||
boolean visit(HiveCreateTableStatement x);
|
||||
void endVisit(HiveCreateTableStatement x);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,17 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.hive.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
|
||||
|
||||
public class HiveASTVisitorAdapter extends SQLASTVisitorAdapter implements HiveASTVisitor {
|
||||
@Override
|
||||
public boolean visit(HiveCreateTableStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(HiveCreateTableStatement x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,12 @@
|
||||
*/
|
||||
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.dialect.hive.stmt.HiveCreateTableStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
import java.util.List;
|
||||
@@ -34,4 +37,84 @@ public class HiveOutputVisitor extends SQLASTOutputVisitor implements HiveASTVis
|
||||
public HiveOutputVisitor(Appendable appender, boolean parameterized) {
|
||||
super(appender, parameterized);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(HiveCreateTableStatement x) {
|
||||
printCreateTable(x, true);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printCreateTable(HiveCreateTableStatement x, boolean printSelect) {
|
||||
print0(ucase ? "CREATE " : "create ");
|
||||
|
||||
final SQLCreateTableStatement.Type tableType = x.getType();
|
||||
if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(tableType)) {
|
||||
print0(ucase ? "GLOBAL TEMPORARY " : "global temporary ");
|
||||
} else if (SQLCreateTableStatement.Type.LOCAL_TEMPORARY.equals(tableType)) {
|
||||
print0(ucase ? "LOCAL TEMPORARY " : "local temporary ");
|
||||
}
|
||||
print0(ucase ? "TABLE " : "table ");
|
||||
|
||||
if (x.isIfNotExiists()) {
|
||||
print0(ucase ? "IF NOT EXISTS " : "if not exists ");
|
||||
}
|
||||
|
||||
printTableSourceExpr(x.getName());
|
||||
|
||||
printTableElements(x.getTableElementList());
|
||||
|
||||
SQLExprTableSource inherits = x.getInherits();
|
||||
if (inherits != null) {
|
||||
print0(ucase ? " INHERITS (" : " inherits (");
|
||||
inherits.accept(this);
|
||||
print(')');
|
||||
}
|
||||
|
||||
List<SQLName> clusteredBy = x.getClusteredBy();
|
||||
if (clusteredBy.size() > 0) {
|
||||
println();
|
||||
print0(ucase ? "CLUSTERED BY (" : "clustered by (");
|
||||
printAndAccept(clusteredBy, ",");
|
||||
print(')');
|
||||
}
|
||||
|
||||
List<SQLSelectOrderByItem> sortedBy = x.getSortedBy();
|
||||
if (sortedBy.size() > 0) {
|
||||
println();
|
||||
print0(ucase ? "SORTED BY (" : "sorted by (");
|
||||
printAndAccept(sortedBy, ", ");
|
||||
print(')');
|
||||
}
|
||||
|
||||
int buckets = x.getBuckets();
|
||||
if (buckets > 0) {
|
||||
println();
|
||||
print0(ucase ? "INTO " : "into ");
|
||||
print(buckets);
|
||||
print0(ucase ? " BUCKETS" : " buckets");
|
||||
}
|
||||
|
||||
SQLName storedAs = x.getStoredAs();
|
||||
if (storedAs != null) {
|
||||
println();
|
||||
print0(ucase ? "STORE AS " : "store as ");
|
||||
printExpr(storedAs);
|
||||
}
|
||||
|
||||
SQLSelect select = x.getSelect();
|
||||
if (printSelect && select != null) {
|
||||
println();
|
||||
print0(ucase ? "AS" : "as");
|
||||
|
||||
println();
|
||||
visit(select);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(HiveCreateTableStatement x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
public class OdpsCreateTableParser extends SQLCreateTableParser {
|
||||
|
||||
@@ -137,7 +138,7 @@ public class OdpsCreateTableParser extends SQLCreateTableParser {
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("CLUSTERED")) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CLUSTERED)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
accept(Token.LPAREN);
|
||||
@@ -145,7 +146,7 @@ public class OdpsCreateTableParser extends SQLCreateTableParser {
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("SORTED")) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.SORTED)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
accept(Token.LPAREN);
|
||||
@@ -164,7 +165,7 @@ public class OdpsCreateTableParser extends SQLCreateTableParser {
|
||||
acceptIdentifier("BUCKETS");
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals("LIFECYCLE")) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.LIFECYCLE)) {
|
||||
lexer.nextToken();
|
||||
stmt.setLifecycle(this.exprParser.expr());
|
||||
}
|
||||
|
||||
@@ -611,5 +611,8 @@ public final class FnvHash {
|
||||
|
||||
long INT4 = fnv1a_64_lower("INT4");
|
||||
long VARBIT = fnv1a_64_lower("VARBIT");
|
||||
long CLUSTERED = fnv1a_64_lower("CLUSTERED");
|
||||
long SORTED = fnv1a_64_lower("SORTED");
|
||||
long LIFECYCLE = fnv1a_64_lower("LIFECYCLE");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user