Skip to content

Commit 464da8f

Browse files
authored
[Fix][Connector-V2] Fix doris primary key order and fields order are inconsistent (#7377)
1 parent 527c7c7 commit 464da8f

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

seatunnel-connectors-v2/connector-doris/src/main/java/org/apache/seatunnel/connectors/doris/util/DorisCatalogUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ public static String getCreateTableStatement(
117117

118118
String primaryKey = "";
119119
if (tableSchema.getPrimaryKey() != null) {
120-
primaryKey =
121-
tableSchema.getPrimaryKey().getColumnNames().stream()
122-
.map(r -> "`" + r + "`")
123-
.collect(Collectors.joining(","));
120+
List<String> fields = Arrays.asList(catalogTable.getTableSchema().getFieldNames());
121+
List<String> keys = tableSchema.getPrimaryKey().getColumnNames();
122+
keys.sort(Comparator.comparingInt(fields::indexOf));
123+
primaryKey = keys.stream().map(r -> "`" + r + "`").collect(Collectors.joining(","));
124124
}
125125
String uniqueKey = "";
126126
if (!tableSchema.getConstraintKeys().isEmpty()) {

seatunnel-connectors-v2/connector-doris/src/test/java/org/apache/seatunnel/connectors/doris/catalog/DorisCreateTableTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException;
3434
import org.apache.seatunnel.connectors.doris.config.DorisOptions;
3535
import org.apache.seatunnel.connectors.doris.datatype.DorisTypeConverterV1;
36+
import org.apache.seatunnel.connectors.doris.datatype.DorisTypeConverterV2;
3637
import org.apache.seatunnel.connectors.doris.util.DorisCatalogUtil;
3738

3839
import org.apache.commons.lang3.StringUtils;
@@ -389,7 +390,45 @@ public void testWithThreePrimaryKeys() {
389390
+ "`comment` VARCHAR(500) NULL ,\n"
390391
+ "`description` STRING NULL \n"
391392
+ " )\n"
392-
+ " partitioned by `id`,`age`,`name`;",
393+
+ " partitioned by `id`,`name`,`age`;",
394+
result);
395+
}
396+
397+
@Test
398+
public void testWithResortedMultiPrimaryKey() {
399+
List<Column> columns = new ArrayList<>();
400+
401+
columns.add(PhysicalColumn.of("id", BasicType.LONG_TYPE, (Long) null, true, null, ""));
402+
columns.add(PhysicalColumn.of("name", BasicType.STRING_TYPE, (Long) null, true, null, ""));
403+
columns.add(PhysicalColumn.of("age", BasicType.INT_TYPE, (Long) null, true, null, ""));
404+
405+
String result =
406+
DorisCatalogUtil.getCreateTableStatement(
407+
DorisOptions.SAVE_MODE_CREATE_TEMPLATE.defaultValue(),
408+
TablePath.of("test1", "test2"),
409+
CatalogTable.of(
410+
TableIdentifier.of("test", "test1", "test2"),
411+
TableSchema.builder()
412+
.primaryKey(PrimaryKey.of("", Arrays.asList("age", "id")))
413+
.columns(columns)
414+
.build(),
415+
Collections.emptyMap(),
416+
Collections.emptyList(),
417+
""),
418+
DorisTypeConverterV2.INSTANCE);
419+
Assertions.assertEquals(
420+
"CREATE TABLE IF NOT EXISTS `test1`.`test2` (\n"
421+
+ "`id` BIGINT NULL ,`age` INT NULL ,\n"
422+
+ "`name` STRING NULL \n"
423+
+ ") ENGINE=OLAP\n"
424+
+ " UNIQUE KEY (`id`,`age`)\n"
425+
+ "DISTRIBUTED BY HASH (`id`,`age`)\n"
426+
+ " PROPERTIES (\n"
427+
+ "\"replication_allocation\" = \"tag.location.default: 1\",\n"
428+
+ "\"in_memory\" = \"false\",\n"
429+
+ "\"storage_format\" = \"V2\",\n"
430+
+ "\"disable_auto_compaction\" = \"false\"\n"
431+
+ ")",
393432
result);
394433
}
395434
}

0 commit comments

Comments
 (0)