support clickhouse nested datatype/create table engine, fix #4106

This commit is contained in:
高铁
2020-12-30 15:01:52 +08:00
parent c22f0f0bfd
commit 5761dccf90
7 changed files with 123 additions and 0 deletions
@@ -72,6 +72,7 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
protected boolean replace = false;
protected boolean ignore = false;
protected boolean dimension;
protected SQLExpr engine;
public SQLCreateTableStatement(){
@@ -1534,4 +1535,15 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
return false;
}
public SQLExpr getEngine() {
return engine;
}
public void setEngine(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.engine = x;
}
}
@@ -1,6 +1,10 @@
package com.alibaba.druid.sql.dialect.clickhouse.visitor;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLRowDataType;
import com.alibaba.druid.sql.ast.SQLStructDataType;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
@@ -40,4 +44,31 @@ public class ClickhouseOutputVisitor extends SQLASTOutputVisitor implements Clic
return false;
}
public boolean visit(SQLStructDataType x) {
print0(ucase ? "NESTED (" : "nested (");
incrementIndent();
println();
printlnAndAccept(x.getFields(), ",");
decrementIndent();
println();
print(')');
return false;
}
@Override
public boolean visit(SQLStructDataType.Field x) {
SQLName name = x.getName();
if (name != null) {
name.accept(this);
}
SQLDataType dataType = x.getDataType();
if (dataType != null) {
print(' ');
dataType.accept(this);
}
return false;
}
}
@@ -182,6 +182,16 @@ public class SQLCreateTableParser extends SQLDDLParser {
createTable.setPartitioning(partitionClause);
}
if (dbType == DbType.clickhouse) {
if (lexer.identifierEquals(FnvHash.Constants.ENGINE)) {
lexer.nextToken();
accept(Token.EQ);
createTable.setEngine(
this.exprParser.expr()
);
}
}
return createTable;
}
@@ -3675,6 +3675,46 @@ public class SQLExprParser extends SQLParser {
} else if (lexer.identifierEquals(FnvHash.Constants.ROW) || lexer.token == Token.ROW) {
lexer.nextToken();
return parseSqlRowDataType();
} else if (lexer.identifierEquals(FnvHash.Constants.NESTED) && dbType == DbType.clickhouse) {
lexer.nextToken();
accept(Token.LPAREN);
SQLStructDataType struct = new SQLStructDataType(dbType);
for (;;) {
SQLName name;
switch (lexer.token) {
case GROUP:
case ORDER:
case FROM:
case TO:
name = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
break;
default:
name = this.name();
break;
}
SQLDataType dataType = this.parseDataType();
SQLStructDataType.Field field = struct.addField(name, dataType);
if (lexer.token == Token.COMMENT) {
lexer.nextToken();
SQLCharExpr chars = (SQLCharExpr) this.primary();
field.setComment(chars.getText());
}
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
return struct;
}
if (lexer.identifierEquals(FnvHash.Constants.UNIONTYPE)) {
@@ -3526,6 +3526,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
tablespace.accept(this);
}
SQLExpr engine = x.getEngine();
if (engine != null) {
print0(ucase ? " ENGINE = " : " engine = ");
engine.accept(this);
}
return false;
}