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 b31fe566ddc Remove useless single and broadcast table index name 
rewrite in IndexToken (#31173)
b31fe566ddc is described below

commit b31fe566ddc992b52a3676a196d945be7c96d6e6
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Thu May 9 13:44:16 2024 +0800

    Remove useless single and broadcast table index name rewrite in IndexToken 
(#31173)
---
 .../context/ShardingSQLRewriteContextDecorator.java  | 17 +++++++++++++----
 .../sharding/rewrite/token/pojo/IndexToken.java      |  7 +++++--
 .../engine/scenario/ShardingSQLRewriterIT.java       |  6 ++++--
 .../scenario/sharding/case/ddl/alter-table.xml       |  6 +++---
 .../scenario/sharding/case/ddl/create-table.xml      |  6 +++---
 .../resources/scenario/sharding/case/ddl/index.xml   | 20 ++++++++++++++++++++
 6 files changed, 48 insertions(+), 14 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 664700a5531..5e84a146a88 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
@@ -20,7 +20,9 @@ package org.apache.shardingsphere.sharding.rewrite.context;
 import lombok.Setter;
 import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import 
org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
+import 
org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterIndexStatementContext;
+import 
org.apache.shardingsphere.infra.binder.context.statement.ddl.DropIndexStatementContext;
+import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
 import 
org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContextDecorator;
@@ -30,6 +32,7 @@ import 
org.apache.shardingsphere.sharding.constant.ShardingOrder;
 import 
org.apache.shardingsphere.sharding.rewrite.parameter.ShardingParameterRewriterBuilder;
 import 
org.apache.shardingsphere.sharding.rewrite.token.ShardingTokenGenerateBuilder;
 import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 
 import java.util.Collection;
 
@@ -43,7 +46,7 @@ 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)) {
+        if (!isAlterOrDropIndexStatement(sqlStatementContext) && 
!containsShardingTable(shardingRule, sqlStatementContext)) {
             return;
         }
         if (!sqlRewriteContext.getParameters().isEmpty()) {
@@ -54,9 +57,15 @@ public final class ShardingSQLRewriteContextDecorator 
implements SQLRewriteConte
         sqlRewriteContext.addSQLTokenGenerators(new 
ShardingTokenGenerateBuilder(shardingRule, routeContext, 
sqlStatementContext).getSQLTokenGenerators());
     }
     
+    private boolean isAlterOrDropIndexStatement(final SQLStatementContext 
sqlStatementContext) {
+        return sqlStatementContext instanceof AlterIndexStatementContext || 
sqlStatementContext instanceof DropIndexStatementContext;
+    }
+    
     private boolean containsShardingTable(final ShardingRule shardingRule, 
final SQLStatementContext sqlStatementContext) {
-        for (String each : 
sqlStatementContext.getTablesContext().getTableNames()) {
-            if (shardingRule.findShardingTable(each).isPresent()) {
+        Collection<SimpleTableSegment> tableSegments =
+                sqlStatementContext instanceof TableAvailable ? 
((TableAvailable) sqlStatementContext).getAllTables() : 
sqlStatementContext.getTablesContext().getSimpleTableSegments();
+        for (SimpleTableSegment each : tableSegments) {
+            if 
(shardingRule.isShardingTable(each.getTableName().getIdentifier().getValue())) {
                 return true;
             }
         }
diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java
index f1a6c078238..4278697febf 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java
@@ -69,9 +69,12 @@ public final class IndexToken extends SQLToken implements 
Substitutable, RouteUn
     }
     
     private String getIndexValue(final RouteUnit routeUnit) {
+        Optional<String> logicTableName = 
findLogicTableNameFromMetaData(identifier.getValue());
+        if (logicTableName.isPresent() && 
!shardingRule.isShardingTable(logicTableName.get())) {
+            return identifier.getValue();
+        }
         Map<String, String> logicAndActualTables = 
TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, 
shardingRule);
-        String actualTableName = 
findLogicTableNameFromMetaData(identifier.getValue()).map(logicAndActualTables::get)
-                .orElseGet(() -> 
logicAndActualTables.values().stream().findFirst().orElse(null));
+        String actualTableName = 
logicTableName.map(logicAndActualTables::get).orElseGet(() -> 
logicAndActualTables.isEmpty() ? null : 
logicAndActualTables.values().iterator().next());
         return IndexMetaDataUtils.getActualIndexName(identifier.getValue(), 
actualTableName);
     }
     
diff --git 
a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java
 
b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java
index 7a66e736c34..83789a0b63d 100644
--- 
a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java
+++ 
b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/scenario/ShardingSQLRewriterIT.java
@@ -83,12 +83,14 @@ class ShardingSQLRewriterIT extends SQLRewriterIT {
                 new ShardingSphereColumn("content", Types.VARCHAR, false, 
false, false, true, false, false)), Collections.emptyList(), 
Collections.emptyList()));
         tables.put("t_single", new ShardingSphereTable("t_single", 
Arrays.asList(
                 new ShardingSphereColumn("id", Types.INTEGER, false, false, 
false, true, false, false),
-                new ShardingSphereColumn("account_id", Types.INTEGER, false, 
false, false, true, false, false)), Collections.emptyList(), 
Collections.emptyList()));
+                new ShardingSphereColumn("account_id", Types.INTEGER, false, 
false, false, true, false, false)), Collections.singletonList(new 
ShardingSphereIndex("single_id_idx")),
+                Collections.emptyList()));
         tables.put("t_single_extend", new 
ShardingSphereTable("t_single_extend", Collections.singletonList(
                 new ShardingSphereColumn("id", Types.INTEGER, false, false, 
false, true, false, false)), Collections.emptyList(), Collections.emptyList()));
         tables.put("t_config", new ShardingSphereTable("t_config", 
Arrays.asList(
                 new ShardingSphereColumn("id", Types.INTEGER, false, false, 
false, true, false, false),
-                new ShardingSphereColumn("account_id", Types.INTEGER, false, 
false, false, true, false, false)), Collections.emptyList(), 
Collections.emptyList()));
+                new ShardingSphereColumn("account_id", Types.INTEGER, false, 
false, false, true, false, false)), Collections.singletonList(new 
ShardingSphereIndex("broadcast_id_idx")),
+                Collections.emptyList()));
         tables.put("T_ROLE", new ShardingSphereTable("T_ROLE", Arrays.asList(
                 new ShardingSphereColumn("id", Types.INTEGER, false, false, 
false, true, false, false),
                 new ShardingSphereColumn("ROLE_ID", Types.INTEGER, false, 
false, false, true, false, false)), Collections.emptyList(), 
Collections.emptyList()));
diff --git 
a/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/alter-table.xml
 
b/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/alter-table.xml
index e21a4b457e4..3e78878bc49 100644
--- 
a/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/alter-table.xml
+++ 
b/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/alter-table.xml
@@ -55,7 +55,7 @@
     </rewrite-assertion>
     <rewrite-assertion 
id="alter_table_with_broadcast_table_with_add_foreign_constraint_for" 
db-types="MySQL,PostgreSQL,openGauss">
         <input sql="ALTER TABLE t_config ADD CONSTRAINT t_config_fk FOREIGN 
KEY (order_id) REFERENCES t_order_type (order_id)" />
-        <output sql="ALTER TABLE t_config ADD CONSTRAINT t_config_fk_t_config 
FOREIGN KEY (order_id) REFERENCES t_order_type (order_id)" />
+        <output sql="ALTER TABLE t_config ADD CONSTRAINT t_config_fk FOREIGN 
KEY (order_id) REFERENCES t_order_type (order_id)" />
     </rewrite-assertion>
     <rewrite-assertion 
id="alter_table_with_single_and_single_data_node_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN 
KEY (order_id) REFERENCES t_order (order_id)" />
@@ -63,11 +63,11 @@
     </rewrite-assertion>
     <rewrite-assertion 
id="alter_table_with_single_and_broadcast_table_with_add_foreign_constraint" 
db-types="MySQL,PostgreSQL,openGauss">
         <input sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN 
KEY (order_id) REFERENCES t_config (order_id)" />
-        <output sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk_t_single 
FOREIGN KEY (order_id) REFERENCES t_config (order_id)" />
+        <output sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN 
KEY (order_id) REFERENCES t_config (order_id)" />
     </rewrite-assertion>
     <rewrite-assertion 
id="alter_table_with_single_table_with_add_foreign_constraint" 
db-types="MySQL,PostgreSQL,openGauss">
         <input sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN 
KEY (order_id) REFERENCES t_single_extend (order_id)" />
-        <output sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk_t_single 
FOREIGN KEY (order_id) REFERENCES t_single_extend (order_id)" />
+        <output sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN 
KEY (order_id) REFERENCES t_single_extend (order_id)" />
     </rewrite-assertion>
     <rewrite-assertion id="alter_table_with_single_table_with_rename_table" 
db-types="MySQL,PostgreSQL,openGauss">
         <input sql="ALTER TABLE t_single RENAME TO t_single_extend" />
diff --git 
a/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/create-table.xml
 
b/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/create-table.xml
index 2f52ce9119c..842faf0edb1 100644
--- 
a/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/create-table.xml
+++ 
b/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/create-table.xml
@@ -59,11 +59,11 @@
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_broadcast_table_with_add_foreign_constraint" 
db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_config_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_new_fk FOREIGN KEY (order_id) REFERENCES t_order_new_type 
(order_id))" />
-        <output sql="CREATE TABLE t_config_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_new_fk_t_config_new FOREIGN KEY (order_id) REFERENCES 
t_order_new_type (order_id))" />
+        <output sql="CREATE TABLE t_config_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_new_fk FOREIGN KEY (order_id) REFERENCES t_order_new_type 
(order_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_broadcast_and_single_table_with_add_foreign_constraint" 
db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_config_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_new_fk FOREIGN KEY (order_id) REFERENCES t_single_new 
(order_id))" />
-        <output sql="CREATE TABLE t_config_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_new_fk_t_config_new FOREIGN KEY (order_id) REFERENCES 
t_single_new (order_id))" />
+        <output sql="CREATE TABLE t_config_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_new_fk FOREIGN KEY (order_id) REFERENCES t_single_new 
(order_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_single_and_single_data_node_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_single_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_new_fk FOREIGN KEY (order_id) REFERENCES t_order_new 
(order_id))" />
@@ -76,7 +76,7 @@
     <!--    </rewrite-assertion>-->
     <rewrite-assertion 
id="create_table_with_single_table_with_add_foreign_constraint" 
db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_single_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_new_fk FOREIGN KEY (order_id) REFERENCES t_single_extend 
(order_id))" />
-        <output sql="CREATE TABLE t_single_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_new_fk_t_single_new FOREIGN KEY (order_id) REFERENCES 
t_single_extend (order_id))" />
+        <output sql="CREATE TABLE t_single_new(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_new_fk FOREIGN KEY (order_id) REFERENCES t_single_extend 
(order_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_multi_data_node_with_storage_parameter" 
db-types="openGauss">
         <input sql="CREATE TABLE t_account_detail_new (order_id INT,account_id 
INT) WITH (FILLFACTOR = 80, ORIENTATION=ROW)" />
diff --git 
a/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/index.xml 
b/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/index.xml
index 8a258425641..e2ac3063cc3 100644
--- a/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/index.xml
+++ b/test/it/rewriter/src/test/resources/scenario/sharding/case/ddl/index.xml
@@ -71,11 +71,31 @@
         <output sql="DROP INDEX status_idx_exist_t_account_1 ON t_account_1" />
     </rewrite-assertion>
     
+    <rewrite-assertion id="drop_index_with_single_table_for_mysql" 
db-types="MySQL">
+        <input sql="DROP INDEX single_id_idx ON t_single" />
+        <output sql="DROP INDEX single_id_idx ON t_single" />
+    </rewrite-assertion>
+    
+    <rewrite-assertion id="drop_index_with_broadcast_table_for_mysql" 
db-types="MySQL">
+        <input sql="DROP INDEX broadcast_id_idx ON t_config" />
+        <output sql="DROP INDEX broadcast_id_idx ON t_config" />
+    </rewrite-assertion>
+    
     <rewrite-assertion id="drop_index_for_postgresql" 
db-types="PostgreSQL,openGauss">
         <input sql="DROP INDEX status_idx_exist" />
         <output sql="DROP INDEX status_idx_exist_t_account_0" />
         <output sql="DROP INDEX status_idx_exist_t_account_1" />
     </rewrite-assertion>
+    
+    <rewrite-assertion id="drop_index_with_single_table_for_postgresql" 
db-types="PostgreSQL,openGauss">
+        <input sql="DROP INDEX single_id_idx" />
+        <output sql="DROP INDEX single_id_idx" />
+    </rewrite-assertion>
+    
+    <rewrite-assertion id="drop_index_with_broadcast_table_for_postgresql" 
db-types="PostgreSQL,openGauss">
+        <input sql="DROP INDEX broadcast_id_idx" />
+        <output sql="DROP INDEX broadcast_id_idx" />
+    </rewrite-assertion>
 
     <rewrite-assertion id="drop_index_with_schema_for_postgresql" 
db-types="PostgreSQL,openGauss">
         <input sql="DROP INDEX public.status_idx_exist" />

Reply via email to