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

chengzhang 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 a203da93bdb Skip sharding sql rewrite when insert statement not 
contains sharding table (#29091)
a203da93bdb is described below

commit a203da93bdbb4f8fcc777b57c5d0f637229584f8
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Mon Nov 20 19:18:01 2023 +0800

    Skip sharding sql rewrite when insert statement not contains sharding table 
(#29091)
---
 .../context/ShardingSQLRewriteContextDecorator.java   | 19 +++++++++++++++++--
 .../ShardingSQLRewriteContextDecoratorTest.java       | 14 ++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
index 9872d5a4ea8..eb0e9688b7f 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
@@ -18,6 +18,8 @@
 package org.apache.shardingsphere.sharding.rewrite.context;
 
 import lombok.Setter;
+import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import 
org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
 import 
org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContextDecorator;
@@ -38,12 +40,25 @@ public final class ShardingSQLRewriteContextDecorator 
implements SQLRewriteConte
     
     @Override
     public void decorate(final ShardingRule shardingRule, final 
ConfigurationProperties props, final SQLRewriteContext sqlRewriteContext, final 
RouteContext routeContext) {
+        SQLStatementContext sqlStatementContext = 
sqlRewriteContext.getSqlStatementContext();
+        if (sqlStatementContext instanceof InsertStatementContext && 
!containsShardingTable(shardingRule, sqlStatementContext)) {
+            return;
+        }
         if (!sqlRewriteContext.getParameters().isEmpty()) {
             Collection<ParameterRewriter> parameterRewriters =
-                    new ShardingParameterRewriterBuilder(shardingRule, 
routeContext, sqlRewriteContext.getDatabase().getSchemas(), 
sqlRewriteContext.getSqlStatementContext()).getParameterRewriters();
+                    new ShardingParameterRewriterBuilder(shardingRule, 
routeContext, sqlRewriteContext.getDatabase().getSchemas(), 
sqlStatementContext).getParameterRewriters();
             rewriteParameters(sqlRewriteContext, parameterRewriters);
         }
-        sqlRewriteContext.addSQLTokenGenerators(new 
ShardingTokenGenerateBuilder(shardingRule, routeContext, 
sqlRewriteContext.getSqlStatementContext()).getSQLTokenGenerators());
+        sqlRewriteContext.addSQLTokenGenerators(new 
ShardingTokenGenerateBuilder(shardingRule, routeContext, 
sqlStatementContext).getSQLTokenGenerators());
+    }
+    
+    private boolean containsShardingTable(final ShardingRule shardingRule, 
final SQLStatementContext sqlStatementContext) {
+        for (String each : 
sqlStatementContext.getTablesContext().getTableNames()) {
+            if (shardingRule.findTableRule(each).isPresent()) {
+                return true;
+            }
+        }
+        return false;
     }
     
     private void rewriteParameters(final SQLRewriteContext sqlRewriteContext, 
final Collection<ParameterRewriter> parameterRewriters) {
diff --git 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
index 221f51abb7a..7ad1b58e9f3 100644
--- 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
+++ 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.sharding.rewrite.context;
 
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import 
org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
@@ -26,6 +27,7 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
 import org.junit.jupiter.api.Test;
 
 import java.util.Collections;
+import java.util.Optional;
 
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -43,4 +45,16 @@ class ShardingSQLRewriteContextDecoratorTest {
         new 
ShardingSQLRewriteContextDecorator().decorate(mock(ShardingRule.class), 
mock(ConfigurationProperties.class), sqlRewriteContext, 
mock(RouteContext.class));
         assertTrue(sqlRewriteContext.getSqlTokens().isEmpty());
     }
+    
+    @Test
+    void assertDecorateWhenInsertStatementNotContainsShardingTable() {
+        SQLRewriteContext sqlRewriteContext = mock(SQLRewriteContext.class);
+        InsertStatementContext insertStatementContext = 
mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
+        
when(insertStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("t_order"));
+        
when(sqlRewriteContext.getSqlStatementContext()).thenReturn(insertStatementContext);
+        ShardingRule shardingRule = mock(ShardingRule.class);
+        
when(shardingRule.findTableRule("t_order")).thenReturn(Optional.empty());
+        new ShardingSQLRewriteContextDecorator().decorate(shardingRule, 
mock(ConfigurationProperties.class), sqlRewriteContext, 
mock(RouteContext.class));
+        assertTrue(sqlRewriteContext.getSqlTokens().isEmpty());
+    }
 }

Reply via email to