Skip to content

Commit 312ee86

Browse files
authored
[Improve][Jdbc] Optimize index name conflicts when create table for postgresql (apache#7875)
1 parent 266d177 commit 312ee86

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/catalog/psql/PostgresCreateTableSqlBuilder.java

+8-14
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
import org.apache.commons.lang3.StringUtils;
3131

3232
import lombok.Getter;
33+
import lombok.extern.slf4j.Slf4j;
3334

3435
import java.util.ArrayList;
3536
import java.util.List;
37+
import java.util.UUID;
3638
import java.util.stream.Collectors;
3739

40+
@Slf4j
3841
public class PostgresCreateTableSqlBuilder {
3942
private List<Column> columns;
4043
private PrimaryKey primaryKey;
@@ -161,10 +164,7 @@ private String buildColumnCommentSql(Column column, String tableName) {
161164
}
162165

163166
private String buildUniqueKeySql(ConstraintKey constraintKey) {
164-
String constraintName = constraintKey.getConstraintName();
165-
if (constraintName.length() > 25) {
166-
constraintName = constraintName.substring(0, 25);
167-
}
167+
String constraintName = UUID.randomUUID().toString().replace("-", "");
168168
String indexColumns =
169169
constraintKey.getColumnNames().stream()
170170
.map(
@@ -175,16 +175,12 @@ private String buildUniqueKeySql(ConstraintKey constraintKey) {
175175
constraintKeyColumn.getColumnName(),
176176
fieldIde)))
177177
.collect(Collectors.joining(", "));
178-
return "CONSTRAINT " + constraintName + " UNIQUE (" + indexColumns + ")";
178+
return "CONSTRAINT \"" + constraintName + "\" UNIQUE (" + indexColumns + ")";
179179
}
180180

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

199-
return "CREATE INDEX "
200-
+ constraintName
201-
+ " ON "
195+
return "CREATE INDEX ON "
202196
+ tablePath.getSchemaAndTableName("\"")
203197
+ "("
204198
+ indexColumns

seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/catalog/psql/PostgresCreateTableSqlBuilderTest.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Arrays;
3636
import java.util.Collections;
3737
import java.util.List;
38+
import java.util.regex.Pattern;
3839

3940
class PostgresCreateTableSqlBuilderTest {
4041

@@ -49,17 +50,18 @@ void build() {
4950
String createTableSql =
5051
postgresCreateTableSqlBuilder.build(
5152
catalogTable.getTableId().toTablePath());
52-
Assertions.assertEquals(
53-
"CREATE TABLE \"test\" (\n"
53+
String pattern =
54+
"CREATE TABLE \"test\" \\(\n"
5455
+ "\"id\" int4 NOT NULL PRIMARY KEY,\n"
5556
+ "\"name\" text NOT NULL,\n"
5657
+ "\"age\" int4 NOT NULL,\n"
57-
+ "\tCONSTRAINT unique_name UNIQUE (\"name\")\n"
58-
+ ");",
59-
createTableSql);
58+
+ "\tCONSTRAINT \"([a-zA-Z0-9]+)\" UNIQUE \\(\"name\"\\)\n"
59+
+ "\\);";
60+
Assertions.assertTrue(
61+
Pattern.compile(pattern).matcher(createTableSql).find());
62+
6063
Assertions.assertEquals(
61-
Lists.newArrayList(
62-
"CREATE INDEX test_index_age ON \"test\"(\"age\");"),
64+
Lists.newArrayList("CREATE INDEX ON \"test\"(\"age\");"),
6365
postgresCreateTableSqlBuilder.getCreateIndexSqls());
6466

6567
// skip index

0 commit comments

Comments
 (0)