Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve]Improve the way of dorisSystem create table #452

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
package org.apache.doris.flink.catalog.doris;

import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.util.Preconditions;
import org.apache.flink.util.StringUtils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.doris.flink.exception.CreateTableException;
import org.apache.doris.flink.tools.cdc.DorisTableConfig;

import java.util.ArrayList;
Expand All @@ -30,6 +33,7 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Factory that creates doris schema.
Expand Down Expand Up @@ -103,4 +107,132 @@ public static Integer parseTableSchemaBuckets(
}
return null;
}

public static String generateCreateTableDDL(TableSchema schema) {
StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
sb.append(identifier(schema.getDatabase()))
.append(".")
.append(identifier(schema.getTable()))
.append("(");

Map<String, FieldSchema> fields = schema.getFields();
List<String> keys = schema.getKeys();
// append keys
for (String key : keys) {
if (!fields.containsKey(key)) {
throw new CreateTableException("key " + key + " not found in column list");
}
FieldSchema field = fields.get(key);
buildColumn(sb, field, true);
}

// append values
for (Map.Entry<String, FieldSchema> entry : fields.entrySet()) {
if (keys.contains(entry.getKey())) {
continue;
}
FieldSchema field = entry.getValue();
buildColumn(sb, field, false);
}
sb = sb.deleteCharAt(sb.length() - 1);
sb.append(" ) ");
// append uniq model
if (DataModel.UNIQUE.equals(schema.getModel())) {
sb.append(schema.getModel().name())
.append(" KEY(")
.append(String.join(",", identifier(schema.getKeys())))
.append(")");
}

// append table comment
if (!StringUtils.isNullOrWhitespaceOnly(schema.getTableComment())) {
sb.append(" COMMENT '").append(quoteComment(schema.getTableComment())).append("' ");
}

// append distribute key
sb.append(" DISTRIBUTED BY HASH(")
.append(String.join(",", identifier(schema.getDistributeKeys())))
.append(")");

Map<String, String> properties = schema.getProperties();
if (schema.getTableBuckets() != null) {

int bucketsNum = schema.getTableBuckets();
if (bucketsNum <= 0) {
throw new CreateTableException("The number of buckets must be positive.");
}
sb.append(" BUCKETS ").append(bucketsNum);
} else {
sb.append(" BUCKETS AUTO ");
}
// append properties
int index = 0;
for (Map.Entry<String, String> entry : properties.entrySet()) {
if (index == 0) {
sb.append(" PROPERTIES (");
}
if (index > 0) {
sb.append(",");
}
sb.append(quoteProperties(entry.getKey()))
.append("=")
.append(quoteProperties(entry.getValue()));
index++;

if (index == (schema.getProperties().size())) {
sb.append(")");
}
}
return sb.toString();
}

private static void buildColumn(StringBuilder sql, FieldSchema field, boolean isKey) {
String fieldType = field.getTypeString();
if (isKey && DorisType.STRING.equals(fieldType)) {
fieldType = String.format("%s(%s)", DorisType.VARCHAR, 65533);
}
sql.append(identifier(field.getName())).append(" ").append(fieldType);

if (field.getDefaultValue() != null) {
sql.append(" DEFAULT " + quoteDefaultValue(field.getDefaultValue()));
}
sql.append(" COMMENT '").append(quoteComment(field.getComment())).append("',");
}

private static String quoteProperties(String name) {
return "'" + name + "'";
}

private static List<String> identifier(List<String> names) {
return names.stream().map(DorisSchemaFactory::identifier).collect(Collectors.toList());
}

public static String identifier(String name) {
if (name.startsWith("`") && name.endsWith("`")) {
return name;
}
return "`" + name + "`";
}

public static String quoteDefaultValue(String defaultValue) {
// DEFAULT current_timestamp not need quote
if (defaultValue.equalsIgnoreCase("current_timestamp")) {
return defaultValue;
}
return "'" + defaultValue + "'";
}

public static String quoteComment(String comment) {
if (comment == null) {
return "";
} else {
return comment.replaceAll("'", "\\\\'");
}
}

public static String quoteTableIdentifier(String tableIdentifier) {
String[] dbTable = tableIdentifier.split("\\.");
Preconditions.checkArgument(dbTable.length == 2);
return identifier(dbTable[0]) + "." + identifier(dbTable[1]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
package org.apache.doris.flink.catalog.doris;

import org.apache.flink.annotation.Public;
import org.apache.flink.util.Preconditions;
import org.apache.flink.util.StringUtils;

import org.apache.commons.compress.utils.Lists;
import org.apache.doris.flink.cfg.DorisConnectionOptions;
import org.apache.doris.flink.connection.JdbcConnectionProvider;
import org.apache.doris.flink.connection.SimpleJdbcConnectionProvider;
import org.apache.doris.flink.exception.CreateTableException;
import org.apache.doris.flink.exception.DorisRuntimeException;
import org.apache.doris.flink.exception.DorisSystemException;
import org.slf4j.Logger;
Expand All @@ -41,7 +39,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.apache.flink.util.Preconditions.checkArgument;

Expand Down Expand Up @@ -141,81 +138,7 @@ public List<String> extractColumnValuesBySQL(
}

public static String buildCreateTableDDL(TableSchema schema) {
StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
sb.append(identifier(schema.getDatabase()))
.append(".")
.append(identifier(schema.getTable()))
.append("(");

Map<String, FieldSchema> fields = schema.getFields();
List<String> keys = schema.getKeys();
// append keys
for (String key : keys) {
if (!fields.containsKey(key)) {
throw new CreateTableException("key " + key + " not found in column list");
}
FieldSchema field = fields.get(key);
buildColumn(sb, field, true);
}

// append values
for (Map.Entry<String, FieldSchema> entry : fields.entrySet()) {
if (keys.contains(entry.getKey())) {
continue;
}
FieldSchema field = entry.getValue();
buildColumn(sb, field, false);
}
sb = sb.deleteCharAt(sb.length() - 1);
sb.append(" ) ");
// append uniq model
if (DataModel.UNIQUE.equals(schema.getModel())) {
sb.append(schema.getModel().name())
.append(" KEY(")
.append(String.join(",", identifier(schema.getKeys())))
.append(")");
}

// append table comment
if (!StringUtils.isNullOrWhitespaceOnly(schema.getTableComment())) {
sb.append(" COMMENT '").append(quoteComment(schema.getTableComment())).append("' ");
}

// append distribute key
sb.append(" DISTRIBUTED BY HASH(")
.append(String.join(",", identifier(schema.getDistributeKeys())))
.append(")");

Map<String, String> properties = schema.getProperties();
if (schema.getTableBuckets() != null) {

int bucketsNum = schema.getTableBuckets();
if (bucketsNum <= 0) {
throw new CreateTableException("The number of buckets must be positive.");
}
sb.append(" BUCKETS ").append(bucketsNum);
} else {
sb.append(" BUCKETS AUTO ");
}
// append properties
int index = 0;
for (Map.Entry<String, String> entry : properties.entrySet()) {
if (index == 0) {
sb.append(" PROPERTIES (");
}
if (index > 0) {
sb.append(",");
}
sb.append(quoteProperties(entry.getKey()))
.append("=")
.append(quoteProperties(entry.getValue()));
index++;

if (index == (schema.getProperties().size())) {
sb.append(")");
}
}
return sb.toString();
return DorisSchemaFactory.generateCreateTableDDL(schema);
}

public Map<String, String> getTableFieldNames(String databaseName, String tableName) {
Expand Down Expand Up @@ -244,53 +167,23 @@ public Map<String, String> getTableFieldNames(String databaseName, String tableN
}
}

private static void buildColumn(StringBuilder sql, FieldSchema field, boolean isKey) {
String fieldType = field.getTypeString();
if (isKey && DorisType.STRING.equals(fieldType)) {
fieldType = String.format("%s(%s)", DorisType.VARCHAR, 65533);
}
sql.append(identifier(field.getName())).append(" ").append(fieldType);

if (field.getDefaultValue() != null) {
sql.append(" DEFAULT " + quoteDefaultValue(field.getDefaultValue()));
}
sql.append(" COMMENT '").append(quoteComment(field.getComment())).append("',");
}

@Deprecated
public static String quoteDefaultValue(String defaultValue) {
// DEFAULT current_timestamp not need quote
if (defaultValue.equalsIgnoreCase("current_timestamp")) {
return defaultValue;
}
return "'" + defaultValue + "'";
return DorisSchemaFactory.quoteDefaultValue(defaultValue);
}

@Deprecated
public static String quoteComment(String comment) {
if (comment == null) {
return "";
} else {
return comment.replaceAll("'", "\\\\'");
}
}

private static List<String> identifier(List<String> name) {
return name.stream().map(DorisSystem::identifier).collect(Collectors.toList());
return DorisSchemaFactory.quoteComment(comment);
}

@Deprecated
public static String identifier(String name) {
if (name.startsWith("`") && name.endsWith("`")) {
return name;
}
return "`" + name + "`";
return DorisSchemaFactory.identifier(name);
}

@Deprecated
public static String quoteTableIdentifier(String tableIdentifier) {
String[] dbTable = tableIdentifier.split("\\.");
Preconditions.checkArgument(dbTable.length == 2);
return identifier(dbTable[0]) + "." + identifier(dbTable[1]);
}

private static String quoteProperties(String name) {
return "'" + name + "'";
return DorisSchemaFactory.quoteTableIdentifier(tableIdentifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.doris.flink.sink.copy;

import org.apache.doris.flink.catalog.doris.DorisSystem;
import org.apache.doris.flink.catalog.doris.DorisSchemaFactory;
import org.apache.doris.flink.cfg.DorisExecutionOptions;

import java.util.Arrays;
Expand Down Expand Up @@ -53,7 +53,7 @@ public CopySQLBuilder(
public String buildCopySQL() {
StringBuilder sb = new StringBuilder();
sb.append("COPY INTO ")
.append(DorisSystem.quoteTableIdentifier(tableIdentifier))
.append(DorisSchemaFactory.quoteTableIdentifier(tableIdentifier))
.append(" FROM @~('{")
.append(String.join(",", fileList))
.append("}') ")
Expand Down
Loading
Loading