Skip to content

[Fix][Connector-V2][JDBC] Report the declared length of Dameng NVARCHAR and NVARCHAR2 - #12882

Merged
nzw921rx merged 1 commit into
apache:devfrom
zhengxiang378928908-code:fix/dameng-nvarchar-length
Oct 26, 2026
Merged

nzw921rx merged 1 commit into
apache:devfrom
zhengxiang378928908-code:fix/dameng-nvarchar-length

Conversation

@zhengxiang378928908-code

@zhengxiang378928908-code zhengxiang378928908-code commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Purpose of this pull request

Fixes #12752.

DamengCatalog took every column's length from ALL_TAB_COLUMNS.DATA_LENGTH. For NVARCHAR(n) and NVARCHAR2(n) that is a byte count, while the columns hold n characters, so NVARCHAR(10) was reported as NVARCHAR(20) (GB18030) or NVARCHAR(40) (UTF-8), and the length was then expanded again for SeaTunnel's column length.

Following @DanielLeens's review in #12752, this PR selects the metadata explicitly and does not divide DATA_LENGTH.

What Dameng actually stores. The definition of SYS.ALL_TAB_COLUMNS (from SYS.SYSTEXTS) is:

DATA_LENGTH = CASE ... WHEN COL.SCALE = 7 AND UNICODE() = 1 THEN COL.LENGTH$ * 4
                       WHEN COL.SCALE = 7 AND UNICODE() = 0 THEN COL.LENGTH$ * 2
                       ELSE COL.LENGTH$ END
CHAR_LENGTH = CASE WHEN COL.TYPE$ IN ('CHAR','CHARACTER','VARCHAR','VARCHAR2','NCHAR','NVARCHAR')
                   THEN COL.LENGTH$ ELSE 0 END      -- NVARCHAR2 is not in the list

SYS.SYSCOLUMNS.LENGTH$ is the declared length. DATA_LENGTH multiplies it by the charset's bytes per character for character-length columns (SCALE = 7), and CHAR_LENGTH is 0 for NVARCHAR2 only because the view leaves it out of the list. So DATA_LENGTH / 2 would be wrong on a UTF-8 database.

Change. SELECT_COLUMNS_SQL also selects ALL_TAB_COLUMNS.CHAR_LENGTH and SYS.SYSCOLUMNS.LENGTH$ (LEFT JOIN through SYS.SYSOBJECTS on owner, table and column name). For NVARCHAR and NVARCHAR2 only, the length is LENGTH$, then CHAR_LENGTH if LENGTH$ is missing, then DATA_LENGTH (the current behavior, which oversizes but never truncates). All other types keep DATA_LENGTH, so CHAR, CHARACTER, VARCHAR, VARCHAR2 and NCHAR are unchanged.

Measurements on DM V8 (0x7000c, the laglangyue/dmdb8 image used by JdbcDmIT), with four databases initialized by dminit with each LENGTH_IN_CHAR / CHARSET combination:

column LIC=0 GB18030 LIC=1 GB18030 LIC=0 UTF-8 LIC=1 UTF-8
NVARCHAR(10) DATA_LENGTH / CHAR_LENGTH / LENGTH$ 20 / 10 / 10 20 / 10 / 10 40 / 10 / 10 40 / 10 / 10
NVARCHAR2(10) DATA_LENGTH / CHAR_LENGTH / LENGTH$ 20 / 0 / 10 20 / 0 / 10 40 / 0 / 10 40 / 0 / 10
NVARCHAR(100), NVARCHAR2(100) 200 / 100 or 0 / 100 same 400 / 100 or 0 / 100 same
capacity of NVARCHAR(10) and NVARCHAR2(10) 10 × a ok, 11 × a rejected, 10 × 达 ok same same same
CHAR(10), VARCHAR(10), VARCHAR2(10), NCHAR(10) 10 / 10 / 10 10 / 10 / 10 10 / 10 / 10 10 / 10 / 10

So in every mode NVARCHAR(n) and NVARCHAR2(n) hold exactly n characters, and LENGTH$ is n. LENGTH_IN_CHAR does not change their metadata. A user that only has SELECT on the table (default PUBLIC role) can read SYS.SYSCOLUMNS and gets the same values.

Not changed here: VARCHAR(n CHAR) has the same pattern (DATA_LENGTH 2n/4n, LENGTH$ n). It keeps the current VARCHAR2(DATA_LENGTH) mapping, because changing it to VARCHAR2(n) would make a Dameng sink table byte-length and too small for multi-byte values. Fixing it properly means keeping the CHAR length semantics in the source type, so I'd suggest a separate issue if you think it is worth it.

Does this PR introduce any user-facing change?

Yes. For Dameng NVARCHAR(n) / NVARCHAR2(n), the source type changes from NVARCHAR(2n) / NVARCHAR(4n) to NVARCHAR(n), and the column length used for sink auto-create is halved (GB18030) or quartered (UTF-8). For example, NVARCHAR(10) gets VARCHAR(40) instead of VARCHAR(80) on a MySQL sink. The new columns still hold every source value, since the length is counted in characters. Existing tables are not altered. Recorded in incompatible-changes.md (en/zh).

How was this patch tested?

  • DamengCatalogTest (new): the measured matrix above for NVARCHAR / NVARCHAR2 (including CHAR_LENGTH = 0), the fallbacks when LENGTH$ or CHAR_LENGTH is missing, unchanged CHAR / CHARACTER / VARCHAR / VARCHAR2 / NCHAR / VARCHAR(10 CHAR), and buildColumn end to end with a mocked ResultSet (source type and column length).
  • JdbcDmIT#testNationalCharLengthFromCatalog (new): runs DamengCatalog#getTable against real Dameng:
    • the shared container (the image's default database: LENGTH_IN_CHAR=0, GB18030), as SYSDBA and as a user with only SELECT on the table;
    • a second container whose database is initialized with LENGTH_IN_CHAR=1 and CHARSET=1 (UTF-8);
    • the test asserts the mode of each database, the character capacity (10 Chinese characters fit, 11 × a is rejected), and the source type and length of NVARCHAR(10/100), NVARCHAR2(10/100), NCHAR, CHAR, VARCHAR, VARCHAR2.
  • Locally: DamengCatalogTest, DmdbTypeConverterTest and DamengCreateTableSqlBuilderTest pass (73 tests), and JdbcDmIT#testNationalCharLengthFromCatalog passes.

Check list

  • No new Jar binary package.
  • incompatible-changes.md updated (en/zh).
  • Not a new connector.

🤖 Generated with Claude Code

@DanielLeens DanielLeens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What Problem Does This PR Solve?

  • User pain point: Dameng NVARCHAR(n) and NVARCHAR2(n) were catalogued using byte length, making a character-length column appear two or four times wider and propagating an incorrect schema to downstream auto-created tables.
  • Fix approach: query the declared SYS.SYSCOLUMNS.LENGTH$ and use it for national variable-width character types, with safe fallbacks to CHAR_LENGTH and then the existing byte length.
  • One-sentence summary: the Dameng JDBC catalog now reports national character columns in their declared character unit.

1. Code Change Review

1.1 Core Logic Analysis

Reviewed head: 8a8c4b86ee4579ab8315490af2fa05cc5a922699. The runtime path is JDBC catalog metadata lookup -> DamengCatalog.getTable() -> SELECT_COLUMNS_SQL -> buildColumn() -> nationalCharLength() -> DmdbTypeConverter. The changed boundary is correct: the unit conversion belongs where Dameng metadata becomes a SeaTunnel catalog column, not in downstream sinks.

// before
long columnLength = resultSet.getLong("DATA_LENGTH");

// after
long columnLength = nationalCharLength(typeName,
        resultSet.getLong("DATA_LENGTH"), resultSet.getLong("CHAR_LENGTH"),
        resultSet.getLong("DECLARED_LENGTH"));

Key findings:

  • The normal catalog path reaches the conversion before source schema and sink auto-create consume the column definition.
  • Only NVARCHAR and NVARCHAR2 change; CHAR, VARCHAR, VARCHAR2, NCHAR, and binary types keep their prior byte-length behavior.
  • The declared-length fallback avoids guessing a charset conversion ratio when catalog metadata is incomplete.
  • This is a precise schema-correction fix and the intentional schema impact is documented in both languages.

Runtime path:

JDBC source discovers Dameng table
  -> DamengCatalog executes metadata query with SYS.SYSCOLUMNS join
  -> buildColumn reads declared national-character length
  -> DmdbTypeConverter builds SeaTunnel Column
  -> source schema and downstream auto-create use the corrected length

1.2 Compatibility Impact

Partially incompatible by design: newly discovered NVARCHAR/NVARCHAR2 column definitions can become narrower because they now reflect characters rather than bytes. Existing downstream tables are not altered. Both incompatible-change notes explain the impact and migration option.

1.3 Performance / Side-Effect Analysis

The metadata query adds catalog joins only during schema discovery. Per-row source processing, checkpointing, concurrency, retries, and resources are unchanged.

1.4 Error Handling and Logging

The fallback preserves the old byte length when declared metadata is unavailable, preferring oversizing over possible truncation. No sensitive information is logged. No source-level blocking issue was found.

2. Code Quality Assessment

2.1 Coding Standards

The national-character calculation has a detailed contract comment and the SQL join explains why CHAR_LENGTH alone is insufficient for NVARCHAR2.

2.2 Test Coverage and Test Stability

Parameterized unit coverage includes GB18030 and UTF-8, both national types, LENGTH_IN_CHAR variants, fallbacks, unaffected types, and buildColumn. The updated JdbcDmIT provides the database-level coverage. Stability rating: Stable. The added assertions use deterministic catalog data and do not add sleeps, fixed ports, or order-dependent state.

2.3 Documentation Updates

The English and Chinese incompatible-change entries accurately describe source-schema and auto-create impact.

3. Architectural Soundness

3.1 Elegance of the Solution

Precise fix at the catalog metadata boundary.

3.2 Maintainability

The helper makes the type-specific unit contract explicit and safely isolates it from unrelated types.

3.3 Extensibility

Additional Dameng metadata exceptions can be handled in the same catalog layer.

3.4 Historical-Version Compatibility

The documented schema correction is the only compatibility effect; existing sink tables remain untouched.

4. Issue Summary

Number Issue Location Severity
— No source-level blocking issues found — —

5. Merge Recommendation

Conclusion: Ready to merge after fixes

  1. Blockers — must be fixed

    • No source-level blocker was found. The current Build check is still in progress; wait for the JDBC/Dameng CI evidence before merge.
  2. Recommended fixes — non-blocking

    • None.

Overall assessment: the change corrects the type unit at the smallest safe and globally appropriate metadata boundary. Local analysis covered the complete diff, catalog call chain, converter behavior, unit matrix, E2E additions, and documentation; git diff --check passed. No local compilation or execution was performed under the SeaTunnel review policy.

@zhengxiang378928908-code

Copy link
Copy Markdown
Contributor Author

The red Build here is not from this change. All unit-test jobs (and edge-agent-it, which runs the same module) fail on one test, JdbcOptionRuleCompletenessTest.testSinkRuleDeclaresAllSinkOptions:

Connector 'Jdbc' has unknown option keys: [numeric_special_value_as_null]

That option came in with #12792, which is in this branch's base (e2957d27a). dev fails the same way; it's tracked in #12874, and #12875 fixes it. This PR doesn't touch JDBC options (fork run 39198018999).

Once #12875 is merged I'll rebase and post the new result.

@DanielLeens

Copy link
Copy Markdown
Contributor

Thanks for digging into the red Build, @zhengxiang378928908-code, and for linking the fork run. I checked it against the logs: in fork run 39198018999 (head 8a8c4b86) the failing jobs are the unit-test jobs (11 and 17, ubuntu and windows) plus edge-agent-it (11 and 17 on ubuntu), and the unit-test log shows JdbcOptionRuleCompletenessTest.testSinkRuleDeclaresAllSinkOptions failing with Connector 'Jdbc' has unknown option keys: [numeric_special_value_as_null]. That option was added by #12792 (merged 2026-10-18), the failure is tracked in #12874, and the fix is #12875, which is still open. This PR does not touch JDBC option definitions (it only changes DamengCatalog, its tests, JdbcDmIT and the incompatible-changes pages), so the failure is unrelated to the diff, and I agree with your plan.

Please sync the latest dev once #12875 is merged and re-run only the failed jobs; I will take another quick look at the CI result then. My review conclusion on the code itself is unchanged: no source-level blocker, and the remaining gate is a green Build on the final head.

@zhengxiang378928908-code

Copy link
Copy Markdown
Contributor Author

@SEZ9 Thanks. All three are covered in #12882:

  • NVARCHAR2 with CHAR_LENGTH = 0: every NVARCHAR2 row in DamengCatalogTest has CHAR_LENGTH = 0, and a separate case covers a missing LENGTH$ falling back to DATA_LENGTH.
  • Read-only user: JdbcDmIT#testNationalCharLengthFromCatalog creates E2E_DM_READER with only GRANT SELECT on the test table and reads the catalog as that user.
  • CHAR/VARCHAR/NCHAR: unchanged, asserted in both the catalog tests and the E2E.

CI is still red only because of #12874 (fix in #12875); I'll sync dev and rerun the failed jobs once #12875 is merged.

@DanielLeens

Copy link
Copy Markdown
Contributor

Thanks for mapping those cases to the catalog tests and the read-only-user E2E, @zhengxiang378928908-code. There is no new code in this exchange, so my earlier source-level conclusion remains unchanged; this reply does not claim a fresh run or full rereview. @SEZ9 can use those pointers for the independent recheck.

The upstream CI dependency has not changed: #12875 is still open, so the fix for #12874 is not in current dev. Your plan to sync after that fix lands and rerun the failed jobs is the right sequence; syncing before it lands will not clear the known JDBC completeness assertion. No unrelated change is requested in this PR. Thank you for keeping the validation and scope clear.

@DanielLeens DanielLeens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for rebasing, @zhengxiang378928908-code. Since my last review at 8a8c4b8, the head is now 517d260 (a single commit rebased onto current dev). I compared the PR-only diff of both heads line by line and it is identical: the same five files (DamengCatalog, DamengCatalogTest, JdbcDmIT, and the English/Chinese incompatible-changes pages). The rebase also picks up the upstream fix for the JDBC option completeness failure (#12938 is in dev, and #12875 was closed as superseded), so the earlier unrelated red Build should no longer apply. This is a full fresh review of the current head. My earlier conclusion on the code is unchanged, and none of my previous findings were open (I had no numbered issues).

What Problem Does This PR Solve?

  • User pain point: Dameng NVARCHAR(n) and NVARCHAR2(n) hold n characters, but the catalog read ALL_TAB_COLUMNS.DATA_LENGTH (bytes), so NVARCHAR(10) was reported as NVARCHAR(20) on GB18030 or NVARCHAR(40) on UTF-8, and downstream auto-created tables were two to four times too wide.
  • Fix approach: join SYS.SYSCOLUMNS to read the declared LENGTH$, use it for NVARCHAR/NVARCHAR2, and fall back to CHAR_LENGTH, then to the old byte length.
  • One-sentence summary: the Dameng JDBC catalog now reports national variable-width character columns in their declared character unit.

Example:

Before: NVARCHAR(10) on a GB18030 database -> source type NVARCHAR(20) -> MySQL sink VARCHAR(80)
After:  NVARCHAR(10) on a GB18030 database -> source type NVARCHAR(10) -> MySQL sink VARCHAR(40)

1. Code Change Review

1.1 Core Logic Analysis

Reviewed head: 517d260a3508932003e88d3a812b9b0891467a21. Changed files: DamengCatalog.java (SQL + buildColumn + new nationalCharLength), DamengCatalogTest.java (new), JdbcDmIT.java, and docs/{en,zh}/introduction/concepts/incompatible-changes.md.

// before
long columnLength = resultSet.getLong("DATA_LENGTH");
// after
long columnLength = nationalCharLength(typeName,
        resultSet.getLong("DATA_LENGTH"), resultSet.getLong("CHAR_LENGTH"),
        resultSet.getLong("DECLARED_LENGTH"));

Runtime flow:

JDBC source/sink resolves a Dameng table
  -> DamengCatalog.getTable() runs SELECT_COLUMNS_SQL
       (ALL_TAB_COLUMNS LEFT JOIN comments LEFT JOIN SYSOBJECTS(SCH) / SYSOBJECTS(TAB) / SYSCOLUMNS)
  -> buildColumn() -> nationalCharLength(): NVARCHAR/NVARCHAR2 -> LENGTH$ > CHAR_LENGTH > DATA_LENGTH
  -> DmdbTypeConverter.convert() builds the SeaTunnel Column
  -> source schema and sink auto-create use the corrected length

Findings:

  • The normal user path (any Dameng table discovered through DamengCatalog) reaches the changed logic. Per-row read/write paths, splits, checkpoints and serialization are untouched.
  • All joins are LEFT JOINs, so a missing or unreadable system-table row yields DECLARED_LENGTH = 0 via getLong, and the code falls back instead of dropping the column or failing. A user with only SELECT on the table is covered by the E2E.
  • Only NVARCHAR/NVARCHAR2 (case-insensitive) change; CHAR, VARCHAR, VARCHAR2, NCHAR and binary types keep DATA_LENGTH, which is the pre-change behavior.
  • The SELECT_COLUMNS_SQL change keeps its existing %s placeholders and column order, and the previously selected columns are unchanged.

1.2 Compatibility Impact

Partially incompatible (by design, documented). Newly discovered NVARCHAR/NVARCHAR2 source types and sink auto-create lengths become narrower, but still hold every source value because the length is counted in characters. No option names, defaults, public APIs or serialization formats change, and existing downstream tables are not altered. Both the English and Chinese incompatible-change entries describe the impact and migration path.

1.3 Performance / Side-Effect Analysis

The extra joins run once per table at schema discovery, keyed by object id and name, so there is no per-row or per-checkpoint cost, and no new threads, connections or resources. At the production scale of very large data volumes this is not on the data path.

1.4 Error Handling and Logging

The fallback chain preserves the previous byte length when metadata is unavailable (oversizing, never truncating). No new exceptions are introduced and nothing sensitive is logged. No source-level issue was found, so there are no formal issues to list (Issue count: 0).

2. Code Quality Assessment

2.1 Coding Standards

The new nationalCharLength has a complete Javadoc with parameters and the reason DATA_LENGTH and CHAR_LENGTH are insufficient, and the SQL join carries an explanatory comment. The new test class has the ASF header. No missing-comment issue.

2.2 Test Coverage and Test Stability

DamengCatalogTest is a parameterized matrix over NVARCHAR/NVARCHAR2, GB18030/UTF-8, LENGTH_IN_CHAR 0/1, fallbacks, unaffected types, and buildColumn with a mocked ResultSet. It is deterministic. JdbcDmIT#testNationalCharLengthFromCatalog checks real catalog output on the default database and on a second container initialized with LENGTH_IN_CHAR=1 CHARSET=1, plus a read-only user. The second container uses a mapped (non-fixed) port and a readiness log wait with a 5-minute timeout, and it cleans up the table and user in finally. Stability rating: Stable. Only the extra container start-up time is worth keeping an eye on in CI.

2.3 Documentation Updates

docs/en and docs/zh incompatible-changes entries are present, consistent with each other, and match the code (source column and the 1/2 or 1/4 factor for auto-create).

3. Architectural Soundness

3.1 Elegance of the Solution

Precise fix at the catalog metadata boundary, with the logic isolated in connector code (no engine/core/API change).

3.2 Maintainability

A small static helper with an explicit per-type contract; easy to extend if another Dameng type needs the same treatment.

3.3 Extensibility

Further Dameng metadata exceptions can be added in the same helper and query without touching shared classes.

3.4 Historical-Version Compatibility

Job configs and options are unchanged. The only effect is the documented schema correction for newly resolved columns.

4. Issue Summary

Number Issue Location Severity
- No source-level issues found - -

5. Merge Recommendation

Conclusion: Ready to merge after fixes

  1. Blockers - must be fixed

    • No source-level blocker. The only gate is a green Build on this head (see below).
  2. Recommended fixes - non-blocking

    • None.

CI / merge state: for head 517d260, the contributor fork's Build run (https://github.com/zhengxiang378928908-code/seatunnel/actions/runs/39915800508) was in_progress when I checked, and the apache-side PR signal run completed with action_required (that is only the pointer check that waits on the fork run). The previous red was JdbcOptionRuleCompletenessTest from the numeric_special_value_as_null option, which dev has since fixed via #12938; this head is rebased on that dev. The merge state shows BLOCKED pending required checks/approvals, and the branch is mergeable with no conflicts. Being behind dev is only metadata. No local build or test was run (analysis is by code reading); please let the fork Build finish and, if any job fails, tell me which lane so I can check whether it relates to this diff.

Overall assessment: this is a small, well-bounded and well-tested correction of a real unit mismatch, with the compatibility effect documented. I see nothing to change in the code. @SEZ9, your earlier three questions are answered in the author's reply (NVARCHAR2 with CHAR_LENGTH = 0, read-only user, unaffected CHAR/VARCHAR/NCHAR) and I verified those cases in the tests above.
Alternatives: keeping the byte length and dividing by a charset ratio would be a guess and was rightly avoided.

@zhengxiang378928908-code

Copy link
Copy Markdown
Contributor Author

Thanks, @DanielLeens. The fork Build for 517d260 has finished and is green: https://github.com/zhengxiang378928908-code/seatunnel/actions/runs/39915800508 . unit-test passes on JDK 11 and 17 (Ubuntu and Windows), and JdbcDmIT (12 tests, including testNationalCharLengthFromCatalog) passes on both JDKs in the updated-modules lanes.

@DanielLeens

Copy link
Copy Markdown
Contributor

Thanks for the update, @zhengxiang378928908-code, and for linking the run. I checked fork run 39915800508: it is completed / success and bound to head 517d260, so it matches the current PR head. unit-test is green on JDK 11 and 17 for both Ubuntu and Windows, all updated-modules-integration-test lanes are green, and the remaining jobs are skipped (not failed). That clears the earlier JdbcOptionRuleCompletenessTest failure.

There is no new code since my last review, so my source-level conclusion is unchanged: no source-level blocker. I did not rerun anything locally. I also did not separately confirm which lane executed JdbcDmIT, so I am relying on your report for that detail.

Nothing further is needed from you. The PR is still waiting on the required review approval, which is not something you need to act on, and @SEZ9 can finish the independent recheck. Thanks again for the clear validation write-up.

…AR and NVARCHAR2

Fixes apache#12752.

DamengCatalog took the length of every column from
ALL_TAB_COLUMNS.DATA_LENGTH. For NVARCHAR(n) and NVARCHAR2(n) that is a
byte count, n * 2 on GB18030 and n * 4 on UTF-8, while the columns hold
n characters. NVARCHAR(10) was reported as NVARCHAR(20) or NVARCHAR(40).

The view definition shows DATA_LENGTH = SYSCOLUMNS.LENGTH$ * 2 or * 4
for character-length columns, and CHAR_LENGTH = LENGTH$ only for a type
list that leaves out NVARCHAR2. The catalog now also selects
SYS.SYSCOLUMNS.LENGTH$ and uses it for NVARCHAR and NVARCHAR2, then
CHAR_LENGTH, then DATA_LENGTH (the old behavior, which oversizes but
never truncates). All other types keep DATA_LENGTH.

Measured on DM V8 with LENGTH_IN_CHAR 0/1 and GB18030/UTF-8: in all
four, NVARCHAR(n) and NVARCHAR2(n) take exactly n characters, and
LENGTH$ is n. A user that can only read the table can read LENGTH$.

- DamengCatalogTest: the measured matrix, fallbacks, unchanged
  CHAR/VARCHAR/NCHAR, and buildColumn.
- JdbcDmIT: the image's default database (LENGTH_IN_CHAR=0, GB18030),
  a second one initialized with LENGTH_IN_CHAR=1 and UTF-8, and a
  read-only user.
- incompatible-changes (en/zh).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@DanielLeens DanielLeens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for rebasing again, @zhengxiang378928908-code. The head is now 02c5b80ddf4f9eb72f2d2a700e9d9813cf63da0e, one commit on top of the current dev tip (9ab97e15f). I compared it with the previous head 517d260 using git range-diff. The production code, the tests and the wording of both incompatible-changes.md entries are unchanged. The only difference is where the entries sit in the ## dev section, which moved because of new upstream entries. That is the expected result of the rebase. This is a fresh full review of the current head. My earlier conclusion on the code still holds, and I had no open numbered issues.

What Problem Does This PR Solve?

  • User pain point: Dameng NVARCHAR(n) / NVARCHAR2(n) hold n characters, but DamengCatalog read ALL_TAB_COLUMNS.DATA_LENGTH, which is a byte count (2n on GB18030, 4n on UTF-8). So NVARCHAR(10) was reported as NVARCHAR(20) or NVARCHAR(40), and sink auto-create made columns two to four times too wide.
  • Fix approach: also select CHAR_LENGTH and SYS.SYSCOLUMNS.LENGTH$ (the declared length, through LEFT JOINs on SYS.SYSOBJECTS). For NVARCHAR / NVARCHAR2 only, use LENGTH$, then CHAR_LENGTH, then the old DATA_LENGTH.
  • One-sentence summary: the Dameng catalog now reports national variable-width character columns in their declared character unit.

1. Code Change Review

1.1 Core Logic Analysis

Changed files: DamengCatalog.java (SELECT_COLUMNS_SQL, buildColumn, new nationalCharLength), new DamengCatalogTest.java, JdbcDmIT.java, and docs/{en,zh}/introduction/concepts/incompatible-changes.md.

// before (DamengCatalog.buildColumn)
long columnLength = resultSet.getLong("DATA_LENGTH");
// after
long columnLength = nationalCharLength(typeName,
        resultSet.getLong("DATA_LENGTH"), resultSet.getLong("CHAR_LENGTH"),
        resultSet.getLong("DECLARED_LENGTH"));

Runtime flow:

JDBC source (table_path) / sink (save mode, auto-create) resolves a Dameng table
  -> DamengCatalog.getTable(TablePath) -> SELECT_COLUMNS_SQL
       ALL_TAB_COLUMNS LEFT JOIN ALL_COL_COMMENTS
       LEFT JOIN SYSOBJECTS SCH (TYPE$='SCH') -> SYSOBJECTS TAB (TYPE$='SCHOBJ') -> SYSCOLUMNS
  -> buildColumn() -> nationalCharLength(): NVARCHAR/NVARCHAR2 -> LENGTH$ > CHAR_LENGTH > DATA_LENGTH
  -> DmdbTypeConverter.convert(): sourceType "<type>(n)", columnLength = charTo4ByteLength(n)
  -> source CatalogTable schema and downstream sink auto-create

Key findings:

  • The normal user path (any Dameng table resolved through DamengCatalog.getTable(TablePath)) reaches the change. Row reading and writing, splits, checkpoints and serialization are untouched.
  • Every new join is a LEFT JOIN, and getLong returns 0 for SQL NULL. So when the system-table lookup misses or cannot be read, the column falls back to the previous length. Nothing is dropped and nothing fails. The join keys (schema object by name and TYPE$='SCH', schema object by SCHID, name and TYPE$='SCHOBJ', column by object id and name) identify one row per column. I do not see a way for them to duplicate rows.
  • Only NVARCHAR / NVARCHAR2 (case-insensitive) change. CHAR, VARCHAR, VARCHAR2, NCHAR, binary and LOB types keep DATA_LENGTH, exactly as before. The existing %s placeholders, the column order and the SOURCE_TYPE expression are unchanged.
  • The query-mode path (getTable(String sqlQuery), which uses JDBC ResultSetMetaData) is not touched, so this PR does not change it.

1.2 Compatibility Impact

Partially incompatible (by design, documented). For newly discovered NVARCHAR / NVARCHAR2 columns, the source type and the auto-create length become narrower. They still hold every source value, because the length is now counted in characters. No option, default, public API or serialization format changes, and existing downstream tables are not altered. Both the EN and ZH incompatible-changes.md entries describe the impact and the migration path, and they are consistent with each other and with the code.

1.3 Performance / Side-Effect Analysis

The extra joins run once per table at schema discovery and use object id / name keys. There is no per-row or per-checkpoint cost, and no new threads, connections or resources. Even at very large data volumes this is not on the data path.

1.4 Error Handling and Logging

The fallback chain keeps the previous byte length when the metadata is unavailable, so it may oversize but never truncates. No new exceptions are introduced and nothing sensitive is logged. Formal issues: none.

2. Code Quality Assessment

2.1 Coding Standards

nationalCharLength has a complete Javadoc, which explains why neither DATA_LENGTH nor CHAR_LENGTH is sufficient. The new join has a why-comment, and the new test class has the ASF header. A tiny style point, not an issue: INIT_LENGTH_IN_CHAR_UTF8 in JdbcDmIT is a static final field declared between methods. Moving it next to the other constants would read a little better.

2.2 Test Coverage and Test Stability

  • DamengCatalogTest is a deterministic, parameterized matrix: both national types, GB18030 / UTF-8, LENGTH_IN_CHAR 0 / 1, CHAR_LENGTH = 0 for NVARCHAR2, the missing-LENGTH$ and missing-CHAR_LENGTH fallbacks, unaffected types, and buildColumn with a mocked ResultSet. Reverting buildColumn to DATA_LENGTH, or dropping the LENGTH$ priority, turns it red.
  • JdbcDmIT#testNationalCharLengthFromCatalog checks real catalog output, and the expected values would fail on the old code (NVARCHAR(20) / 80 instead of NVARCHAR(10) / 40). It runs on the default database (GB18030, LENGTH_IN_CHAR=0), on a second container initialized with LENGTH_IN_CHAR=1 CHARSET=1, and as a read-only user. It also asserts the database mode first, so a changed image default cannot make the test pass silently. The second container uses a mapped port, waits for a readiness log line with a 5-minute timeout, and is closed by try-with-resources. Table and user are cleaned up in finally.
  • Stability rating: Stable. The only cost is the extra container start-up time in the JDBC part-5 lane.

2.3 Documentation Updates

The EN and ZH incompatible-change entries are present, rebased cleanly into the current ## dev section, and match the code. No connector option or user page needs an update.

3. Architectural Soundness

  • 3.1 Elegance: Precise fix at the catalog metadata boundary, inside connector code (no engine, core or API change). It avoids the tempting but wrong "divide DATA_LENGTH by a charset ratio".
  • 3.2 Maintainability: a small static helper with an explicit per-type contract.
  • 3.3 Extensibility: other Dameng metadata exceptions (for example VARCHAR(n CHAR), which the PR description deliberately leaves for a separate issue) can be handled in the same layer.
  • 3.4 Historical-Version Compatibility: job configs and options are unchanged. The only effect is the documented schema correction for newly resolved columns.

4. Issue Summary

Number Issue Location Severity
- No issues found - -

5. Merge Recommendation

CI: the rebase restarted CI. The fork Build for this head (run 40086136661) is queued, and the apache-side Build check has not reported yet. The previous head 517d260 was fully green (run 39915800508), and the PR-only diff is the same, so I expect the same result, but it still has to finish. The branch is level with dev (ahead 1, behind 0), with no conflicts.

Conclusion: Ready to merge

  1. Blockers (must fix): none. No blocking issues found.
  2. Recommended fixes (non-blocking): none (the JdbcDmIT constant placement is optional).

Overall assessment: a small, well-bounded and well-tested correction of a real unit mismatch, with the compatibility effect documented. The remaining gates are a green Build on this head and a write-capable committer approval. Residual risk: Dameng versions other than the DM8 image used in CI. The LEFT JOIN fallback keeps the old byte length there, so the worst case is the previous oversizing, never truncation. Nothing was built or executed locally. This review is based on the full diff, the call chain at this head, and the previous green CI evidence.

Better alternative: none. Dividing DATA_LENGTH by a charset ratio would be a guess, and the PR rightly avoids it.

@zhengxiang378928908-code

Copy link
Copy Markdown
Contributor Author

@DanielLeens The fork Build for head 02c5b80dd is green now: https://github.com/zhengxiang378928908-code/seatunnel/actions/runs/40086136661

The first attempt failed in mysql-cdc-connector-it (11) before any test ran: Maven couldn't start (Could not find or load main class org.apache.maven.wrapper.MavenWrapperMain). I re-ran only that job, and it passed. No code changed.

@DanielLeens

Copy link
Copy Markdown
Contributor

Thanks for rerunning only the affected job, @zhengxiang378928908-code. I checked Build 40086136661: it is completed/success on the current 02c5b80ddf4f9eb72f2d2a700e9d9813cf63da0e; the latest job listing has 78 entries and no failed or unfinished job. That supersedes my earlier CI-pending statement. There is no new source change to re-review and my technical conclusion remains non-blocking. The upstream PR snapshot still has no required Build result attached, so a write-capable maintainer should inspect that workflow/check gate and perform the final effective approval. Another source patch or rerun of the successful fork workflow is not the next step.

@DanielLeens

Copy link
Copy Markdown
Contributor

Thanks for the update, @zhengxiang378928908-code. I checked fork run 40086136661: attempt 2 is completed/success and bound to the unchanged head 02c5b80. The first-attempt failure in mysql-cdc-connector-it (11) was a Maven wrapper startup error before any test ran, so rerunning only that job is the right call and it is unrelated to this change. My review found no blockers, and the CI gate is now met, so my conclusion on this head is ready to merge. The remaining gate is an approval from a committer with write access. Nothing was executed locally; this is based on code reading and the fork CI evidence.

@nzw921rx nzw921rx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 LGTM

@nzw921rx
nzw921rx added this pull request to the merge queue Oct 26, 2026
@nzw921rx
nzw921rx removed this pull request from the merge queue due to a manual request Oct 26, 2026
@nzw921rx
nzw921rx added this pull request to the merge queue Oct 26, 2026
Merged via the queue into apache:dev with commit 4f70bc5 Oct 26, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [Connector-V2] [JDBC] Dameng NVARCHAR/NVARCHAR2 length is reported as twice the declared length

3 participants