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

duanzhengqiang 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 ac4a93b  fix: fix create table with foreign key error. (#15882)
ac4a93b is described below

commit ac4a93b7baebeee33dc4bfbf84f675534a94f778
Author: ICannerxy <[email protected]>
AuthorDate: Wed Mar 9 10:36:50 2022 +0800

    fix: fix create table with foreign key error. (#15882)
    
    * fix: fix create table with foreign key error.
    
    * test: add unit test and integration test
    
    * fix: remove unused package
---
 .../statement/ddl/CreateTableStatementContext.java | 13 +++++++-
 .../ddl/CreateTableStatementContextTest.java       |  5 +++
 .../resources/scenario/sharding/case/create.xml    | 36 +++++++++++-----------
 3 files changed, 35 insertions(+), 19 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContext.java
 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContext.java
index e0d2f0f..140ba58 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContext.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContext.java
@@ -20,10 +20,12 @@ package 
org.apache.shardingsphere.infra.binder.statement.ddl;
 import lombok.Getter;
 import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
 import 
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
+import org.apache.shardingsphere.infra.binder.type.ConstraintAvailable;
 import org.apache.shardingsphere.infra.binder.type.IndexAvailable;
 import org.apache.shardingsphere.infra.binder.type.TableAvailable;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.ColumnDefinitionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintDefinitionSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
@@ -35,7 +37,7 @@ import java.util.LinkedList;
  * Create table statement context.
  */
 @Getter
-public final class CreateTableStatementContext extends 
CommonSQLStatementContext<CreateTableStatement> implements TableAvailable, 
IndexAvailable {
+public final class CreateTableStatementContext extends 
CommonSQLStatementContext<CreateTableStatement> implements TableAvailable, 
IndexAvailable, ConstraintAvailable {
     
     private final TablesContext tablesContext;
     
@@ -67,4 +69,13 @@ public final class CreateTableStatementContext extends 
CommonSQLStatementContext
         }
         return result;
     }
+
+    @Override
+    public Collection<ConstraintSegment> getConstraints() {
+        Collection<ConstraintSegment> result = new LinkedList<>();
+        for (ConstraintDefinitionSegment each : 
getSqlStatement().getConstraintDefinitions()) {
+            each.getConstraintName().ifPresent(result::add);
+        }
+        return result;
+    }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContextTest.java
 
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContextTest.java
index 72acb0e..af8d9f4 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContextTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContextTest.java
@@ -18,8 +18,10 @@
 package org.apache.shardingsphere.infra.binder.statement.ddl;
 
 import 
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
+import org.apache.shardingsphere.infra.binder.type.ConstraintAvailable;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.ColumnDefinitionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintDefinitionSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
@@ -75,15 +77,18 @@ public final class CreateTableStatementContextTest {
         SimpleTableSegment table = new SimpleTableSegment(new 
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
         when(createTableStatement.getTable()).thenReturn(table);
         assertThat(actual, instanceOf(CommonSQLStatementContext.class));
+        assertThat(actual, instanceOf(ConstraintAvailable.class));
         ColumnDefinitionSegment columnDefinition = 
mock(ColumnDefinitionSegment.class);
         
when(columnDefinition.getReferencedTables()).thenReturn(Collections.singletonList(table));
         
when(createTableStatement.getColumnDefinitions()).thenReturn(Collections.singletonList(columnDefinition));
         ConstraintDefinitionSegment constraintDefinition = 
mock(ConstraintDefinitionSegment.class);
+        
when(constraintDefinition.getConstraintName()).thenReturn(Optional.of(new 
ConstraintSegment(0, 0, new IdentifierValue("fk_1"))));
         
when(constraintDefinition.getReferencedTable()).thenReturn(Optional.of(table));
         
when(createTableStatement.getConstraintDefinitions()).thenReturn(Collections.singletonList(constraintDefinition));
         assertThat(actual.getSqlStatement(), is(createTableStatement));
         assertThat(actual.getAllTables().stream().map(each -> 
each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()), 
is(Arrays.asList("tbl_1", "tbl_1", "tbl_1")));
         when(constraintDefinition.getIndexName()).thenReturn(Optional.of(new 
IndexSegment(0, 0, new IdentifierValue("index_1"))));
         assertThat(actual.getIndexes().stream().map(each -> 
each.getIdentifier().getValue()).collect(Collectors.toList()), 
is(Collections.singletonList("index_1")));
+        assertThat(actual.getConstraints().stream().map(each -> 
each.getIdentifier().getValue()).collect(Collectors.toList()), 
is(Collections.singletonList("fk_1")));
     }
 }
diff --git 
a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/create.xml
 
b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/create.xml
index abb6442..077bbf2 100644
--- 
a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/create.xml
+++ 
b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/create.xml
@@ -19,63 +19,63 @@
 <rewrite-assertions yaml-rule="scenario/sharding/config/sharding-rule.yaml">
     <rewrite-assertion 
id="create_table_with_single_data_node_binding_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_order(order_id INT PRIMARY KEY, CONSTRAINT 
t_order_fk FOREIGN KEY (order_id) REFERENCES t_order_item (order_id))" />
-        <output sql="CREATE TABLE t_order_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_order_fk FOREIGN KEY (order_id) REFERENCES t_order_item_0 
(order_id))" />
+        <output sql="CREATE TABLE t_order_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_order_fk_t_order_0 FOREIGN KEY (order_id) REFERENCES 
t_order_item_0 (order_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_single_data_node_unbinding_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_order(order_id INT PRIMARY KEY, CONSTRAINT 
t_order_fk FOREIGN KEY (order_id) REFERENCES t_order_extend (order_id))" />
-        <output sql="CREATE TABLE t_order_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_order_fk FOREIGN KEY (order_id) REFERENCES t_order_extend_0 
(order_id))" />
+        <output sql="CREATE TABLE t_order_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_order_fk_t_order_0 FOREIGN KEY (order_id) REFERENCES 
t_order_extend_0 (order_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_multi_data_node_and_single_data_node_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_account_detail(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_order 
(account_id))" />
-        <output sql="CREATE TABLE t_account_detail_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_order_0 
(account_id))" />
-        <output sql="CREATE TABLE t_account_detail_1(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_order_0 
(account_id))" />
+        <output sql="CREATE TABLE t_account_detail_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk_t_account_detail_0 FOREIGN KEY (account_id) REFERENCES 
t_order_0 (account_id))" />
+        <output sql="CREATE TABLE t_account_detail_1(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk_t_account_detail_1 FOREIGN KEY (account_id) REFERENCES 
t_order_0 (account_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_multi_data_node_binding_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_account_detail(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_account_detail 
(account_id))" />
-        <output sql="CREATE TABLE t_account_detail_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_account_detail_0 
(account_id))" />
-        <output sql="CREATE TABLE t_account_detail_1(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_account_detail_1 
(account_id))" />
+        <output sql="CREATE TABLE t_account_detail_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk_t_account_detail_0 FOREIGN KEY (account_id) REFERENCES 
t_account_detail_0 (account_id))" />
+        <output sql="CREATE TABLE t_account_detail_1(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk_t_account_detail_1 FOREIGN KEY (account_id) REFERENCES 
t_account_detail_1 (account_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_single_data_node_and_broadcast_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_order(order_id INT PRIMARY KEY, CONSTRAINT 
t_order_fk FOREIGN KEY (order_id) REFERENCES t_config (order_id))" />
-        <output sql="CREATE TABLE t_order_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_order_fk FOREIGN KEY (order_id) REFERENCES t_config (order_id))" />
+        <output sql="CREATE TABLE t_order_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_order_fk_t_order_0 FOREIGN KEY (order_id) REFERENCES t_config 
(order_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_multi_data_node_and_broadcast_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_account_detail(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_config 
(account_id))" />
-        <output sql="CREATE TABLE t_account_detail_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_config 
(account_id))" />
-        <output sql="CREATE TABLE t_account_detail_1(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_config 
(account_id))" />
+        <output sql="CREATE TABLE t_account_detail_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk_t_account_detail_0 FOREIGN KEY (account_id) REFERENCES 
t_config (account_id))" />
+        <output sql="CREATE TABLE t_account_detail_1(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk_t_account_detail_1 FOREIGN KEY (account_id) REFERENCES 
t_config (account_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_single_data_node_and_single_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_order(order_id INT PRIMARY KEY, CONSTRAINT 
t_order_fk FOREIGN KEY (order_id) REFERENCES t_single (order_id))" />
-        <output sql="CREATE TABLE t_order_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_order_fk FOREIGN KEY (order_id) REFERENCES t_single (order_id))" />
+        <output sql="CREATE TABLE t_order_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_order_fk_t_order_0 FOREIGN KEY (order_id) REFERENCES t_single 
(order_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_multi_data_node_and_single_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_account_detail(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_single 
(account_id))" />
-        <output sql="CREATE TABLE t_account_detail_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_single 
(account_id))" />
-        <output sql="CREATE TABLE t_account_detail_1(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk FOREIGN KEY (account_id) REFERENCES t_single 
(account_id))" />
+        <output sql="CREATE TABLE t_account_detail_0(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk_t_account_detail_0 FOREIGN KEY (account_id) REFERENCES 
t_single (account_id))" />
+        <output sql="CREATE TABLE t_account_detail_1(order_id INT PRIMARY KEY, 
CONSTRAINT t_account_fk_t_account_detail_1 FOREIGN KEY (account_id) REFERENCES 
t_single (account_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_broadcast_and_single_data_node_table_with_add_foreign_constraint"
 db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_config(order_id INT PRIMARY KEY, CONSTRAINT 
t_config_fk FOREIGN KEY (order_id) REFERENCES t_order (order_id))" />
-        <output sql="CREATE TABLE t_config(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_fk FOREIGN KEY (order_id) REFERENCES t_order_0 (order_id))" 
/>
+        <output sql="CREATE TABLE t_config(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_fk_t_config FOREIGN KEY (order_id) REFERENCES t_order_0 
(order_id))" />
     </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(order_id INT PRIMARY KEY, CONSTRAINT 
t_config_fk FOREIGN KEY (order_id) REFERENCES t_order_type (order_id))" />
-        <output sql="CREATE TABLE t_config(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_fk FOREIGN KEY (order_id) REFERENCES t_order_type 
(order_id))" />
+        <output sql="CREATE TABLE t_config(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_fk_t_config FOREIGN KEY (order_id) REFERENCES t_order_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(order_id INT PRIMARY KEY, CONSTRAINT 
t_config_fk FOREIGN KEY (order_id) REFERENCES t_single (order_id))" />
-        <output sql="CREATE TABLE t_config(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_fk FOREIGN KEY (order_id) REFERENCES t_single (order_id))" 
/>
+        <output sql="CREATE TABLE t_config(order_id INT PRIMARY KEY, 
CONSTRAINT t_config_fk_t_config FOREIGN KEY (order_id) REFERENCES t_single 
(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(order_id INT PRIMARY KEY, CONSTRAINT 
t_single_fk FOREIGN KEY (order_id) REFERENCES t_order (order_id))" />
-        <output sql="CREATE TABLE t_single(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_fk FOREIGN KEY (order_id) REFERENCES t_order_0 (order_id))" 
/>
+        <output sql="CREATE TABLE t_single(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_fk_t_single FOREIGN KEY (order_id) REFERENCES t_order_0 
(order_id))" />
     </rewrite-assertion>
     <rewrite-assertion 
id="create_table_with_single_and_broadcast_table_with_add_foreign_constraint" 
db-types="MySQL,PostgreSQL,openGauss">
         <input sql="CREATE TABLE t_single(order_id INT PRIMARY KEY, CONSTRAINT 
t_single_fk FOREIGN KEY (order_id) REFERENCES t_config (order_id))" />
-        <output sql="CREATE TABLE t_single(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_fk FOREIGN KEY (order_id) REFERENCES t_config (order_id))" 
/>
+        <output sql="CREATE TABLE t_single(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_fk_t_single FOREIGN KEY (order_id) REFERENCES t_config 
(order_id))" />
     </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(order_id INT PRIMARY KEY, CONSTRAINT 
t_single_fk FOREIGN KEY (order_id) REFERENCES t_single_extend (order_id))" />
-        <output sql="CREATE TABLE t_single(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_fk FOREIGN KEY (order_id) REFERENCES t_single_extend 
(order_id))" />
+        <output sql="CREATE TABLE t_single(order_id INT PRIMARY KEY, 
CONSTRAINT t_single_fk_t_single 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 (order_id INT,account_id 
INT) WITH (FILLFACTOR = 80, ORIENTATION=ROW)" />

Reply via email to