Fix incorrect generated key handling for explicit auto-increment values#38810
Open
somiljain2006 wants to merge 7 commits into
Open
Fix incorrect generated key handling for explicit auto-increment values#38810somiljain2006 wants to merge 7 commits into
somiljain2006 wants to merge 7 commits into
Conversation
Contributor
Author
|
@terrymanu Can you review this pr? |
Member
|
Please resolve conflicts first. |
terrymanu
requested changes
Jun 6, 2026
Member
terrymanu
left a comment
There was a problem hiding this comment.
Summary
- Merge Decision: Not Mergeable
- Reason: The patch filters generated values only after execution, while explicit auto-increment inserts can still request and read backend generated keys before that filter.
Positive Feedback
- Using
GeneratedKeyContext.isGenerated()is the right direction for separating generated values from client-supplied values, and the PR includes a release-note entry.
Issues
- P1 Root-cause path still requests backend generated keys for explicit values (
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutor.java:189)- Problem: Normal Proxy execution still sets
isReturnGeneratedKeysfor everyInsertStatementwhen the dialect has a generated-key option, without checkingInsertStatementContext.getGeneratedKeyContext().isGenerated(). That flag is passed intoStatementOptionand can still reachstatement.getGeneratedKeys()/resultSet.getLong(1)atproxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/executor/callback/ProxyJDBCExecutorCallback.java:75andproxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/executor/callback/ProxyJDBCExecutorCallback.java:86. The same unconditional flag exists on the federation path atproxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseProxyConnector.java:264. The new.filter(GeneratedKeyContext::isGenerated)atproxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseProxyConnector.java:334is too late to prevent the linked issue's backend generated-key read failure, and it also cannot suppress a positive explicit value already carried inUpdateResult.lastInsertId, whichUpdateResponseHeaderkeeps atproxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/response/header/update/UpdateResponseHeader.java:67. - Impact: The reported Proxy/MySQL rule-managed insert with explicit
-3can still fail beforeUpdateResponseHeaderis built, and an adjacent explicit positive auto-increment value can still be returned aslastInsertId. This leaves issue #38717 only partially fixed. - Required Change: Please gate
RETURN_GENERATED_KEYSand the generated-key callback path with the same explicit/generated distinction, for both normal and federation execution paths if both are reachable. Please also add regression coverage that does not mock awayProxyJDBCExecutorCallback: verify explicit auto-increment values useNO_GENERATED_KEYSor otherwise never callgetGeneratedKeys(), and keep a generated-value case to prove real generated keys still work.
- Problem: Normal Proxy execution still sets
Review Details
- Reviewed Scope: Latest PR head
97cca573cb323987908b6620cd155acf0731c9fc; GitHub base SHA8162bdf1377764c7cf4a52edd32c35f4d0a2796c; local merge-base8162bdf1377764c7cf4a52edd32c35f4d0a2796c; local triple-dot file list matched GitHub/pulls/38810/files. ReviewedRELEASE-NOTES.md,proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseProxyConnector.java,proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseProxyConnectorTest.java, and the related generated-key execution / OK-packet path. Shared-path blast radius checked for MySQL and MariaDB generated-key metadata; other dialect metadata defaults to no generated-key option. - Not Reviewed Scope: CI/check statuses and full live Proxy + MySQL reproduction were not reviewed; no code changes were made.
- Verification:
git fetch apache master:refs/remotes/apache/master pull/38810/head:refs/remotes/apache/pr/38810exit 0;git diff --name-status refs/remotes/apache/master...refs/remotes/apache/pr/38810exit 0 and matched GitHub files; staticgit show/git grepinspection exit 0. Maven tests were not run because the code-path blocker is visible and the added unit test bypasses the failing callback path. - Release Note / User Docs: Release note is present in
RELEASE-NOTES.md:29; user docs are not required for this internal Proxy bug fix, but the release-note claim should be kept only after the root-cause fix is complete.
Contributor
Author
|
Thanks for the detailed review. I've updated the fix to address the generated-key retrieval path before execution rather than only filtering values in UpdateResponseHeader. Changes made:
All related tests are passing locally, including:
|
terrymanu
requested changes
Jun 6, 2026
Member
terrymanu
left a comment
There was a problem hiding this comment.
Summary
- Merge Decision: Not Mergeable
- Reason: The latest patch fixes the original explicit
-3path too broadly and now suppresses generated keys for MySQLAUTO_INCREMENTvalues that are still generated when the key column is explicitly present asNULLor0.
Positive Feedback
- The latest head is in the right direction compared with the previous round: it now gates
StatementOption, the normal Proxy callback path, the federation path, and the finalUpdateResponseHeadervalue propagation instead of only filtering after execution.
Issues
- P1 Explicit
NULL/0auto-increment inserts lose generated-key handling (proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutor.java:266,proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseProxyConnector.java:431)- Problem: The new gate returns generated keys only when
GeneratedKeyContext.isGenerated()is true, butGeneratedKeyContextEnginesetsgenerated=falsewhenever the auto-increment column is present in the INSERT list (infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/insert/keygen/engine/GeneratedKeyContextEngine.java:59andinfra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/insert/keygen/engine/GeneratedKeyContextEngine.java:103). That correctly covers explicit nonspecial values such as-3, but it also coversINSERT INTO t(id, ...) VALUES (NULL, ...)and0under normal MySQL mode, where MySQL still generates anAUTO_INCREMENTvalue. The MySQL 8.4 C API documentation distinguishes explicit nonspecial values fromNULL/0values that generate auto-increment IDs. - Impact: Valid Proxy MySQL/MariaDB inserts that intentionally request database-side generation with an explicit
NULLor0key column will now useStatement.NO_GENERATED_KEYS/prepareStatement(sql)andProxyJDBCExecutorCallbackwill skipgetGeneratedKeys()atproxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/executor/callback/ProxyJDBCExecutorCallback.java:75. That regresseslast_insert_id/ generated-key response behavior for an adjacent valid input while fixing issue #38717. - Required Change: Please distinguish explicit nonspecial client-supplied values from explicit special values that still ask MySQL/MariaDB to generate the auto-increment value. Keep suppressing generated-key reads for values like
-3, but preserve backend generated-key handling forNULLand0where the storage engine generates the value. Please add regression tests for both sides: explicit nonspecial values return no generated key, while explicitNULL/0generated cases still return the generated key.
- Problem: The new gate returns generated keys only when
Multi-Round Comparison
- Previous blocker: The prior review requested moving the fix earlier than
UpdateResponseHeaderso Proxy would not request/read backend generated keys for explicit nonspecial values. This is partially fixed in the latest head by gating both normal and federation execution paths. - New blocker: The new gate uses
GeneratedKeyContext.isGenerated()as a broader “should ask backend for generated keys” signal, but that flag does not model MySQL’s explicitNULL/0generation semantics. The adjacent generated-key case remains untested.
Review Details
- Reviewed Scope: Latest PR head
d0d9c09c91a48602130176bebfb6ba4e373eac00; GitHub base SHA8162bdf1377764c7cf4a52edd32c35f4d0a2796c; local merge-base8162bdf1377764c7cf4a52edd32c35f4d0a2796c; local triple-dot file list matched GitHub/pulls/38810/files. ReviewedRELEASE-NOTES.md,proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutor.java,proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseProxyConnector.java, and the three changed test files, plus related generated-key binder/callback/OK-packet paths. Shared-path blast radius checked for MySQL and MariaDB, which exposeDialectGeneratedKeyOption; dialects such as PostgreSQL/openGauss/SQL92 default to no generated-key option on this path. Review baseline includedCODE_OF_CONDUCT.md:8,CODE_OF_CONDUCT.md:11,CODE_OF_CONDUCT.md:17, andCODE_OF_CONDUCT.md:18. - Not Reviewed Scope: CI/check statuses, full live Proxy + MySQL reproduction, and unrelated dirty local workspace files were not reviewed or used for conclusions.
- Verification:
git fetch apache master:refs/remotes/apache/master pull/38810/head:refs/remotes/apache/pr/38810exit 0; localgit diff --name-status 8162bdf1377764c7cf4a52edd32c35f4d0a2796c..refs/remotes/apache/pr/38810matched GitHub/pulls/38810/filesexit 0; staticgit show/git grepinspection exit 0;git grep computeIfAbsenton changed production files returned no matches. Maven was not run because the merge blocker is visible in the control flow and the current tests do not cover the adjacentNULL/0generated-key case. - Release Note / User Docs: Release note is present at
RELEASE-NOTES.md:29; user docs are not required for this internal Proxy bug fix, but the release-note claim should remain only after the regression above is fixed.
Contributor
Author
|
Thanks for the detailed review. I've updated the implementation to distinguish explicit non-special values from explicit values that still trigger AUTO_INCREMENT generation in MySQL/MariaDB. Changes made:
All related tests are passing locally. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #38717
Changes proposed in this pull request:
When an INSERT specifies an explicit value for an auto-increment column, Proxy should not populate lastInsertId from that value. This aligns the behavior with MySQL and prevents incorrectly generated key responses for explicit values. Add regression test covering explicit negative auto-increment values.
Before committing this PR, I'm sure that I have checked the following options:
./mvnw clean install -B -T1C -Dmaven. javadoc.skip -Dmaven. jacoco.skip -e.