Skip to content

Commit 5d888e9

Browse files
yzeng1618zengyi
andauthored
[Bug][connector-jdbc] Fix integer overflow when merging table schema with large column length (#10387)
Co-authored-by: zengyi <zengyi@chinatelecom.cn>
1 parent d36ba85 commit 5d888e9

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/utils/JdbcCatalogUtils.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,23 +332,22 @@ static CatalogTable mergeCatalogTable(CatalogTable tableOfPath, CatalogTable tab
332332
.get(column.getName())
333333
.getDataType()
334334
.getSqlType())
335-
? PhysicalColumn.of(
335+
? new PhysicalColumn(
336336
column.getName(),
337337
column.getDataType(),
338-
column.getColumnLength() == null
339-
? null
340-
: Math.toIntExact(
341-
column.getColumnLength()),
338+
column.getColumnLength(),
339+
column.getScale(),
342340
column.isNullable(),
343341
column.getDefaultValue(),
344342
columnsOfPath
345343
.get(column.getName())
346344
.getComment(),
347345
column.getSourceType(),
346+
column.getSinkType(),
347+
column.getOptions(),
348348
column.isUnsigned(),
349349
column.isZeroFill(),
350350
column.getBitLen(),
351-
column.getOptions(),
352351
column.getLongColumnLength())
353352
: column;
354353
})

seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/utils/JdbcCatalogUtilsTest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,94 @@ public void testColumnNotIncludeMerge() {
335335
mergeTable.getTableSchema().getColumns());
336336
}
337337

338+
@Test
339+
public void testColumnNotIncludeMergeWithLargeColumnLength() {
340+
long largeLength = 4294967295L;
341+
342+
CatalogTable tableOfPath =
343+
CatalogTable.of(
344+
TableIdentifier.of("mysql-1", "database-x", null, "table-x"),
345+
TableSchema.builder()
346+
.column(
347+
PhysicalColumn.of(
348+
"id",
349+
BasicType.LONG_TYPE,
350+
(Long) null,
351+
false,
352+
null,
353+
"id comment"))
354+
.column(
355+
PhysicalColumn.of(
356+
"config",
357+
BasicType.STRING_TYPE,
358+
largeLength,
359+
false,
360+
null,
361+
"config comment"))
362+
.build(),
363+
Collections.emptyMap(),
364+
Collections.emptyList(),
365+
null);
366+
367+
CatalogTable tableOfQuery =
368+
CatalogTable.of(
369+
TableIdentifier.of("default", null, null, "default"),
370+
TableSchema.builder()
371+
.column(
372+
PhysicalColumn.of(
373+
"id",
374+
BasicType.LONG_TYPE,
375+
(Long) null,
376+
true,
377+
null,
378+
null))
379+
.column(
380+
PhysicalColumn.of(
381+
"config",
382+
BasicType.STRING_TYPE,
383+
largeLength,
384+
true,
385+
null,
386+
null))
387+
.column(
388+
PhysicalColumn.of(
389+
"dummy",
390+
BasicType.INT_TYPE,
391+
(Long) null,
392+
true,
393+
null,
394+
null))
395+
.build(),
396+
Collections.emptyMap(),
397+
Collections.emptyList(),
398+
null);
399+
400+
CatalogTable mergeTable = JdbcCatalogUtils.mergeCatalogTable(tableOfPath, tableOfQuery);
401+
402+
Assertions.assertEquals(
403+
tableOfPath.getTableId().toTablePath(), mergeTable.getTableId().toTablePath());
404+
Assertions.assertEquals(
405+
tableOfQuery.getTableId().getCatalogName(),
406+
mergeTable.getTableId().getCatalogName());
407+
408+
Map<String, Column> mergedColumns =
409+
mergeTable.getTableSchema().getColumns().stream()
410+
.collect(Collectors.toMap(e -> e.getName(), e -> e));
411+
412+
Column mergedId = mergedColumns.get("id");
413+
Column mergedConfig = mergedColumns.get("config");
414+
415+
Assertions.assertNotNull(mergedId);
416+
Assertions.assertNotNull(mergedConfig);
417+
418+
// The merge should use the query column as base, and fill comment from the table_path.
419+
Assertions.assertTrue(mergedId.isNullable());
420+
Assertions.assertEquals("id comment", mergedId.getComment());
421+
422+
Assertions.assertEquals(Long.valueOf(largeLength), mergedConfig.getColumnLength());
423+
Assertions.assertEquals("config comment", mergedConfig.getComment());
424+
}
425+
338426
@Test
339427
public void testDecimalColumnMerge() {
340428
CatalogTable tableOfQuery =

0 commit comments

Comments
 (0)