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

zhonghongsheng 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 5ecfc421647 Add CreateTableSQLGenerateException (#20313)
5ecfc421647 is described below

commit 5ecfc421647ffe81c96aa0a4d5b9750273bbfe4c
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Aug 20 00:02:53 2022 +0800

    Add CreateTableSQLGenerateException (#20313)
---
 .../user-manual/error-code/sql-error-code.cn.md    |  1 +
 .../user-manual/error-code/sql-error-code.en.md    |  1 +
 .../DropMigrationSourceResourceStatement.java      |  1 +
 .../spi/ddlgenerator/CreateTableSQLGenerator.java  |  6 +-
 .../CreateTableSQLGeneratorFactory.java            | 10 ++-
 .../CreateTableSQLGeneratorFactoryTest.java        |  8 ++-
 .../fixture/CreateTableSQLGeneratorFixture.java    |  2 +-
 .../exception/CreateTableSQLGenerateException.java | 20 +++---
 .../metadata/generator/PipelineDDLGenerator.java   | 72 +++++++---------------
 .../datasource/AbstractDataSourcePreparer.java     |  2 +-
 .../ddlgenerator/MySQLCreateTableSQLGenerator.java |  6 +-
 .../OpenGaussCreateTableSQLGenerator.java          |  6 +-
 .../PostgreSQLCreateTableSQLGenerator.java         |  2 +-
 .../cases/general/CreateTableSQLGeneratorIT.java   |  6 +-
 14 files changed, 57 insertions(+), 86 deletions(-)

diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md 
b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index c17c7d37cd2..a74821ba293 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -20,5 +20,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | HY000     | 14001       | The table \`%s\` of schema \`%s\` lock wait 
timeout of %s ms exceeded |
 | HY000     | 15000       | Work ID assigned failed, which can not exceed 1024 
|
 | HY000     | 16000       | Can not find pipeline job \`%s\` |
+| HY000     | 16001       | Failed to get DDL for table \`%s\` |
 | HY004     | 25000       | Shadow column \`%s\` of table \`%s\` does not 
support \`%s\` type |
 | 42000     | 30000       | Unknown exception: %s |
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md 
b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 39ee7af2df0..a1ce078aac4 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -20,5 +20,6 @@ SQL error codes provide by standard `SQL State`, `Vendor 
Code` and `Reason`, whi
 | HY000     | 14001       | The table \`%s\` of schema \`%s\` lock wait 
timeout of %s ms exceeded |
 | HY000     | 15000       | Work ID assigned failed, which can not exceed 1024 
|
 | HY000     | 16000       | Can not find pipeline job \`%s\` |
+| HY000     | 16001       | Failed to get DDL for table \`%s\` |
 | HY004     | 25000       | Shadow column \`%s\` of table \`%s\` does not 
support \`%s\` type |
 | 42000     | 30000       | Unknown exception: %s |
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/migration/distsql/statement/DropMigrationSourceResourceStatement.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/migration/distsql/statement/DropMigrationSourceResourceStatement.java
index e043bf95f3f..e1f08846e85 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/migration/distsql/statement/DropMigrationSourceResourceStatement.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/migration/distsql/statement/DropMigrationSourceResourceStatement.java
@@ -27,6 +27,7 @@ import java.util.Collection;
  */
 @Getter
 public final class DropMigrationSourceResourceStatement extends 
UpdatableScalingRALStatement {
+    
     private final Collection<String> names;
     
     public DropMigrationSourceResourceStatement(final Collection<String> 
names) {
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGenerator.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGenerator.java
index a0177bec53e..f234f3a36e7 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGenerator.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGenerator.java
@@ -33,11 +33,11 @@ public interface CreateTableSQLGenerator extends TypedSPI {
     /**
     * Generate create table SQLs.
     * 
-    * @param tableName table name
-    * @param schemaName schema name
     * @param dataSource dataSource
+    * @param schemaName schema name
+    * @param tableName table name
     * @return generated SQLs
     * @throws SQLException SQL exception
     */
-    Collection<String> generate(String tableName, String schemaName, 
DataSource dataSource) throws SQLException;
+    Collection<String> generate(DataSource dataSource, String schemaName, 
String tableName) throws SQLException;
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGeneratorFactory.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGeneratorFactory.java
index 0cda16841ba..af350792e89 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGeneratorFactory.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/main/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGeneratorFactory.java
@@ -23,8 +23,6 @@ import 
org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
 
-import java.util.Optional;
-
 /**
  * Create table SQL generator factory.
  */
@@ -36,12 +34,12 @@ public final class CreateTableSQLGeneratorFactory {
     }
     
     /**
-     * Find instance of create table SQL generator.
+     * Get instance of create table SQL generator.
      *
      * @param databaseType database type
-     * @return found instance
+     * @return got instance
      */
-    public static Optional<CreateTableSQLGenerator> findInstance(final 
DatabaseType databaseType) {
-        return 
TypedSPIRegistry.findRegisteredService(CreateTableSQLGenerator.class, 
databaseType.getType());
+    public static CreateTableSQLGenerator getInstance(final DatabaseType 
databaseType) {
+        return 
TypedSPIRegistry.getRegisteredService(CreateTableSQLGenerator.class, 
databaseType.getType());
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/test/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGeneratorFactoryTest.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/test/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGeneratorFactoryTest.java
index 4ab75c5d45d..c39fd2c9c5b 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/test/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGeneratorFactoryTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/test/java/org/apache/shardingsphere/data/pipeline/spi/ddlgenerator/CreateTableSQLGeneratorFactoryTest.java
@@ -17,15 +17,17 @@
 
 package org.apache.shardingsphere.data.pipeline.spi.ddlgenerator;
 
+import 
org.apache.shardingsphere.data.pipeline.spi.fixture.CreateTableSQLGeneratorFixture;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeFactory;
 import org.junit.Test;
 
-import static org.junit.Assert.assertTrue;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
 
 public final class CreateTableSQLGeneratorFactoryTest {
     
     @Test
-    public void assertFindInstance() {
-        
assertTrue(CreateTableSQLGeneratorFactory.findInstance(DatabaseTypeFactory.getInstance("FIXTURE")).isPresent());
+    public void assertGetInstance() {
+        
assertThat(CreateTableSQLGeneratorFactory.getInstance(DatabaseTypeFactory.getInstance("FIXTURE")),
 instanceOf(CreateTableSQLGeneratorFixture.class));
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/test/java/org/apache/shardingsphere/data/pipeline/spi/fixture/CreateTableSQLGeneratorFixture.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/test/java/org/apache/shardingsphere/data/pipeline/spi/fixture/CreateTableSQLGeneratorFixture.java
index 90134675312..70ea2c469e2 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/test/java/org/apache/shardingsphere/data/pipeline/spi/fixture/CreateTableSQLGeneratorFixture.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-api/src/test/java/org/apache/shardingsphere/data/pipeline/spi/fixture/CreateTableSQLGeneratorFixture.java
@@ -26,7 +26,7 @@ import java.util.Collections;
 public final class CreateTableSQLGeneratorFixture implements 
CreateTableSQLGenerator {
     
     @Override
-    public Collection<String> generate(final String tableName, final String 
schemaName, final DataSource dataSource) {
+    public Collection<String> generate(final DataSource dataSource, final 
String schemaName, final String tableName) {
         return Collections.singletonList("");
     }
     
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/migration/distsql/statement/DropMigrationSourceResourceStatement.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/CreateTableSQLGenerateException.java
similarity index 57%
copy from 
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/migration/distsql/statement/DropMigrationSourceResourceStatement.java
copy to 
shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/CreateTableSQLGenerateException.java
index e043bf95f3f..9e0a4b30341 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/migration/distsql/statement/DropMigrationSourceResourceStatement.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/CreateTableSQLGenerateException.java
@@ -15,21 +15,19 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.migration.distsql.statement;
+package org.apache.shardingsphere.data.pipeline.core.exception;
 
-import lombok.Getter;
-import 
org.apache.shardingsphere.distsql.parser.statement.ral.scaling.UpdatableScalingRALStatement;
-
-import java.util.Collection;
+import 
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
+import 
org.apache.shardingsphere.infra.util.exception.sql.sqlstate.XOpenSQLState;
 
 /**
- * Drop resource statement.
+ * Create table SQL generate exception.
  */
-@Getter
-public final class DropMigrationSourceResourceStatement extends 
UpdatableScalingRALStatement {
-    private final Collection<String> names;
+public final class CreateTableSQLGenerateException extends 
ShardingSphereSQLException {
+    
+    private static final long serialVersionUID = -219467568498936298L;
     
-    public DropMigrationSourceResourceStatement(final Collection<String> 
names) {
-        this.names = names;
+    public CreateTableSQLGenerateException(final String tableName) {
+        super(XOpenSQLState.GENERAL_ERROR, 16001, "Failed to get DDL for table 
`%s`", tableName);
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/generator/PipelineDDLGenerator.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/generator/PipelineDDLGenerator.java
index 812dc1bbced..b45548fdaf3 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/generator/PipelineDDLGenerator.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/generator/PipelineDDLGenerator.java
@@ -20,7 +20,6 @@ package 
org.apache.shardingsphere.data.pipeline.core.metadata.generator;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
-import 
org.apache.shardingsphere.data.pipeline.spi.ddlgenerator.CreateTableSQLGenerator;
 import 
org.apache.shardingsphere.data.pipeline.spi.ddlgenerator.CreateTableSQLGeneratorFactory;
 import org.apache.shardingsphere.infra.binder.LogicSQL;
 import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
@@ -33,11 +32,8 @@ 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.infra.database.type.DatabaseType;
-import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.util.IndexMetaDataUtil;
 import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
-import 
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
-import 
org.apache.shardingsphere.infra.util.spi.exception.ServiceProviderNotFoundException;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
@@ -48,12 +44,10 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import javax.sql.DataSource;
 import java.sql.Connection;
 import java.sql.SQLException;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.TreeMap;
 
@@ -69,7 +63,7 @@ public final class PipelineDDLGenerator {
     private static final String SET_SEARCH_PATH_PREFIX = "set search_path";
     
     /**
-     * Generate logic ddl sql.
+     * Generate logic DDL.
      *
      * @param sourceDataSource source data source
      * @param databaseType database type
@@ -77,46 +71,20 @@ public final class PipelineDDLGenerator {
      * @param logicTableName table name
      * @param actualTableName actual table name
      * @param parserEngine parser engine
-     * @return ddl SQL
+     * @return DDL
      */
-    @SneakyThrows
-    public String generateLogicDDLSQL(final DataSource sourceDataSource, final 
DatabaseType databaseType, final String schemaName, final String 
logicTableName, final String actualTableName,
-                                      final ShardingSphereSQLParserEngine 
parserEngine) {
-        log.info("generateLogicDDLSQL, databaseType={},  schemaName={}, 
tableName={}", databaseType.getType(), schemaName, logicTableName);
-        Collection<String> multiSQL = generateActualDDLSQL(databaseType, 
schemaName, actualTableName, sourceDataSource);
+    @SneakyThrows(SQLException.class)
+    public String generateLogicDDL(final DataSource sourceDataSource, final 
DatabaseType databaseType,
+                                   final String schemaName, final String 
logicTableName, final String actualTableName, final 
ShardingSphereSQLParserEngine parserEngine) {
+        log.info("generateLogicDDLSQL, databaseType={}, schemaName={}, 
tableName={}", databaseType.getType(), schemaName, logicTableName);
         StringBuilder result = new StringBuilder();
-        for (String each : multiSQL) {
+        for (String each : 
CreateTableSQLGeneratorFactory.getInstance(databaseType).generate(sourceDataSource,
 schemaName, actualTableName)) {
             Optional<String> logicSQL = decorate(databaseType, schemaName, 
sourceDataSource, each, logicTableName, parserEngine);
             logicSQL.ifPresent(ddlSQL -> 
result.append(ddlSQL).append(DELIMITER).append(System.lineSeparator()));
         }
         return result.toString();
     }
     
-    /**
-     * Replace table name with prefix.
-     *
-     * @param sql sql
-     * @param prefix prefix
-     * @param databaseName database name
-     * @param parserEngine parser engine
-     * @return replaced sql
-     */
-    public String replaceTableNameWithPrefix(final String sql, final String 
prefix, final String databaseName,
-                                             final 
ShardingSphereSQLParserEngine parserEngine) {
-        LogicSQL logicSQL = getLogicSQL(sql, databaseName, parserEngine);
-        SQLStatementContext<?> sqlStatementContext = 
logicSQL.getSqlStatementContext();
-        if (sqlStatementContext instanceof CreateTableStatementContext || 
sqlStatementContext instanceof CommentStatementContext || sqlStatementContext 
instanceof CreateIndexStatementContext
-                || sqlStatementContext instanceof AlterTableStatementContext) {
-            if (!sqlStatementContext.getTablesContext().getTables().isEmpty()) 
{
-                TableNameSegment tableNameSegment = 
sqlStatementContext.getTablesContext().getTables().iterator().next().getTableName();
-                Map<SQLSegment, String> replaceMap = new 
TreeMap<>(Comparator.comparing(SQLSegment::getStartIndex));
-                replaceMap.put(tableNameSegment, prefix + 
tableNameSegment.getIdentifier().getValue());
-                return doDecorateActualTable(replaceMap, sql);
-            }
-        }
-        return sql;
-    }
-    
     private Optional<String> decorate(final DatabaseType databaseType, final 
String schemaName, final DataSource dataSource, final String sql, final String 
logicTableName,
                                       final ShardingSphereSQLParserEngine 
parserEngine) throws SQLException {
         if (sql.trim().isEmpty()) {
@@ -134,11 +102,6 @@ public final class PipelineDDLGenerator {
         return Optional.of(result);
     }
     
-    private Collection<String> generateActualDDLSQL(final DatabaseType 
databaseType, final String schemaName, final String actualTable, final 
DataSource dataSource) throws SQLException {
-        return 
CreateTableSQLGeneratorFactory.findInstance(databaseType).orElseThrow(() -> new 
ServiceProviderNotFoundException(CreateTableSQLGenerator.class, 
databaseType.getType()))
-                .generate(actualTable, schemaName, dataSource);
-    }
-    
     private String decorateActualSQL(final String sql, final String 
logicTableName, final String databaseName, final ShardingSphereSQLParserEngine 
parserEngine) {
         LogicSQL logicSQL = getLogicSQL(sql, databaseName, parserEngine);
         SQLStatementContext<?> sqlStatementContext = 
logicSQL.getSqlStatementContext();
@@ -204,12 +167,6 @@ public final class PipelineDDLGenerator {
         return result.toString();
     }
     
-    private String findLogicTable(final TableNameSegment tableNameSegment, 
final ShardingSphereDatabase database) {
-        String actualTable = tableNameSegment.getIdentifier().getValue();
-        return database.getRuleMetaData().getRules().stream().filter(each -> 
each instanceof DataNodeContainedRule)
-                .map(each -> ((DataNodeContainedRule) 
each).findLogicTableByActualTable(actualTable).orElse(null)).filter(Objects::nonNull).findFirst().orElse(actualTable);
-    }
-    
     private LogicSQL getLogicSQL(final String sql, final String databaseName, 
final ShardingSphereSQLParserEngine parserEngine) {
         SQLStatement sqlStatement = parserEngine.parse(sql, false);
         SQLStatementContext<?> sqlStatementContext = 
SQLStatementContextFactory.newInstance(null, sqlStatement, databaseName);
@@ -224,4 +181,19 @@ public final class PipelineDDLGenerator {
         }
         return Optional.of(replaceTableNameWithPrefix(logicSQL, schemaName + 
".", databaseName, parserEngine));
     }
+    
+    private String replaceTableNameWithPrefix(final String sql, final String 
prefix, final String databaseName, final ShardingSphereSQLParserEngine 
parserEngine) {
+        LogicSQL logicSQL = getLogicSQL(sql, databaseName, parserEngine);
+        SQLStatementContext<?> sqlStatementContext = 
logicSQL.getSqlStatementContext();
+        if (sqlStatementContext instanceof CreateTableStatementContext || 
sqlStatementContext instanceof CommentStatementContext || sqlStatementContext 
instanceof CreateIndexStatementContext
+                || sqlStatementContext instanceof AlterTableStatementContext) {
+            if (!sqlStatementContext.getTablesContext().getTables().isEmpty()) 
{
+                TableNameSegment tableNameSegment = 
sqlStatementContext.getTablesContext().getTables().iterator().next().getTableName();
+                Map<SQLSegment, String> replaceMap = new 
TreeMap<>(Comparator.comparing(SQLSegment::getStartIndex));
+                replaceMap.put(tableNameSegment, prefix + 
tableNameSegment.getIdentifier().getValue());
+                return doDecorateActualTable(replaceMap, sql);
+            }
+        }
+        return sql;
+    }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/prepare/datasource/AbstractDataSourcePreparer.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/prepare/datasource/AbstractDataSourcePreparer.java
index 191f9a8bc3f..c4d196c2f01 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/prepare/datasource/AbstractDataSourcePreparer.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/prepare/datasource/AbstractDataSourcePreparer.java
@@ -128,7 +128,7 @@ public abstract class AbstractDataSourcePreparer implements 
DataSourcePreparer {
             DataSource dataSource = 
parameter.getSourceDataSourceMap().get(dataSourceName);
             DatabaseType databaseType = 
DatabaseTypeEngine.getDatabaseType(Collections.singletonList(dataSource));
             String actualTableName = 
parameter.getTableNameMap().get(each.getLogicTableName());
-            result.add(generator.generateLogicDDLSQL(dataSource, databaseType, 
schemaName, each.getLogicTableName(), actualTableName, 
parameter.getSqlParserEngine()));
+            result.add(generator.generateLogicDDL(dataSource, databaseType, 
schemaName, each.getLogicTableName(), actualTableName, 
parameter.getSqlParserEngine()));
         }
         return result;
     }
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ddlgenerator/MySQLCreateTableSQLGenerator.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ddlgenerator/MySQLCreateTableSQLGenerator.java
index 6686468d0e0..b150d4a3008 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ddlgenerator/MySQLCreateTableSQLGenerator.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ddlgenerator/MySQLCreateTableSQLGenerator.java
@@ -17,8 +17,8 @@
 
 package org.apache.shardingsphere.data.pipeline.mysql.ddlgenerator;
 
+import 
org.apache.shardingsphere.data.pipeline.core.exception.CreateTableSQLGenerateException;
 import 
org.apache.shardingsphere.data.pipeline.spi.ddlgenerator.CreateTableSQLGenerator;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -38,7 +38,7 @@ public final class MySQLCreateTableSQLGenerator implements 
CreateTableSQLGenerat
     private static final String COLUMN_LABEL = "create table";
     
     @Override
-    public Collection<String> generate(final String tableName, final String 
schemaName, final DataSource dataSource) throws SQLException {
+    public Collection<String> generate(final DataSource dataSource, final 
String schemaName, final String tableName) throws SQLException {
         try (
                 Connection connection = dataSource.getConnection();
                 Statement statement = connection.createStatement();
@@ -47,7 +47,7 @@ public final class MySQLCreateTableSQLGenerator implements 
CreateTableSQLGenerat
                 return 
Collections.singletonList(resultSet.getString(COLUMN_LABEL));
             }
         }
-        throw new ShardingSphereException("Failed to get ddl sql for table 
%s", tableName);
+        throw new CreateTableSQLGenerateException(tableName);
     }
     
     @Override
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ddlgenerator/OpenGaussCreateTableSQLGenerator.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ddlgenerator/OpenGaussCreateTableSQLGenerator.java
index ac8b4402c03..0c28c101c6d 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ddlgenerator/OpenGaussCreateTableSQLGenerator.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ddlgenerator/OpenGaussCreateTableSQLGenerator.java
@@ -17,8 +17,8 @@
 
 package org.apache.shardingsphere.data.pipeline.opengauss.ddlgenerator;
 
+import 
org.apache.shardingsphere.data.pipeline.core.exception.CreateTableSQLGenerateException;
 import 
org.apache.shardingsphere.data.pipeline.spi.ddlgenerator.CreateTableSQLGenerator;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -40,7 +40,7 @@ public final class OpenGaussCreateTableSQLGenerator 
implements CreateTableSQLGen
     private static final String DELIMITER = ";";
     
     @Override
-    public Collection<String> generate(final String tableName, final String 
schemaName, final DataSource dataSource) throws SQLException {
+    public Collection<String> generate(final DataSource dataSource, final 
String schemaName, final String tableName) throws SQLException {
         try (
                 Connection connection = dataSource.getConnection();
                 Statement statement = connection.createStatement();
@@ -50,7 +50,7 @@ public final class OpenGaussCreateTableSQLGenerator 
implements CreateTableSQLGen
                 return 
Arrays.asList(resultSet.getString(COLUMN_LABEL).split(DELIMITER));
             }
         }
-        throw new ShardingSphereException("Failed to get ddl sql for table 
%s", tableName);
+        throw new CreateTableSQLGenerateException(tableName);
     }
     
     @Override
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgreSQLCreateTableSQLGenerator.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgreSQLCreateTableSQLGen
 [...]
index f663405412a..b96dda8bf90 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgreSQLCreateTableSQLGenerator.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ddlgenerator/PostgreSQLCreateTableSQLGenerator.java
@@ -36,7 +36,7 @@ public final class PostgreSQLCreateTableSQLGenerator 
implements CreateTableSQLGe
     
     // TODO support partitions etc.
     @Override
-    public Collection<String> generate(final String tableName, final String 
schemaName, final DataSource dataSource) throws SQLException {
+    public Collection<String> generate(final DataSource dataSource, final 
String schemaName, final String tableName) throws SQLException {
         try (Connection connection = dataSource.getConnection()) {
             int majorVersion = 
connection.getMetaData().getDatabaseMajorVersion();
             int minorVersion = 
connection.getMetaData().getDatabaseMinorVersion();
diff --git 
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/general/CreateTableSQLGeneratorIT.java
 
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/general/CreateTableSQLGeneratorIT.java
index ad329580a43..e90e280f7d7 100644
--- 
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/general/CreateTableSQLGeneratorIT.java
+++ 
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/general/CreateTableSQLGeneratorIT.java
@@ -21,7 +21,6 @@ import 
org.apache.shardingsphere.data.pipeline.spi.ddlgenerator.CreateTableSQLGe
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
 import 
org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
 import 
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import 
org.apache.shardingsphere.integration.data.pipeline.cases.entity.CreateTableSQLGeneratorAssertionEntity;
 import 
org.apache.shardingsphere.integration.data.pipeline.cases.entity.CreateTableSQLGeneratorAssertionsRootEntity;
 import 
org.apache.shardingsphere.integration.data.pipeline.cases.entity.CreateTableSQLGeneratorOutputEntity;
@@ -113,9 +112,8 @@ public final class CreateTableSQLGeneratorIT {
             int majorVersion = 
connection.getMetaData().getDatabaseMajorVersion();
             for (CreateTableSQLGeneratorAssertionEntity each : 
rootEntity.getAssertions()) {
                 statement.execute(each.getInput().getSql());
-                Collection<String> multiSQL = 
CreateTableSQLGeneratorFactory.findInstance(parameterized.getDatabaseType())
-                        .orElseThrow(() -> new ShardingSphereException("Failed 
to get create table sql generator")).generate(each.getInput().getTable(), 
DEFAULT_SCHEMA, dataSource);
-                assertIsCorrect(multiSQL, getVersionOutput(each.getOutputs(), 
majorVersion));
+                Collection<String> actualDDLs = 
CreateTableSQLGeneratorFactory.getInstance(parameterized.getDatabaseType()).generate(dataSource,
 DEFAULT_SCHEMA, each.getInput().getTable());
+                assertIsCorrect(actualDDLs, 
getVersionOutput(each.getOutputs(), majorVersion));
             }
         }
     }

Reply via email to