You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/05/07 04:56:37 UTC

[shardingsphere] branch master updated: openGauss create table SQL append schema name (#17411)

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 87d5a4b4cd6 openGauss create table SQL append schema name (#17411)
87d5a4b4cd6 is described below

commit 87d5a4b4cd6b96270987abd3f531872dcab61a69
Author: Hongsheng Zhong <sa...@126.com>
AuthorDate: Sat May 7 12:56:31 2022 +0800

    openGauss create table SQL append schema name (#17411)
    
    * Ignore default schema creation
    
    * openGauss create table SQL append schema name
    
    * Improve replaceActualTableNameToLogicTableName
---
 .../datasource/AbstractDataSourcePreparer.java     | 27 +++++++++++++++++-----
 .../datasource/OpenGaussDataSourcePreparer.java    | 24 +++++++++----------
 .../datasource/AbstractDataSourcePreparerTest.java | 10 +++++++-
 3 files changed, 41 insertions(+), 20 deletions(-)

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 06e011a904a..040779e63d2 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
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.data.pipeline.core.prepare.datasource;
 
+import lombok.NonNull;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.data.pipeline.api.config.rulealtered.RuleAlteredJobConfiguration;
 import org.apache.shardingsphere.data.pipeline.api.config.rulealtered.TaskConfiguration;
@@ -70,11 +71,16 @@ public abstract class AbstractDataSourcePreparer implements DataSourcePreparer {
             return;
         }
         Set<String> schemaNames = getSchemaNames(parameter);
-        log.info("prepareTargetSchemas, schemaNames={}", schemaNames);
+        String defaultSchema = targetDatabaseType.getDefaultSchema(parameter.getTaskConfig().getJobConfig().getDatabaseName());
+        log.info("prepareTargetSchemas, schemaNames={}, defaultSchema={}", schemaNames, defaultSchema);
         PipelineSQLBuilder pipelineSQLBuilder = PipelineSQLBuilderFactory.newInstance(targetDatabaseType.getName());
         try (Connection targetConnection = getTargetCachedDataSource(parameter.getTaskConfig(), parameter.getDataSourceManager()).getConnection()) {
             for (String each : schemaNames) {
+                if (each.equalsIgnoreCase(defaultSchema)) {
+                    continue;
+                }
                 String sql = pipelineSQLBuilder.buildCreateSchemaSQL(each);
+                log.info("prepareTargetSchemas, sql={}", sql);
                 try (Statement statement = targetConnection.createStatement()) {
                     statement.execute(sql);
                 } catch (final SQLException ignored) {
@@ -150,12 +156,21 @@ public abstract class AbstractDataSourcePreparer implements DataSourcePreparer {
         return PATTERN_CREATE_TABLE.matcher(createTableSQL).replaceFirst("CREATE TABLE IF NOT EXISTS ");
     }
     
-    protected String replaceActualTableNameToLogicTableName(final String createOrAlterTableSQL, final String actualTableName, final String logicTableName) {
-        int start = createOrAlterTableSQL.indexOf(actualTableName);
-        if (start <= 0) {
+    protected String replaceActualTableNameToLogicTableName(final String createOrAlterTableSQL, final @NonNull String actualTableName, final @NonNull String logicTableName) {
+        if (actualTableName.equalsIgnoreCase(logicTableName)) {
             return createOrAlterTableSQL;
         }
-        int end = start + actualTableName.length();
-        return new StringBuilder(createOrAlterTableSQL).replace(start, end, logicTableName).toString();
+        StringBuilder logicalTableSQL = new StringBuilder(createOrAlterTableSQL);
+        for (int i = 0; i < 10_000; i++) {
+            int start = logicalTableSQL.indexOf(actualTableName);
+            if (start <= 0) {
+                return logicalTableSQL.toString();
+            }
+            int end = start + actualTableName.length();
+            logicalTableSQL.replace(start, end, logicTableName);
+        }
+        log.error("replaceActualTableNameToLogicTableName, too many times loop, createOrAlterTableSQL={}, actualTableName={}, logicTableName={}",
+                createOrAlterTableSQL, actualTableName, logicalTableSQL);
+        throw new RuntimeException("Too many times loop");
     }
 }
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/prepare/datasource/OpenGaussDataSourcePreparer.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/prepare/datasource/OpenGaussDataSourcePreparer.java
index 872750fa954..a3dd652e917 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/prepare/datasource/OpenGaussDataSourcePreparer.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/prepare/datasource/OpenGaussDataSourcePreparer.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.data.pipeline.opengauss.prepare.datasource;
 
 import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.data.pipeline.api.config.TableNameSchemaNameMapping;
 import org.apache.shardingsphere.data.pipeline.api.datanode.JobDataNodeEntry;
 import org.apache.shardingsphere.data.pipeline.api.datasource.PipelineDataSourceWrapper;
 import org.apache.shardingsphere.data.pipeline.api.datasource.config.PipelineDataSourceConfigurationFactory;
@@ -59,7 +60,7 @@ public final class OpenGaussDataSourcePreparer extends AbstractDataSourcePrepare
         } catch (final SQLException ex) {
             throw new PipelineJobPrepareFailedException("get table definitions failed.", ex);
         }
-        Map<String, Collection<String>> createLogicTableSQLs = getCreateLogicTableSQLs(actualTableDefinitions);
+        Map<String, Collection<String>> createLogicTableSQLs = getCreateLogicTableSQLs(actualTableDefinitions, parameter.getTableNameSchemaNameMapping());
         try (Connection targetConnection = getTargetCachedDataSource(parameter.getTaskConfig(), parameter.getDataSourceManager()).getConnection()) {
             for (Entry<String, Collection<String>> entry : createLogicTableSQLs.entrySet()) {
                 for (String each : entry.getValue()) {
@@ -112,20 +113,22 @@ public final class OpenGaussDataSourcePreparer extends AbstractDataSourcePrepare
      * @param actualTableDefinitions actual table definitions. key is logic table name, value is actual table definition.
      * @return all SQLs. key is logic table name, value is collection of logic table SQLs.
      */
-    private Map<String, Collection<String>> getCreateLogicTableSQLs(final Collection<ActualTableDefinition> actualTableDefinitions) {
+    private Map<String, Collection<String>> getCreateLogicTableSQLs(final Collection<ActualTableDefinition> actualTableDefinitions, final TableNameSchemaNameMapping tableNameSchemaNameMapping) {
         Map<String, Collection<String>> result = new HashMap<>();
         for (ActualTableDefinition each : actualTableDefinitions) {
+            String schemaName = tableNameSchemaNameMapping.getSchemaName(each.getLogicTableName());
             Collection<String> logicTableSQLs = splitTableDefinitionToSQLs(each).stream().map(sql -> {
                 TableDefinitionSQLType sqlType = getTableDefinitionSQLType(sql);
                 switch (sqlType) {
                     // TODO replace constraint and index name
                     case CREATE_TABLE:
                         sql = addIfNotExistsForCreateTableSQL(sql);
-                        // TODO schemaName in create table SQL
+                        sql = appendSchemaName(sql, each.getActualTableName(), schemaName);
                         sql = replaceActualTableNameToLogicTableName(sql, each.getActualTableName(), each.getLogicTableName());
                         sql = skipCreateTableExtendSet(sql);
                         return sql;
                     case ALTER_TABLE:
+                        sql = appendSchemaName(sql, each.getActualTableName(), schemaName);
                         return replaceActualTableNameToLogicTableName(sql, each.getActualTableName(), each.getLogicTableName());
                     default:
                         return "";
@@ -136,17 +139,12 @@ public final class OpenGaussDataSourcePreparer extends AbstractDataSourcePrepare
         return result;
     }
     
-    @Override
-    protected String replaceActualTableNameToLogicTableName(final String createOrAlterTableSQL, final String actualTableName, final String logicTableName) {
-        StringBuilder logicalTableSQL = new StringBuilder(createOrAlterTableSQL);
-        while (true) {
-            int start = logicalTableSQL.indexOf(actualTableName);
-            if (start <= 0) {
-                return logicalTableSQL.toString();
-            }
-            int end = start + actualTableName.length();
-            logicalTableSQL.replace(start, end, logicTableName);
+    private String appendSchemaName(final String createOrAlterTableSQL, final String actualTableName, final String schemaName) {
+        int start = createOrAlterTableSQL.indexOf(actualTableName);
+        if (start <= 0) {
+            return createOrAlterTableSQL;
         }
+        return new StringBuilder(createOrAlterTableSQL).insert(start, schemaName + ".").toString();
     }
     
     private String skipCreateTableExtendSet(final String createSQL) {
diff --git a/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/prepare/datasource/AbstractDataSourcePreparerTest.java b/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/prepare/datasource/AbstractDataSourcePreparerTest.java
index a4f2fd3b775..f9631598e1d 100644
--- a/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/prepare/datasource/AbstractDataSourcePreparerTest.java
+++ b/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/prepare/datasource/AbstractDataSourcePreparerTest.java
@@ -59,8 +59,16 @@ public final class AbstractDataSourcePreparerTest {
     @Test
     public void assertReplaceActualTableNameToLogicTableName() {
         String sql = "ALTER TABLE t_order_0 ADD CONSTRAINT t_order_0_uniq UNIQUE (order_id)";
-        String expected = "ALTER TABLE t_order ADD CONSTRAINT t_order_0_uniq UNIQUE (order_id)";
+        String expected = "ALTER TABLE t_order ADD CONSTRAINT t_order_uniq UNIQUE (order_id)";
         String actual = preparer.replaceActualTableNameToLogicTableName(sql, "t_order_0", "t_order");
         assertThat(actual, is(expected));
     }
+    
+    @Test
+    public void assertReplaceActualTableNameToLogicTableNameTheSame() {
+        String sql = "ALTER TABLE t_order ADD CONSTRAINT t_order_uniq UNIQUE (order_id)";
+        String expected = "ALTER TABLE t_order ADD CONSTRAINT t_order_uniq UNIQUE (order_id)";
+        String actual = preparer.replaceActualTableNameToLogicTableName(sql, "t_order", "t_order");
+        assertThat(actual, is(expected));
+    }
 }