Skip to content

Support setNString in ShardingSphere JDBC PreparedStatement - #40573

Open
Forry-xrm wants to merge 3 commits into
apache:masterfrom
Forry-xrm:fix-32314
Open

Forry-xrm wants to merge 3 commits into
apache:masterfrom
Forry-xrm:fix-32314

Conversation

@Forry-xrm

@Forry-xrm Forry-xrm commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What

Implement PreparedStatement#setNString in ShardingSphere JDBC:

  • record the value in AbstractPreparedStatementAdapter exactly like setString
  • remove the setNString throw from AbstractUnsupportedOperationPreparedStatement
  • add the matching no-op in CircuitBreakerPreparedStatement
  • replace the "unsupported" test with a parameter-capture test

Why

Fixes #32314. setNString currently throws SQLFeatureNotSupportedException.

Behavior

setNString binds through PreparedStatement#setNString on every replay path (regular execution, cached statement reuse, batches and SQL federation scans), preserving SQL Server national-character semantics even when sendStringParametersUnicode=false.

Testing

  • mvn -pl jdbc -Dtest=PreparedStatementAdapterTest,UnsupportedOperationPreparedStatementTest test
  • Full jdbc module tests: 1425 passed
  • Spotless / Checkstyle (-Pcheck): clean

AI assistance

Prepared with Qoder (AI coding agent) under human review; affected files are the five files listed above. The contributor reviewed and verified every change.

@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 adds JDBC PreparedStatement parameter capture for setNString, removes the unsupported-operation exception, and updates the circuit-breaker implementation and tests. However, parameters are still replayed through untyped setObject calls, losing national-character type information. Consequently, the configuration required by the linked issue can still lose Chinese characters.

Result

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

The following is a confirmed subset of findings. Review-record validation could not be completed, so this report does not claim an exhaustive finding set.

Blocking Issues

  1. [P1] Preserve national-character binding semantics for setNString

    • Evidence: AbstractPreparedStatementAdapter.java:101–104 stores only a plain String. Parameter replay always calls setObject(index, value). Ordinary execution, cached-statement reuse, and batch execution all use this path. Microsoft’s documentation distinguishes ordinary string binding from national-character setters, which always send Unicode.
    • Impact: When sendStringParametersAsUnicode=false and the database collation cannot encode the supplied characters, setNString(1, "中文") can still silently write question marks or fail to match NVARCHAR data. This is precisely the scenario described in #32314. The default configuration can mask the problem, and disclosing this limitation in the PR does not satisfy the method’s JDBC contract.
    • Required Change: Preserve national-character binding intent through native execution, including ordinary execution, statement reuse, and batches. Add regression coverage that distinguishes national-character binding from merely capturing a String value.

Coverage

  • Public candidate: fe8b4808cb4d08f60f700642cb0afec973d1f679. Two independent assessments covered all five changed files and the affected execution paths.
  • Checkstyle and Spotless passed for all five files. Production and test compilation passed. Focused tests timed out twice with exit code 124; no passing test result was obtained.
  • The public commit’s Git object was unavailable locally, preventing ledger initialization and review-record validation. No live SQL Server reproduction was performed. This assessment covers code correctness only; CI was not reviewed.

@Forry-xrm

Copy link
Copy Markdown
Contributor Author

Summary

This PR adds JDBC PreparedStatement parameter capture for setNString, removes the unsupported-operation exception, and updates the circuit-breaker implementation and tests. However, parameters are still replayed through untyped setObject calls, losing national-character type information. Consequently, the configuration required by the linked issue can still lose Chinese characters.

Result

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

The following is a confirmed subset of findings. Review-record validation could not be completed, so this report does not claim an exhaustive finding set.

Blocking Issues

  1. [P1] Preserve national-character binding semantics for setNString

    • Evidence: AbstractPreparedStatementAdapter.java:101–104 stores only a plain String. Parameter replay always calls setObject(index, value). Ordinary execution, cached-statement reuse, and batch execution all use this path. Microsoft’s documentation distinguishes ordinary string binding from national-character setters, which always send Unicode.
    • Impact: When sendStringParametersAsUnicode=false and the database collation cannot encode the supplied characters, setNString(1, "中文") can still silently write question marks or fail to match NVARCHAR data. This is precisely the scenario described in #32314. The default configuration can mask the problem, and disclosing this limitation in the PR does not satisfy the method’s JDBC contract.
    • Required Change: Preserve national-character binding intent through native execution, including ordinary execution, statement reuse, and batches. Add regression coverage that distinguishes national-character binding from merely capturing a String value.

Coverage

  • Public candidate: fe8b4808cb4d08f60f700642cb0afec973d1f679. Two independent assessments covered all five changed files and the affected execution paths.
  • Checkstyle and Spotless passed for all five files. Production and test compilation passed. Focused tests timed out twice with exit code 124; no passing test result was obtained.
  • The public commit’s Git object was unavailable locally, preventing ledger initialization and review-record validation. No live SQL Server reproduction was performed. This assessment covers code correctness only; CI was not reviewed.

Summary

This PR adds JDBC PreparedStatement parameter capture for setNString, removes the unsupported-operation exception, and updates the circuit-breaker implementation and tests. However, parameters are still replayed through untyped setObject calls, losing national-character type information. Consequently, the configuration required by the linked issue can still lose Chinese characters.

Result

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

The following is a confirmed subset of findings. Review-record validation could not be completed, so this report does not claim an exhaustive finding set.

Blocking Issues

  1. [P1] Preserve national-character binding semantics for setNString

    • Evidence: AbstractPreparedStatementAdapter.java:101–104 stores only a plain String. Parameter replay always calls setObject(index, value). Ordinary execution, cached-statement reuse, and batch execution all use this path. Microsoft’s documentation distinguishes ordinary string binding from national-character setters, which always send Unicode.
    • Impact: When sendStringParametersAsUnicode=false and the database collation cannot encode the supplied characters, setNString(1, "中文") can still silently write question marks or fail to match NVARCHAR data. This is precisely the scenario described in #32314. The default configuration can mask the problem, and disclosing this limitation in the PR does not satisfy the method’s JDBC contract.
    • Required Change: Preserve national-character binding intent through native execution, including ordinary execution, statement reuse, and batches. Add regression coverage that distinguishes national-character binding from merely capturing a String value.

Coverage

  • Public candidate: fe8b4808cb4d08f60f700642cb0afec973d1f679. Two independent assessments covered all five changed files and the affected execution paths.
  • Checkstyle and Spotless passed for all five files. Production and test compilation passed. Focused tests timed out twice with exit code 124; no passing test result was obtained.
  • The public commit’s Git object was unavailable locally, preventing ledger initialization and review-record validation. No live SQL Server reproduction was performed. This assessment covers code correctness only; CI was not reviewed.

Thanks for the review — updated in e4f28d1.

setNString now records a typed NationalCharacterString value (new class in infra/executor) instead of a plain String, and parameter binding is centralized in NationalCharacterString#bind:

  • Regular execution, cached statement reuse and batch execution all replay parameters through AbstractPreparedStatementAdapter#replaySetParameter, which binds this type with PreparedStatement#setNString and keeps setObject for every other value.
  • SQL federation scans (EnumerableScanImplementor#setParameters) use the same dispatch, so federated queries preserve the intent as well.
  • Regression coverage distinguishes intent from capture: NationalCharacterStringTest verifies N-type values bind via setNString while regular values keep using setObject; PreparedStatementAdapterTest pairs assertSetNString/assertReplaySetNString with assertSetString/assertReplaySetString.

Latest head verification: full jdbc module tests 1433 passed; focused infra/executor and sql-federation/executor tests passed; Spotless/Checkstyle clean on the three touched modules; Apache RAT check passed.

Bind setNString parameters through PreparedStatement#setNString instead of
untyped setObject, covering regular execution, cached statement reuse, batch
execution and SQL federation scans.

@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 enables JDBC setNString for the SQL Server scenario in #32314. It preserves native setter intent using NationalCharacterString and shared replay dispatch. However, that wrapper also reaches logical sharding values and breaks existing string-routing behavior.

Result

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

These findings are a confirmed subset. Formal review-record validation could not complete.

Blocking Issues

  1. [P1] Preserve logical String semantics during routing

    • Evidence: Parameter capture stores the wrapper directly. Condition merging then treats it as the logical value. Focused reproductions confirm mixed-type conversion failures and incorrect case-insensitive intersections.
    • Impact: For WHERE k = ? AND k = ?, mixing setString and setNString can throw before execution. Two national-string values "foo" and "FOO" on a case-insensitive key can produce a false empty intersection, triggering unicast routing and potentially missing matching data on other shards.
    • Required Change: Preserve logical String semantics while retaining native binding intent. Add regression coverage for mixed setters, literal/parameter ranges, and case-insensitive keys.
  2. [P2] Exercise replay through public APIs

    • Evidence: The new replay tests directly invoke protected replaySetParameter.
    • Impact: This violates the explicit public-API testing rule.
    • Required Change: Verify adapter replay through public execution APIs without widening production visibility.
  3. [P2] Use equivalent Lombok constructor generation

    • Evidence: The new constructor only assigns its final field.
    • Impact: It violates the boilerplate constructor rule; no manual-constructor exception applies.
    • Required Change: Use @RequiredArgsConstructor, which preserves the current signature and behavior.
  4. [P2] Document the nullable binding value

  5. [P2] Mark the new high-frequency parameter paths

    • Evidence: The new setNString and shared binder repeat with normal query throughput and lack @HighFrequencyInvocation.
    • Impact: They do not satisfy the required annotation rule. This is a policy finding, not a measured performance regression.
    • Required Change: Apply the annotation at the smallest accurate declaration scope.

Coverage

  • Public head: d081bbfb2467ea1abf30b86dbfaed5fa5b51817c. Two independent assessments covered all eight changed files and affected consumers.
  • Checkstyle and Spotless passed for all eight Java files, exit 0. Focused source reproductions confirmed the routing failures.
  • The public-head Git object was unavailable locally, preventing canonical ledger/session validation; source and style verification used a hash-verified snapshot. Full module tests and live SQL Server execution were not run. This is a code-scope review; CI was not reviewed.

Unwrap NationalCharacterString at every routing parameter read so sharding
condition merging and assignment routing see logical Strings while replay
still binds through PreparedStatement#setNString.

- Unwrap in ConditionValue, InsertClauseShardingConditionEngine and
  ShardingRouteContextCheckUtils
- Drive replay tests through public executeQuery APIs
- Use @requiredargsconstructor, document SQL NULL and mark high-frequency
  parameter paths
- Cover mixed setters, literal/parameter ranges, case-insensitive keys and
  INSERT/SET assignment values
@Forry-xrm

Copy link
Copy Markdown
Contributor Author

Summary

This PR enables JDBC setNString for the SQL Server scenario in #32314. It preserves native setter intent using NationalCharacterString and shared replay dispatch. However, that wrapper also reaches logical sharding values and breaks existing string-routing behavior.

Result

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

These findings are a confirmed subset. Formal review-record validation could not complete.

Blocking Issues

  1. [P1] Preserve logical String semantics during routing

    • Evidence: Parameter capture stores the wrapper directly. Condition merging then treats it as the logical value. Focused reproductions confirm mixed-type conversion failures and incorrect case-insensitive intersections.
    • Impact: For WHERE k = ? AND k = ?, mixing setString and setNString can throw before execution. Two national-string values "foo" and "FOO" on a case-insensitive key can produce a false empty intersection, triggering unicast routing and potentially missing matching data on other shards.
    • Required Change: Preserve logical String semantics while retaining native binding intent. Add regression coverage for mixed setters, literal/parameter ranges, and case-insensitive keys.
  2. [P2] Exercise replay through public APIs

    • Evidence: The new replay tests directly invoke protected replaySetParameter.
    • Impact: This violates the explicit public-API testing rule.
    • Required Change: Verify adapter replay through public execution APIs without widening production visibility.
  3. [P2] Use equivalent Lombok constructor generation

    • Evidence: The new constructor only assigns its final field.
    • Impact: It violates the boilerplate constructor rule; no manual-constructor exception applies.
    • Required Change: Use @RequiredArgsConstructor, which preserves the current signature and behavior.
  4. [P2] Document the nullable binding value

  5. [P2] Mark the new high-frequency parameter paths

    • Evidence: The new setNString and shared binder repeat with normal query throughput and lack @HighFrequencyInvocation.
    • Impact: They do not satisfy the required annotation rule. This is a policy finding, not a measured performance regression.
    • Required Change: Apply the annotation at the smallest accurate declaration scope.

Coverage

  • Public head: d081bbfb2467ea1abf30b86dbfaed5fa5b51817c. Two independent assessments covered all eight changed files and affected consumers.
  • Checkstyle and Spotless passed for all eight Java files, exit 0. Focused source reproductions confirmed the routing failures.
  • The public-head Git object was unavailable locally, preventing canonical ledger/session validation; source and style verification used a hash-verified snapshot. Full module tests and live SQL Server execution were not run. This is a code-scope review; CI was not reviewed.

All five findings are addressed in 979e49d:

  1. Routing now unwraps NationalCharacterString at every parameter read (ConditionValue, InsertClauseShardingConditionEngine, ShardingRouteContextCheckUtils), so condition merging and assignment routing see logical String values while the wrapper stays only in the binding path. Regression coverage: mixed setString/setNString, literal/parameter ranges, case-insensitive keys, and INSERT/SET assignment values.
  2. Replay is verified through public executeQuery() with the production add/replay callbacks; replaySetParameter remains protected.
  3. NationalCharacterString uses @RequiredArgsConstructor with the same signature.
  4. bind/unwrap document SQL NULL in @param value, with a null replay test.
  5. @HighFrequencyInvocation is applied at method scope to setNString, bind and unwrap.

Focused suites pass (condition engines, adapter, federation scan, parameter type); Spotless/Checkstyle clean on all 15 files; RAT clean. Live SQL Server execution and full-module suites were not run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqlServer uses setNString

2 participants