You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/10/01 17:07:27 UTC

(shardingsphere) branch master updated: Fix duplicate key generator check when altering key generate strategy (#39997)

This is an automated email from the ASF dual-hosted git repository.

terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 00bd4d6e5fb Fix duplicate key generator check when altering key generate strategy (#39997)
00bd4d6e5fb is described below

commit 00bd4d6e5fb7d6aea3fe08f22cf4f4da15e507be
Author: Eunbin Son <58...@users.noreply.github.com>
AuthorDate: Fri Oct 2 02:07:20 2026 +0900

    Fix duplicate key generator check when altering key generate strategy (#39997)
---
 RELEASE-NOTES.md                                   |  1 +
 .../AlterShardingKeyGenerateStrategyExecutor.java  |  4 ++--
 ...terShardingKeyGenerateStrategyExecutorTest.java | 26 +++++++++++++++++++++-
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 69c43051cb2..de00a49d5a2 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -55,6 +55,7 @@
 1. DistSQL: Fix `CREATE READWRITE_SPLITTING RULE IF NOT EXISTS` failing for an existing rule name - [#39371](https://github.com/apache/shardingsphere/pull/39371)
 1. DistSQL: Match mask table names case-insensitively in create and alter executors - [#39361](https://github.com/apache/shardingsphere/pull/39361)
 1. DistSQL: Fix shadow DistSQL rejecting unquoted `SQL_HINT` algorithm type - [#39873](https://github.com/apache/shardingsphere/pull/39873)
+1. DistSQL: Fix duplicate key generator check when altering key generate strategy - [#39997](https://github.com/apache/shardingsphere/pull/39997)
 1. JDBC: Fix stale generated values leaking into prepared statement executeBatch calls without pending batches - [#38160](https://github.com/apache/shardingsphere/pull/38160)
 1. JDBC: Fix MySQL-compatible typed string conversion for `ResultSet#getObject(index, Class<T>)` - [#38444](https://github.com/apache/shardingsphere/pull/38444)
 1. JDBC: Fix statement close invalidating live result sets of other statements on the same connection - [#39503](https://github.com/apache/shardingsphere/pull/39503)
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingKeyGenerateStrategyExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingKeyGenerateStrategyExecutor.java
index 4d2089b1d4a..6785e4e16e8 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingKeyGenerateStrategyExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingKeyGenerateStrategyExecutor.java
@@ -65,9 +65,9 @@ public final class AlterShardingKeyGenerateStrategyExecutor
     private void checkDuplicateGeneratedKeyGenerator(final AlterShardingKeyGenerateStrategyStatement sqlStatement) {
         String keyGeneratorName = ShardingKeyGenerateStrategyStatementConverter.getKeyGeneratorName(sqlStatement.getName(), sqlStatement.getKeyGenerateStrategySegment());
         String currentKeyGeneratorName = rule.getConfiguration().getKeyGenerateStrategies().get(sqlStatement.getName()).getKeyGeneratorName();
-        boolean containsSameNameKeyGenerator = rule.getConfiguration().getKeyGenerators().containsKey(sqlStatement.getName());
+        boolean containsSameNameKeyGenerator = rule.getConfiguration().getKeyGenerators().containsKey(keyGeneratorName);
         ShardingSpherePreconditions.checkState(!containsSameNameKeyGenerator || keyGeneratorName.equals(currentKeyGeneratorName),
-                () -> new DuplicateRuleException("key generator", database.getName(), Collections.singleton(sqlStatement.getName())));
+                () -> new DuplicateRuleException("key generator", database.getName(), Collections.singleton(keyGeneratorName)));
     }
     
     private void checkReferencedKeyGenerator(final String keyGeneratorName) {
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingKeyGenerateStrategyExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingKeyGenerateStrategyExecutorTest.java
index 6f5a2246868..63b7b43921d 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingKeyGenerateStrategyExecutorTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingKeyGenerateStrategyExecutorTest.java
@@ -37,6 +37,7 @@ import java.util.Properties;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -74,13 +75,36 @@ class AlterShardingKeyGenerateStrategyExecutorTest {
     
     @Test
     void assertCheckBeforeUpdateWithConflictingGeneratedKeyGenerator() {
+        ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
+        currentRuleConfig.getKeyGenerateStrategies().put("order_strategy", new ColumnKeyGenerateStrategiesRuleConfiguration("old_generator", "t_order", "order_id"));
+        currentRuleConfig.getKeyGenerators().put("order_strategy_snowflake", new AlgorithmConfiguration("UUID", new Properties()));
+        executor.setRule(mockRule(currentRuleConfig));
+        AlterShardingKeyGenerateStrategyStatement sqlStatement = new AlterShardingKeyGenerateStrategyStatement("order_strategy",
+                new ColumnKeyGenerateStrategyDefinitionSegment("t_order", "order_id", null, new AlgorithmSegment("SNOWFLAKE", new Properties())));
+        DuplicateRuleException actual = assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement));
+        assertThat(actual.getMessage(), is("Duplicate key generator rule names 'order_strategy_snowflake' in database 'foo_db'."));
+    }
+    
+    @Test
+    void assertCheckBeforeUpdateWithStrategyNamedKeyGenerator() {
         ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
         currentRuleConfig.getKeyGenerateStrategies().put("order_strategy", new ColumnKeyGenerateStrategiesRuleConfiguration("old_generator", "t_order", "order_id"));
         currentRuleConfig.getKeyGenerators().put("order_strategy", new AlgorithmConfiguration("UUID", new Properties()));
         executor.setRule(mockRule(currentRuleConfig));
         AlterShardingKeyGenerateStrategyStatement sqlStatement = new AlterShardingKeyGenerateStrategyStatement("order_strategy",
                 new ColumnKeyGenerateStrategyDefinitionSegment("t_order", "order_id", null, new AlgorithmSegment("SNOWFLAKE", new Properties())));
-        assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement));
+        assertDoesNotThrow(() -> executor.checkBeforeUpdate(sqlStatement));
+    }
+    
+    @Test
+    void assertCheckBeforeUpdateWithCurrentGeneratedKeyGenerator() {
+        ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
+        currentRuleConfig.getKeyGenerateStrategies().put("order_strategy", new ColumnKeyGenerateStrategiesRuleConfiguration("order_strategy_snowflake", "t_order", "order_id"));
+        currentRuleConfig.getKeyGenerators().put("order_strategy_snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
+        executor.setRule(mockRule(currentRuleConfig));
+        AlterShardingKeyGenerateStrategyStatement sqlStatement = new AlterShardingKeyGenerateStrategyStatement("order_strategy",
+                new ColumnKeyGenerateStrategyDefinitionSegment("t_order", "order_id", null, new AlgorithmSegment("SNOWFLAKE", new Properties())));
+        assertDoesNotThrow(() -> executor.checkBeforeUpdate(sqlStatement));
     }
     
     @Test