use of com.qlangtech.tis.sql.parser.visitor.BlockScriptBuffer in project plugins by qlangtech.
the class BasicDorisStarRocksWriter method generateCreateDDL.
@Override
public StringBuffer generateCreateDDL(IDataxProcessor.TableMap tableMapper) {
if (!this.autoCreateTable) {
return null;
}
// https://doris.apache.org/master/zh-CN/sql-reference/sql-statements/Data%20Definition/CREATE%20TABLE.html#create-table
final CreateTableSqlBuilder createTableSqlBuilder = new CreateTableSqlBuilder(tableMapper) {
@Override
protected void appendExtraColDef(List<ColWrapper> pks) {
// if (pk != null) {
// script.append(" PRIMARY KEY (`").append(pk.getName()).append("`)").append("\n");
// }
}
@Override
protected List<ColWrapper> preProcessCols(List<ColWrapper> pks, List<ISelectedTab.ColMeta> cols) {
// 将主键排在最前面
List<ColWrapper> result = Lists.newArrayList(pks);
cols.stream().filter((c) -> !c.isPk()).forEach((c) -> {
result.add(createColWrapper(c));
});
return result;
}
@Override
protected void appendTabMeta(List<ColWrapper> pks) {
script.append(" ENGINE=olap").append("\n");
if (pks.size() > 0) {
script.append("PRIMARY KEY(").append(pks.stream().map((pk) -> this.colEscapeChar() + pk.getName() + this.colEscapeChar()).collect(Collectors.joining(","))).append(")\n");
}
script.append("DISTRIBUTED BY HASH(");
if (pks.size() > 0) {
script.append(pks.stream().map((pk) -> this.colEscapeChar() + pk.getName() + this.colEscapeChar()).collect(Collectors.joining(",")));
} else {
List<ISelectedTab.ColMeta> cols = this.getCols();
Optional<ISelectedTab.ColMeta> firstCol = cols.stream().findFirst();
if (firstCol.isPresent()) {
script.append(firstCol.get().getName());
} else {
throw new IllegalStateException("can not find table:" + getCreateTableName() + " any cols");
}
}
script.append(")\n");
script.append("BUCKETS 10\n");
script.append("PROPERTIES(\"replication_num\" = \"1\")");
}
@Override
protected ColWrapper createColWrapper(ISelectedTab.ColMeta c) {
return new ColWrapper(c) {
@Override
public String getMapperType() {
return convertType(this.meta);
}
@Override
protected void appendExtraConstraint(BlockScriptBuffer ddlScript) {
if (this.meta.isPk()) {
ddlScript.append(" NOT NULL");
}
}
};
}
protected String convertType(ISelectedTab.ColMeta col) {
DataType type = col.getType();
return type.accept(new DataType.TypeVisitor<String>() {
@Override
public String longType(DataType type) {
return "BIGINT";
}
@Override
public String doubleType(DataType type) {
return "DOUBLE";
}
@Override
public String dateType(DataType type) {
return "DATE";
}
@Override
public String timestampType(DataType type) {
return "DATETIME";
}
@Override
public String bitType(DataType type) {
return "TINYINT";
}
@Override
public String blobType(DataType type) {
return varcharType(type);
}
@Override
public String varcharType(DataType type) {
return "VARCHAR(" + Math.min(type.columnSize, 65000) + ")";
}
@Override
public String intType(DataType type) {
return "INT";
}
@Override
public String floatType(DataType type) {
return "FLOAT";
}
@Override
public String decimalType(DataType type) {
return "DECIMAL(" + type.columnSize + "," + (type.getDecimalDigits() != null ? type.getDecimalDigits() : 0) + ")";
}
});
}
};
return createTableSqlBuilder.build();
}
Aggregations