diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateViewStatement.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateViewStatement.java index 49e56e91e7..78e710a6cf 100644 --- a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateViewStatement.java +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLCreateViewStatement.java @@ -50,6 +50,10 @@ public class SQLCreateViewStatement extends SQLStatementImpl implements SQLCreat private SQLLiteralExpr comment; + // clickhouse + protected boolean onCluster; + private SQLName to; + public SQLCreateViewStatement(){ } @@ -308,6 +312,24 @@ public class SQLCreateViewStatement extends SQLStatementImpl implements SQLCreat } } + public boolean isOnCluster() { + return onCluster; + } + + public void setOnCluster(boolean onCluster) { + this.onCluster = onCluster; + } + + public SQLName getTo() { + return to; + } + + public void setTo(SQLName x) { + if (x != null) { + x.setParent(this); + } + this.to = x; + } public SQLCreateViewStatement clone() { SQLCreateViewStatement x = new SQLCreateViewStatement(); @@ -341,6 +363,11 @@ public class SQLCreateViewStatement extends SQLStatementImpl implements SQLCreat x.setComment(comment.clone()); } + x.onCluster = onCluster; + if (x.to != null) { + to = x.to.clone(); + } + return x; } } diff --git a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLJoinTableSource.java b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLJoinTableSource.java index 65b9d81e2c..e9828b4507 100644 --- a/src/main/java/com/alibaba/druid/sql/ast/statement/SQLJoinTableSource.java +++ b/src/main/java/com/alibaba/druid/sql/ast/statement/SQLJoinTableSource.java @@ -38,6 +38,7 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace protected boolean natural = false; protected UDJ udj; // for maxcompute protected boolean asof; // for clickhouse + protected boolean global; // for clickhouse public SQLJoinTableSource(String alias){ super(alias); @@ -649,6 +650,14 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace return null; } + public boolean isGlobal() { + return global; + } + + public void setGlobal(boolean global) { + this.global = global; + } + @Override public int hashCode() { int result = left != null ? left.hashCode() : 0; @@ -657,6 +666,7 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace result = 31 * result + (condition != null ? condition.hashCode() : 0); result = 31 * result + using.hashCode(); result = 31 * result + (natural ? 1 : 0); + result = 31 * result + (global ? 1 : 0); return result; } @@ -664,15 +674,19 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; SQLJoinTableSource that = (SQLJoinTableSource) o; if (natural != that.natural) return false; + if (asof != that.asof) return false; + if (global != that.global) return false; if (left != null ? !left.equals(that.left) : that.left != null) return false; if (joinType != that.joinType) return false; if (right != null ? !right.equals(that.right) : that.right != null) return false; if (condition != null ? !condition.equals(that.condition) : that.condition != null) return false; - return using.equals(that.using); + if (using != null ? !using.equals(that.using) : that.using != null) return false; + return udj != null ? udj.equals(that.udj) : that.udj == null; } public void splitTo(List outTableSources, JoinType joinType) { diff --git a/src/main/java/com/alibaba/druid/sql/parser/SQLSelectParser.java b/src/main/java/com/alibaba/druid/sql/parser/SQLSelectParser.java index 122b2ba0dd..a229d07939 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/SQLSelectParser.java +++ b/src/main/java/com/alibaba/druid/sql/parser/SQLSelectParser.java @@ -1330,6 +1330,14 @@ public class SQLSelectParser extends SQLParser { } } + boolean global = false; + if (dbType == DbType.clickhouse) { + if (lexer.token == Token.GLOBAL) { + lexer.nextToken(); + global = true; + } + } + switch (lexer.token) { case LEFT: lexer.nextToken(); @@ -1414,6 +1422,7 @@ public class SQLSelectParser extends SQLParser { SQLJoinTableSource join = new SQLJoinTableSource(); join.setLeft(tableSource); join.setJoinType(joinType); + join.setGlobal(global); if (asof) { join.setAsof(true); } diff --git a/src/main/java/com/alibaba/druid/sql/parser/SQLStatementParser.java b/src/main/java/com/alibaba/druid/sql/parser/SQLStatementParser.java index c243be3e06..fd4749ea8c 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/SQLStatementParser.java +++ b/src/main/java/com/alibaba/druid/sql/parser/SQLStatementParser.java @@ -4348,6 +4348,23 @@ public class SQLStatementParser extends SQLParser { createView.setName(exprParser.name()); + if (dbType == DbType.clickhouse) { + if (lexer.token == Token.ON) { + lexer.nextToken(); + acceptIdentifier("CLUSTER"); + createView.setOnCluster(true); + } + + if (lexer.token == LITERAL_CHARS) { + SQLName to = this.exprParser.name(); + createView.setTo(to); + } else if (lexer.token == TO) { + lexer.nextToken(); + SQLName to = this.exprParser.name(); + createView.setTo(to); + } + } + if (lexer.token == Token.LPAREN) { lexer.nextToken(); diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java index 827614c2b9..63f7a66294 100644 --- a/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java @@ -3989,9 +3989,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet print('('); } + JoinType joinType = x.getJoinType(); if (left instanceof SQLJoinTableSource && ((SQLJoinTableSource) left).getJoinType() == JoinType.COMMA - && x.getJoinType() != JoinType.COMMA + && joinType != JoinType.COMMA && DbType.postgresql != dbType && ((SQLJoinTableSource) left).getAttribute("ads.comma_join") == null) { print('('); @@ -4002,7 +4003,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet } this.indentCount++; - if (x.getJoinType() == JoinType.COMMA) { + if (joinType == JoinType.COMMA) { print(','); } else { println(); @@ -4016,7 +4017,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet print0(ucase ? "NATURAL " : "natural "); } - printJoinType(x.getJoinType()); + if (x.isGlobal()) { + print0(ucase ? "GLOBAL " : "global "); + } + + printJoinType(joinType); } print(' '); @@ -4060,7 +4065,6 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet print(')'); } - if (alias != null) { print0(ucase ? ") AS " : ") as "); print0(alias); @@ -4966,6 +4970,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet x.getTableSource().accept(this); + if (x.isOnCluster()) { + print0(ucase ? " ON CLUSTER" : " on cluster"); + } + + SQLName to = x.getTo(); + if (to != null) { + print0(ucase ? " TO " : " to "); + to.accept(this); + } + List columns = x.getColumns(); if (columns.size() > 0) { print0(" (");