Skip to content

Commit

Permalink
[Improve][Jdbc] Optimize index name conflicts when create table for p…
Browse files Browse the repository at this point in the history
…ostgresql (#7875)
  • Loading branch information
hailin0 authored Oct 21, 2024
1 parent 266d177 commit 312ee86
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import org.apache.commons.lang3.StringUtils;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
public class PostgresCreateTableSqlBuilder {
private List<Column> columns;
private PrimaryKey primaryKey;
Expand Down Expand Up @@ -161,10 +164,7 @@ private String buildColumnCommentSql(Column column, String tableName) {
}

private String buildUniqueKeySql(ConstraintKey constraintKey) {
String constraintName = constraintKey.getConstraintName();
if (constraintName.length() > 25) {
constraintName = constraintName.substring(0, 25);
}
String constraintName = UUID.randomUUID().toString().replace("-", "");
String indexColumns =
constraintKey.getColumnNames().stream()
.map(
Expand All @@ -175,16 +175,12 @@ private String buildUniqueKeySql(ConstraintKey constraintKey) {
constraintKeyColumn.getColumnName(),
fieldIde)))
.collect(Collectors.joining(", "));
return "CONSTRAINT " + constraintName + " UNIQUE (" + indexColumns + ")";
return "CONSTRAINT \"" + constraintName + "\" UNIQUE (" + indexColumns + ")";
}

private String buildIndexKeySql(TablePath tablePath, ConstraintKey constraintKey) {
// We add table name to index name to avoid name conflict in PG
// Since index name in PG should unique in the schema
String constraintName = tablePath.getTableName() + "_" + constraintKey.getConstraintName();
if (constraintName.length() > 25) {
constraintName = constraintName.substring(0, 25);
}
// If the index name is omitted, PostgreSQL will choose an appropriate name based on table
// name and indexed columns.
String indexColumns =
constraintKey.getColumnNames().stream()
.map(
Expand All @@ -196,9 +192,7 @@ private String buildIndexKeySql(TablePath tablePath, ConstraintKey constraintKey
fieldIde)))
.collect(Collectors.joining(", "));

return "CREATE INDEX "
+ constraintName
+ " ON "
return "CREATE INDEX ON "
+ tablePath.getSchemaAndTableName("\"")
+ "("
+ indexColumns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

class PostgresCreateTableSqlBuilderTest {

Expand All @@ -49,17 +50,18 @@ void build() {
String createTableSql =
postgresCreateTableSqlBuilder.build(
catalogTable.getTableId().toTablePath());
Assertions.assertEquals(
"CREATE TABLE \"test\" (\n"
String pattern =
"CREATE TABLE \"test\" \\(\n"
+ "\"id\" int4 NOT NULL PRIMARY KEY,\n"
+ "\"name\" text NOT NULL,\n"
+ "\"age\" int4 NOT NULL,\n"
+ "\tCONSTRAINT unique_name UNIQUE (\"name\")\n"
+ ");",
createTableSql);
+ "\tCONSTRAINT \"([a-zA-Z0-9]+)\" UNIQUE \\(\"name\"\\)\n"
+ "\\);";
Assertions.assertTrue(
Pattern.compile(pattern).matcher(createTableSql).find());

Assertions.assertEquals(
Lists.newArrayList(
"CREATE INDEX test_index_age ON \"test\"(\"age\");"),
Lists.newArrayList("CREATE INDEX ON \"test\"(\"age\");"),
postgresCreateTableSqlBuilder.getCreateIndexSqls());

// skip index
Expand Down

0 comments on commit 312ee86

Please sign in to comment.