corgy-w opened a new pull request, #13368:
URL: https://github.com/apache/seatunnel/pull/13368
## What
`OceanBaseMySqlCatalog#getTable(String)` built the primary key of the
`CatalogTable` inside a loop over the `DatabaseMetaData#getPrimaryKeys` result
set, calling `schemaBuilder.primaryKey(...)` once per row:
```java
while (primaryKeys.next()) {
String primaryKeyColumnName = primaryKeys.getString("COLUMN_NAME");
schemaBuilder.primaryKey(
PrimaryKey.of(
primaryKeyColumnName,
Collections.singletonList(primaryKeyColumnName)));
}
```
Every call replaced the previous key and only carried a single column, so
for a composite primary key `(a, b)` the resulting `TableSchema` kept only the
last column. Downstream primary-key handling (upsert/delete routing, generated
DDL, PK checks) then silently worked with an incomplete key.
## Fix
- Collect all primary-key rows and sort them by `KEY_SEQ` before building
the key. `DatabaseMetaData#getPrimaryKeys` returns the rows ordered by
`COLUMN_NAME`, not by the order of the key (see the comment in
`CatalogUtils#getPrimaryKey`), so for a composite key `(a, b)` the rows may
come back as `(b, a)`; `KEY_SEQ` is 1-based and preserves the declared key
order.
- Call `schemaBuilder.primaryKey(...)` exactly once with all the columns.
- Use the metadata `PK_NAME`, cleaned with the shared
`CatalogUtils.cleanKeyName`, as the `PrimaryKey` name, falling back to the
first column name when `PK_NAME` is null/empty. To make that reuse possible
`CatalogUtils.cleanKeyName` is now public (visibility only, no behaviour
change).
- The logic is extracted into the package-private static helper
`buildPrimaryKey(ResultSet)`, so it can be tested without a live database.
`CatalogUtils.getPrimaryKey(DatabaseMetaData, TablePath)` was not reused on
purpose: it re-queries the metadata and it does not implement the required
`PK_NAME` fallback.
- No other behaviour of `getTable` changed (columns, options, comment).
## Tests
Added to the existing `OceanBaseMySqlCatalogTest` (Mockito `ResultSet`
mocks, no new test class):
1. composite key whose rows come back out of order (`b` with `KEY_SEQ` 2
first, then `a` with `KEY_SEQ` 1) -> the built `PrimaryKey` contains `[a, b]`
in `KEY_SEQ` order and is named `PRIMARY`.
2. single-column key -> still one column, named from `PK_NAME`.
3. `PK_NAME` null -> falls back to the first column name.
4. empty result set -> no primary key is set.
RED evidence (old loop restored inside the extracted helper): `Tests run: 5,
Failures: 3, Errors: 0`, e.g.
`shouldKeepEveryColumnOfCompositePrimaryKeyInKeySeqOrder: expected: <[a, b]>
but was: <[a]>`.
GREEN: `Tests run: 5, Failures: 0, Errors: 0, Skipped: 0`
```bash
./mvnw -o -q -pl seatunnel-connectors-v2/connector-jdbc spotless:apply
./mvnw -o -pl seatunnel-connectors-v2/connector-jdbc test
-Dtest=OceanBaseMySqlCatalogTest -DfailIfNoTests=false -Dskip.spotless=true
```
## Compatibility
None. The behaviour changes only where it was wrong (composite keys).
Single-column keys keep the same column and now carry the metadata key name
instead of the column name; `PrimaryKey#primaryKey` is documented as unused.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]