Skip to content

Commit f2ed1fb

Browse files
authored
[Fix][Connector-V2] Fixed doris/starrocks create table sql parse error (#6580)
1 parent 2086b0e commit f2ed1fb

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public static Map<String, ColumnInfo> getColumnList(String createTableSql) {
4343
} else if ((c == ',' || c == ')') && !insideParentheses) {
4444
parseColumn(columnBuilder.toString(), columns, startIndex + i + 1);
4545
columnBuilder.setLength(0);
46+
if (c == ')') {
47+
break;
48+
}
4649
} else if (c == ')') {
4750
insideParentheses = false;
4851
columnBuilder.append(c);

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,45 @@ public void testWithVarchar() {
305305
+ " \"dynamic_partition.prefix\" = \"p\" \n"
306306
+ ");");
307307
}
308+
309+
@Test
310+
public void testWithThreePrimaryKeys() {
311+
List<Column> columns = new ArrayList<>();
312+
313+
columns.add(PhysicalColumn.of("id", BasicType.LONG_TYPE, (Long) null, true, null, ""));
314+
columns.add(PhysicalColumn.of("name", BasicType.STRING_TYPE, (Long) null, true, null, ""));
315+
columns.add(PhysicalColumn.of("age", BasicType.INT_TYPE, (Long) null, true, null, ""));
316+
columns.add(PhysicalColumn.of("comment", BasicType.STRING_TYPE, 500, true, null, ""));
317+
columns.add(PhysicalColumn.of("description", BasicType.STRING_TYPE, 70000, true, null, ""));
318+
319+
String result =
320+
DorisCatalogUtil.getCreateTableStatement(
321+
"create table '${database}'.'${table_name}'(\n"
322+
+ " ${rowtype_fields}\n"
323+
+ " )\n"
324+
+ " partitioned by ${rowtype_primary_key};",
325+
TablePath.of("test1", "test2"),
326+
CatalogTable.of(
327+
TableIdentifier.of("test", "test1", "test2"),
328+
TableSchema.builder()
329+
.primaryKey(
330+
PrimaryKey.of(
331+
"test", Arrays.asList("id", "age", "name")))
332+
.columns(columns)
333+
.build(),
334+
Collections.emptyMap(),
335+
Collections.emptyList(),
336+
""));
337+
338+
Assertions.assertEquals(
339+
"create table 'test1'.'test2'(\n"
340+
+ " `id` BIGINT(1) NULL ,\n"
341+
+ "`name` STRING NULL ,\n"
342+
+ "`age` INT(1) NULL ,\n"
343+
+ "`comment` VARCHAR(500) NULL ,\n"
344+
+ "`description` STRING NULL \n"
345+
+ " )\n"
346+
+ " partitioned by `id`,`age`,`name`;",
347+
result);
348+
}
308349
}

seatunnel-connectors-v2/connector-starrocks/src/main/java/org/apache/seatunnel/connectors/seatunnel/starrocks/util/CreateTableParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public static Map<String, ColumnInfo> getColumnList(String createTableSql) {
4343
} else if ((c == ',' || c == ')') && !insideParentheses) {
4444
parseColumn(columnBuilder.toString(), columns, startIndex + i + 1);
4545
columnBuilder.setLength(0);
46+
if (c == ')') {
47+
break;
48+
}
4649
} else if (c == ')') {
4750
insideParentheses = false;
4851
columnBuilder.append(c);

seatunnel-connectors-v2/connector-starrocks/src/test/java/org/apache/seatunnel/connectors/seatunnel/starrocks/catalog/StarRocksCreateTableTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,40 @@ public void testWithVarchar() {
289289
+ ");",
290290
result);
291291
}
292+
293+
@Test
294+
public void testWithThreePrimaryKeys() {
295+
List<Column> columns = new ArrayList<>();
296+
297+
columns.add(PhysicalColumn.of("id", BasicType.LONG_TYPE, (Long) null, true, null, ""));
298+
columns.add(PhysicalColumn.of("name", BasicType.STRING_TYPE, (Long) null, true, null, ""));
299+
columns.add(PhysicalColumn.of("age", BasicType.INT_TYPE, (Long) null, true, null, ""));
300+
columns.add(PhysicalColumn.of("comment", BasicType.STRING_TYPE, 500, true, null, ""));
301+
columns.add(PhysicalColumn.of("description", BasicType.STRING_TYPE, 70000, true, null, ""));
302+
303+
String result =
304+
StarRocksSaveModeUtil.getCreateTableSql(
305+
"create table '${database}'.'${table_name}'(\n"
306+
+ " ${rowtype_fields}\n"
307+
+ " )\n"
308+
+ " partitioned by ${rowtype_primary_key};",
309+
"test1",
310+
"test2",
311+
TableSchema.builder()
312+
.primaryKey(
313+
PrimaryKey.of("test", Arrays.asList("id", "age", "name")))
314+
.columns(columns)
315+
.build());
316+
317+
Assertions.assertEquals(
318+
"create table 'test1'.'test2'(\n"
319+
+ " `id` BIGINT NULL ,\n"
320+
+ "`name` STRING NULL ,\n"
321+
+ "`age` INT NULL ,\n"
322+
+ "`comment` VARCHAR(500) NULL ,\n"
323+
+ "`description` STRING NULL \n"
324+
+ " )\n"
325+
+ " partitioned by `id`,`age`,`name`;",
326+
result);
327+
}
292328
}

0 commit comments

Comments
 (0)