Skip to content

Fix false positive of table metadata consistency check - #40549

Merged
terrymanu merged 3 commits into
apache:masterfrom
Drifter-yh:fix-30939-table-metadata-consistency
Dec 4, 2026
Merged

terrymanu merged 3 commits into
apache:masterfrom
Drifter-yh:fix-30939-table-metadata-consistency

Conversation

@Drifter-yh

@Drifter-yh Drifter-yh commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #30939.

Changes proposed in this pull request:

  • Compare table metadata by value in SchemaTableMetaDataAggregator instead of comparing TableMetaData.toString().
  • Compare columns in JDBC ordinal order, composite index columns as an ordered sequence, and the outer index/constraint collections as unordered collections.
  • Restore value-based equals/hashCode on ColumnMetaData, IndexMetaData and ConstraintMetaData (present in 5.4.1, dropped when the metadata model moved to database/connector/core).
  • Update ShardingSchemaTableAggregationReviserTest to express its structural mismatch with non-null collections.
  • Add regression tests for uniform structures with different index/constraint order, a collection whose equals is identity based, composite index column order, column definition, column order, table type and an additional index.

Why toString() is not a safe comparison:

  • IndexMetaDataLoader returns HashMap.values(), while the PostgreSQL and openGauss loaders pass LinkedHashMultimap.get() views (Guava WrappedSet) as indexes and constraints. PostgreSQLMetaDataLoader reads pg_indexes without ORDER BY.
  • The same physical table can therefore be loaded with a different collection implementation and a different iteration order. With check-table-metadata-enabled: true, the check reports RuleAndStorageMetaDataMismatchedException for identical structures.
  • EncryptIndexReviser builds index columns as a LinkedHashSet, so the columns of one composite index must be compared as an ordered sequence rather than through Set.equals().

Verification:

  • Ran SchemaTableMetaDataAggregatorTest (13/13), ShardingSchemaTableAggregationReviserTest (3/3) and BroadcastSchemaTableAggregationReviserTest (3/3) against the installed 5.5.4-SNAPSHOT module jars with the changed classes first on the classpath.
  • The composite-index order test fails on 3accf1c with Expected RuleAndStorageMetaDataMismatchedException to be thrown, but nothing was thrown and passes on 3c2832c; the null-fixture test fails with NullPointerException on 3accf1c and passes on 3c2832c.
  • spotless:check with the project Eclipse formatter config and Checkstyle 14.1.0 with src/resources/checkstyle.xml report 0 violations on the changed Java files.
  • ./mvnw clean install -B -T1C -Pcheck was not run locally (no full reactor build in this environment); the module build is left to CI.

Before submitting this PR, I confirm that:

  • My code follows the code of conduct of this project.
  • I have reviewed my changes.
  • I have (or in comment I request) added corresponding labels for the pull request.
  • I have passed the required local checks: ./mvnw clean install -B -T1C -Pcheck.
  • I have updated the relevant documentation, if applicable.
  • I have added or updated unit tests for my changes, if applicable.
  • I have updated the Release Notes of the current development version, if applicable. For more details, see Update Release Note.
  • I have disclosed the tool and affected files or scope for any material AI assistance, as required by the AI-assisted contribution policy.

AI assistance: DeepSeek Harness (deepseek-v4.1-flash) was used to analyze the metadata comparison path and to draft the code and test changes. Affected files: SchemaTableMetaDataAggregator.java, ColumnMetaData.java, IndexMetaData.java, ConstraintMetaData.java, SchemaTableMetaDataAggregatorTest.java, ShardingSchemaTableAggregationReviserTest.java.

Drifter-yh added a commit to Drifter-yh/shardingsphere that referenced this pull request Nov 24, 2026
Add the bug fix entry for the table metadata consistency check.
Table metadata consistency was checked by comparing toString() output, which depends on the collection implementation and iteration order. The PostgreSQL and openGauss loaders pass Multimap views as indexes and constraints, while the core loader passes HashMap values, so identical structures could be reported as inconsistent.

Compare metadata by value instead: columns keep their JDBC ordinal order, while indexes and constraints are compared as unordered collections. Value-based equals and hashCode are restored for column, index and constraint models.
@Drifter-yh
Drifter-yh force-pushed the fix-30939-table-metadata-consistency branch from c21e5e6 to 3accf1c Compare November 24, 2026 08:48

@terrymanu terrymanu 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.

Summary

This PR replaces toString() comparison with value-based comparison to eliminate false metadata inconsistency reports caused by different index and constraint iteration orders. The direction has maintainer support, but the review confirmed a missed composite-index mismatch, an existing test regression, and insufficient regression coverage.

Result

Review Result: Not Mergeable
Feedback Mode: Change Request
Review Status: Not Converged
Confirmed Blocking Issues: 3

The findings below are a confirmed subset. The current head Git object is unavailable locally, preventing the required ledger and session validation. The formal review therefore cannot be declared fully converged.

Blocking Issues

[P2] Preserve column order within composite indexes

Evidence: The Lombok equality introduced at IndexMetaData.java:35 delegates directly to columns.equals(). The encryption reviser produces a LinkedHashSet, and subsequent broadcast-table aggregation executes the new comparison. An exact-source reproduction confirmed that the candidate accepts [a,b] and [b,a], whereas the baseline aggregator rejects them.

Impact: With metadata consistency checking enabled, encrypted broadcast tables with different composite-index structures are incorrectly considered consistent.

Required Change: Compare columns within each index as an ordered sequence and add a regression test for this production path. Continue ignoring order in the outer index collection.

[P2] Update the affected sharding aggregation test fixture

Evidence: ShardingSchemaTableAggregationReviserTest.java:78 constructs metadata with null collections. The new logic throws NullPointerException while comparing the sample with itself, failing the test that expects RuleAndStorageMetaDataMismatchedException. Direct execution passed 2/3 tests with the candidate and 3/3 after substituting the baseline aggregator.

Impact: The PR introduces an existing-test regression. There is currently no evidence that normal production paths supply these null collections.

Required Change: Express the fixture’s structural mismatch using non-null collections while preserving the original exception assertion.

[P2] Add regression coverage for the new structural comparison decisions

Evidence: The cases added at SchemaTableMetaDataAggregatorTest.java:76 do not protect same-name tables with different column definitions, different column orders, different table types, or an additional index in the actual table. An existing broadcast test covers differing column counts, so column-comparison rejection is not entirely uncovered.

Impact: These new decisions lack necessary regression protection and do not satisfy the repository’s requirement for complete coverage of owned behavior.

Required Change: Add the four distinct scenarios above. One meaningful collection-size mismatch case is sufficient for the shared comparison helper; duplicate coverage is unnecessary.

Coverage

Public head: 3accf1c49196d3effdc60e2cdaab7fcba1e5bc55. All 6 changed files and relevant consumers were inspected. Checkstyle and Spotless passed for all 5 Java files, both with exit code 0. Neither configured check governs the release notes.

Exact-source JUnit verification: aggregator 8/8, sharding 2/3, broadcast 3/3. Two focused Maven test attempts timed out with exit code 124; the full build was not completed.

This assessment covers code correctness only; CI was not reviewed. Ledger and session validation remain incomplete. No repository changes or remote comments were made.

Compare composite index columns as an ordered sequence instead of relying on Collection.equals(), which ignores order for Set backed columns such as the LinkedHashSet produced by EncryptIndexReviser. The outer index and constraint collections stay order-insensitive.

Replace the null collections in ShardingSchemaTableAggregationReviserTest with empty collections, and add regression cases for composite index column order, column definition, column order, table type and an additional index.
@Drifter-yh

Drifter-yh commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed the three blocking items in 3c2832c.

Composite index column order. IndexMetaData.equals/hashCode now compare columns as an ordered ArrayList instead of delegating to Collection.equals, so [a, b] and [b, a] differ even when the columns are a LinkedHashSet. isSameIgnoringOrder still ignores order in the outer index and constraint collections. The new assertAggregateWithDifferentCompositeIndexColumnOrderThrowsException mirrors EncryptIndexReviser; on 3accf1c it fails with Expected RuleAndStorageMetaDataMismatchedException to be thrown, but nothing was thrown.

Sharding fixture. assertAggregateFailedWithCheckTableMetaDataEnabled now builds both tables with empty collections and keeps the RuleAndStorageMetaDataMismatchedException assertion. Reproduced the reported failure first: the unchanged test with null collections fails against the new aggregator with NullPointerException: Cannot invoke "java.util.Collection.toArray()" because "c" is null (2/3); with the fixture change it passes (3/3).

Regression coverage. Added column definition (nullable), column order, table type (TABLE/VIEW), additional index and composite index column order. The additional-index case is the one collection-size mismatch for the shared helper, so no duplicate size case was added.

Verification: SchemaTableMetaDataAggregatorTest 13/13, ShardingSchemaTableAggregationReviserTest 3/3 and BroadcastSchemaTableAggregationReviserTest 3/3, 19/19 total, run against the installed 5.5.4-SNAPSHOT jars with the changed classes first on the classpath. Checkstyle 14.1.0 with src/resources/checkstyle.xml: 0 violations; spotless:check: pass.

CI has no result yet: all six workflows on 3c2832c are action_required pending maintainer approval, same as 3accf1c.

@terrymanu re-review requested on 3c2832c.

@Drifter-yh

Copy link
Copy Markdown
Contributor Author

All checks on 3c2832c are green: CI, CheckStyle, Spotless, License and the E2E SQL/Agent matrices (85 check runs: 83 success, 2 skipped matrix placeholders, 0 failures).

@terrymanu re-review requested.

@terrymanu terrymanu 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.

Summary

This PR replaces string-based metadata comparison with value-based comparison, ignoring the outer ordering of indexes and constraints while preserving column and composite-index column order. The direction matches the maintainer-confirmed goal, and all 19 relevant tests pass. One regression-coverage gap and four coding-rule violations remain.

Result

Review Result: Not Mergeable
Feedback Mode: Change Request
Blocking Issues: 5

Blocking Issues

[P2] Add regression coverage for the custom hash calculation

  • Evidence: IndexMetaData.java:64 introduces handwritten hash calculation, while the existing aggregation tests only verify comparison results.
  • Impact: The equal-hash contract lacks regression protection when identical ordered columns are represented by List and LinkedHashSet.
  • Required Change: Add focused tests for this equals/hashCode contract. Unequal objects do not need to produce different hashes.

[P2] Remove the null branch without a supporting contract

  • Evidence: IndexMetaData.java:69 returns null for a null column collection. No supported production path or nullable contract was found for that state.
  • Impact: This violates the default non-null contract and defensive-code rules.
  • Required Change: Remove the newly introduced null branch while preserving ordered collection conversion.

[P2] Inline the test helper that only forwards constructor arguments

  • Evidence: SchemaTableMetaDataAggregatorTest.java:202 forwards all four arguments unchanged and provides no shared fixture setup.
  • Impact: This violates the implementation rule against unnecessary thin wrappers.
  • Required Change: Invoke the existing constructor directly at the call sites.

[P2] Place the single-caller helper immediately after its caller

  • Evidence: SchemaTableMetaDataAggregatorTest.java:207 is called only by the table-type test at line 166, but other tests and helpers separate them.
  • Impact: This violates the declaration-order rule requiring private helpers to immediately follow their callers.
  • Required Change: Move this helper immediately after the table-type test.

[P2] Remove the unnecessary line break under the explicit wrapping rule

  • Evidence: The return expression at SchemaTableMetaDataAggregator.java:68 contains 235 characters in total; combining its first two lines produces only 161 characters.
  • Impact: The three-line layout violates the minimum-line-break rule. Passing Spotless does not cover this manual check. This issue existed in the previous version and was missed in the previous review.
  • Required Change: Combine the first two lines of the expression.

Coverage

Public head: 3c2832cf2bf438506609d80a0261c3b460bcc760. All seven changed files and relevant consumers were reviewed.

Checkstyle and Spotless covered all six Java files and passed with exit code 0. Neither check applies to the release notes. Exact-source execution against installed dependencies passed 19/19 tests. The regular Maven test attempt timed out during compilation (exit 124); the full build was not completed.

This assessment covers code correctness only; CI was not reviewed.

Add equals/hashCode contract coverage for ordered index columns, remove
the null branch from the ordered comparison, inline the test helper that
only forwarded constructor arguments, move the table-type helper next to
its caller, and join the wrapped return expression.
@Drifter-yh

Copy link
Copy Markdown
Contributor Author

Addressed the five blocking items in 93cf8b8.

Hash contract coverage. Added IndexMetaDataTest: the same ordered columns as a List and as a LinkedHashSet compare equal in both directions and produce the same hashCode; reversed column order is asserted not equal. No hash inequality is asserted for unequal objects.

Null branch. Removed toOrderedList; equals and hashCode now convert with new ArrayList<>(columns) directly, so the non-null contract is explicit and no null fallback remains.

Test helper inlined. Deleted the four-argument createTableMetaData wrapper; the six call sites now construct TableMetaData directly.

Helper placement. createTableMetaDataWithType now sits immediately after assertAggregateWithDifferentTableTypeThrowsException.

Line break. The first two lines of the return expression in isSameTableMetaData are joined; the merged line is 161 characters.

Verification: SchemaTableMetaDataAggregatorTest 13/13, IndexMetaDataTest 2/2, ShardingSchemaTableAggregationReviserTest 3/3 and BroadcastSchemaTableAggregationReviserTest 3/3, 21/21 total, run against the installed 5.5.4-SNAPSHOT jars with the changed classes first on the classpath. spotless:check and Checkstyle 14.1.0 with src/resources/checkstyle.xml pass.

CI for 93cf8b8 is action_required on all six workflows and needs maintainer approval. @terrymanu re-review requested.

@kimmking

kimmking commented Dec 4, 2026

Copy link
Copy Markdown
Member

/ci

@terrymanu terrymanu 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.

Correction

  • Previous Finding or Conclusion: The P2 test-helper ordering issue blocked approval.
  • Current Status: Withdrawn.
  • Reason: The cleanup is explicitly accepted for the next PR and no longer blocks this review.

Summary

This PR fixes false metadata inconsistency reports by replacing string comparison with structural value comparison. It preserves table-column and composite-index column order while ignoring irrelevant outer ordering of indexes and constraints.

Result

Review Result: Mergeable

LGTM. No remaining blocking issues were identified within the reviewed code scope.

Evidence

  • The comparison preserves meaningful metadata differences and collection multiplicity.
  • Checkstyle and Spotless passed for all seven changed Java files.
  • All 21 focused tests passed against the reviewed source.

Coverage

Public PR head: 93cf8b864d73baf16d38a5a008035a146a4c7185. All eight changed files were reviewed; verification results were reused for the unchanged head.

This is a code-correctness approval. CI was not reviewed, and the full build was not run.

@terrymanu terrymanu added this to the 5.5.4 milestone Dec 4, 2026
@terrymanu
terrymanu merged commit 12d7a39 into apache:master Dec 4, 2026
85 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants