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 8c2deac  Import optimize module (#10388)
8c2deac is described below

commit 8c2deac1e5e2e7a8740bbb7902876796bb08c334
Author: Juan Pan(Trista) <[email protected]>
AuthorDate: Wed May 19 17:17:28 2021 +0800

    Import optimize module (#10388)
---
 .../shardingsphere-infra-executor/pom.xml          |   5 +
 .../sql/optimize/schema/CalciteLogicSchema.java    |  17 +++-
 .../optimize/schema/CalciteLogicSchemaFactory.java |  19 +---
 .../generator/CalciteLogicSchemaGenerator.java     | 102 ---------------------
 .../generator/CalciteLogicTableGenerator.java      |  95 -------------------
 .../schema/table/AbstractCalciteTable.java         |  11 +--
 .../schema/table/CalciteFilterableTable.java       |  12 +--
 7 files changed, 32 insertions(+), 229 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-executor/pom.xml 
b/shardingsphere-infra/shardingsphere-infra-executor/pom.xml
index 594e1cd..ec074f5 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/pom.xml
+++ b/shardingsphere-infra/shardingsphere-infra-executor/pom.xml
@@ -40,6 +40,11 @@
             <version>${project.version}</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-infra-optimize</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
             <groupId>org.apache.calcite</groupId>
             <artifactId>calcite-core</artifactId>
         </dependency>
diff --git 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/CalciteLogicSchema.java
 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/CalciteLogicSchema.java
index 255879f..3fb585f 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/CalciteLogicSchema.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/CalciteLogicSchema.java
@@ -21,8 +21,13 @@ import lombok.Getter;
 import org.apache.calcite.schema.Table;
 import org.apache.calcite.schema.impl.AbstractSchema;
 import org.apache.commons.collections4.map.LinkedMap;
+import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.row.CalciteRowExecutor;
+import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.table.CalciteFilterableTable;
+import org.apache.shardingsphere.infra.optimize.schema.LogicSchemaMetadata;
+import org.apache.shardingsphere.infra.optimize.schema.LogicTableMetadata;
 
 import java.util.Map;
+import java.util.Map.Entry;
 
 
 /**
@@ -36,9 +41,15 @@ public final class CalciteLogicSchema extends AbstractSchema 
{
     
     private final Map<String, Table> tables = new LinkedMap<>();
     
-    public CalciteLogicSchema(final String name, final Map<String, Table> 
tables) {
-        this.name = name;
-        this.tables.putAll(tables);
+    public CalciteLogicSchema(final LogicSchemaMetadata metadata, final 
CalciteRowExecutor executor) {
+        this.name = metadata.getName();
+        initTables(metadata, executor);
+    }
+    
+    private void initTables(final LogicSchemaMetadata metadata, final 
CalciteRowExecutor executor) {
+        for (Entry<String, LogicTableMetadata> entry : 
metadata.getTables().entrySet()) {
+            tables.put(entry.getKey(), new 
CalciteFilterableTable(entry.getValue(), executor));
+        }
     }
     
     @Override
diff --git 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/CalciteLogicSchemaFactory.java
 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/CalciteLogicSchemaFactory.java
index a51aa92..b94dcb6 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/CalciteLogicSchemaFactory.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/CalciteLogicSchemaFactory.java
@@ -17,31 +17,22 @@
 
 package org.apache.shardingsphere.infra.executor.sql.optimize.schema;
 
-import org.apache.commons.collections4.map.LinkedMap;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
-import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.generator.CalciteLogicSchemaGenerator;
 import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.row.CalciteRowExecutor;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.optimize.schema.LogicSchemaMetadatas;
 
-import java.sql.SQLException;
 import java.util.Map;
-import java.util.Map.Entry;
 
 /**
  * Calcite logic schema factory.
  */
 public final class CalciteLogicSchemaFactory {
     
-    private final Map<String, CalciteLogicSchemaGenerator> schemas = new 
LinkedMap<>();
+    private final LogicSchemaMetadatas metadatas;
     
     public CalciteLogicSchemaFactory(final Map<String, ShardingSphereMetaData> 
metaDataMap) {
-        for (Entry<String, ShardingSphereMetaData> each : 
metaDataMap.entrySet()) {
-            try {
-                schemas.put(each.getKey(), new 
CalciteLogicSchemaGenerator(each.getValue()));
-            } catch (final SQLException ex) {
-                throw new ShardingSphereException(ex);
-            }
-        }
+        metadatas = new LogicSchemaMetadatas(metaDataMap);
     }
     
     /**
@@ -52,9 +43,9 @@ public final class CalciteLogicSchemaFactory {
      * @return schema
      */
     public CalciteLogicSchema create(final String name, final 
CalciteRowExecutor executor) {
-        if (!schemas.containsKey(name)) {
+        if (!metadatas.getSchemas().containsKey(name)) {
             throw new ShardingSphereException("No `%s` schema.", name);
         }
-        return schemas.get(name).create(executor);
+        return new CalciteLogicSchema(metadatas.getSchemas().get(name), 
executor);
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/generator/CalciteLogicSchemaGenerator.java
 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/generator/CalciteLogicSchemaGenerator.java
deleted file mode 100644
index 8cc6b81..0000000
--- 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/generator/CalciteLogicSchemaGenerator.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.executor.sql.optimize.schema.generator;
-
-import org.apache.calcite.schema.Table;
-import org.apache.commons.collections4.map.LinkedMap;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.CalciteLogicSchema;
-import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.row.CalciteRowExecutor;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
-import org.apache.shardingsphere.infra.rule.type.DataSourceContainedRule;
-
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Map.Entry;
-
-/**
- * Calcite logic schema generator.
- */
-public final class CalciteLogicSchemaGenerator {
-    
-    private final String name;
-    
-    private final Map<String, CalciteLogicTableGenerator> tables = new 
LinkedMap<>();
-    
-    public CalciteLogicSchemaGenerator(final ShardingSphereMetaData metaData) 
throws SQLException {
-        name = metaData.getName();
-        initTables(metaData);
-    }
-    
-    private void initTables(final ShardingSphereMetaData metaData) throws 
SQLException {
-        Collection<DataNodeContainedRule> dataNodeRules = 
getDataNodeContainedRules(metaData);
-        Map<String, Collection<DataNode>> tableDataNodes = 
getTableDataNodes(dataNodeRules);
-        Map<String, Collection<String>> dataSourceRules = 
getDataSourceRules(metaData);
-        for (Entry<String, Collection<DataNode>> entry : 
tableDataNodes.entrySet()) {
-            tables.put(entry.getKey(),
-                    new CalciteLogicTableGenerator(entry.getKey(), 
metaData.getResource().getDataSources(), dataSourceRules, entry.getValue(), 
metaData.getResource().getDatabaseType()));
-        }
-    }
-    
-    private Collection<DataNodeContainedRule> getDataNodeContainedRules(final 
ShardingSphereMetaData metaData) {
-        Collection<DataNodeContainedRule> result = new LinkedList<>();
-        for (ShardingSphereRule each : metaData.getRuleMetaData().getRules()) {
-            if (each instanceof DataNodeContainedRule) {
-                result.add((DataNodeContainedRule) each);
-            }
-        }
-        return result;
-    }
-    
-    private Map<String, Collection<String>> getDataSourceRules(final 
ShardingSphereMetaData metaData) {
-        Map<String, Collection<String>> result = new LinkedHashMap<>();
-        for (ShardingSphereRule each : metaData.getRuleMetaData().getRules()) {
-            if (each instanceof DataSourceContainedRule) {
-                result.putAll(((DataSourceContainedRule) 
each).getDataSourceMapper());
-            }
-        }
-        return result;
-    }
-    
-    private Map<String, Collection<DataNode>> getTableDataNodes(final 
Collection<DataNodeContainedRule> dataNodeRules) {
-        Map<String, Collection<DataNode>> result = new LinkedHashMap<>();
-        for (DataNodeContainedRule each : dataNodeRules) {
-            result.putAll(each.getAllDataNodes());
-        }
-        return result;
-    }
-    
-    /**
-     * Create.
-     *
-     * @param executor executor
-     * @return schema
-     */
-    public CalciteLogicSchema create(final CalciteRowExecutor executor) {
-        Map<String, Table> tables = new LinkedMap<>();
-        for (Entry<String, CalciteLogicTableGenerator> entry : 
this.tables.entrySet()) {
-            tables.put(entry.getKey(), entry.getValue().create(executor));
-        }
-        return new CalciteLogicSchema(name, tables);
-    }
-}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/generator/CalciteLogicTableGenerator.java
 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/generator/CalciteLogicTableGenerator.java
deleted file mode 100644
index 09ee2d2..0000000
--- 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/generator/CalciteLogicTableGenerator.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.executor.sql.optimize.schema.generator;
-
-import org.apache.calcite.rel.type.RelDataTypeFactory;
-import org.apache.calcite.rel.type.RelDataTypeImpl;
-import org.apache.calcite.rel.type.RelDataTypeSystem;
-import org.apache.calcite.rel.type.RelProtoDataType;
-import org.apache.calcite.schema.Table;
-import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
-import org.apache.calcite.sql.type.SqlTypeName;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.row.CalciteRowExecutor;
-import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.table.CalciteFilterableTable;
-import 
org.apache.shardingsphere.infra.metadata.schema.builder.loader.TableMetaDataLoader;
-import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
-import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
-
-import javax.sql.DataSource;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.Map;
-import java.util.Optional;
-
-/**
- * Calcite logic table generator.
- */
-public final class CalciteLogicTableGenerator {
-    
-    private final String name;
-    
-    private final TableMetaData tableMetaData;
-    
-    private final RelProtoDataType relProtoDataType;
-    
-    public CalciteLogicTableGenerator(final String name, final Map<String, 
DataSource> dataSources, final Map<String, Collection<String>> dataSourceRules,
-                                      final Collection<DataNode> 
tableDataNodes, final DatabaseType databaseType) throws SQLException {
-        this.name = name;
-        tableMetaData = createTableMetaData(dataSources, dataSourceRules, 
tableDataNodes, databaseType);
-        relProtoDataType = createRelDataType();
-    }
-    
-    private TableMetaData createTableMetaData(final Map<String, DataSource> 
dataSources, final Map<String, Collection<String>> dataSourceRules,
-                                              final Collection<DataNode> 
tableDataNodes, final DatabaseType databaseType) throws SQLException {
-        DataNode dataNode = tableDataNodes.iterator().next();
-        Optional<TableMetaData> tableMetaData =
-                TableMetaDataLoader.load(getActualDataSource(dataSources, 
dataSourceRules, dataNode.getDataSourceName()), dataNode.getTableName(), 
databaseType);
-        return tableMetaData.orElseGet(TableMetaData::new);
-    }
-    
-    private RelProtoDataType createRelDataType() {
-        RelDataTypeFactory typeFactory = new 
SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
-        RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
-        for (Map.Entry<String, ColumnMetaData> entry : 
tableMetaData.getColumns().entrySet()) {
-            SqlTypeName sqlTypeName = 
SqlTypeName.getNameForJdbcType(entry.getValue().getDataType());
-            fieldInfo.add(entry.getKey(), null == sqlTypeName ? 
typeFactory.createUnknownType() : 
typeFactory.createTypeWithNullability(typeFactory.createSqlType(sqlTypeName), 
true));
-        }
-        return RelDataTypeImpl.proto(fieldInfo.build());
-    }
-    
-    private DataSource getActualDataSource(final Map<String, DataSource> 
dataSources,
-                                                   final Map<String, 
Collection<String>> dataSourceRules, final String logicDataSource) {
-        String result = logicDataSource;
-        if (dataSourceRules.containsKey(logicDataSource)) {
-            result = dataSourceRules.get(logicDataSource).iterator().next();
-        }
-        return dataSources.get(result);
-    }
-    
-    /**
-     * Create.
-     *
-     * @param executor executor
-     * @return table
-     */
-    public Table create(final CalciteRowExecutor executor) {
-        return new CalciteFilterableTable(name, tableMetaData, 
relProtoDataType, executor);
-    }
-}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/table/AbstractCalciteTable.java
 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/table/AbstractCalciteTable.java
index f038c49..ffb7693 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/table/AbstractCalciteTable.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/table/AbstractCalciteTable.java
@@ -22,10 +22,9 @@ import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
-import org.apache.calcite.rel.type.RelProtoDataType;
 import org.apache.calcite.schema.impl.AbstractTable;
 import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.row.CalciteRowExecutor;
-import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
+import org.apache.shardingsphere.infra.optimize.schema.LogicTableMetadata;
 
 /**
  * Abstract calcite table.
@@ -34,16 +33,12 @@ import 
org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
 @RequiredArgsConstructor
 public abstract class AbstractCalciteTable extends AbstractTable {
     
-    private final String name;
-    
-    private final TableMetaData tableMetaData;
-    
-    private final RelProtoDataType relProtoDataType;
+    private final LogicTableMetadata metadata;
     
     private final CalciteRowExecutor executor;
     
     @Override
     public final RelDataType getRowType(final RelDataTypeFactory typeFactory) {
-        return relProtoDataType.apply(typeFactory);
+        return metadata.getRelProtoDataType().apply(typeFactory);
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/table/CalciteFilterableTable.java
 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/table/CalciteFilterableTable.java
index 23ad5b9..b481a37 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/table/CalciteFilterableTable.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/optimize/schema/table/CalciteFilterableTable.java
@@ -21,14 +21,13 @@ import org.apache.calcite.DataContext;
 import org.apache.calcite.linq4j.AbstractEnumerable;
 import org.apache.calcite.linq4j.Enumerable;
 import org.apache.calcite.linq4j.Enumerator;
-import org.apache.calcite.rel.type.RelProtoDataType;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.schema.ProjectableFilterableTable;
 import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.row.CalciteRowEnumerator;
 import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.row.CalciteRowExecutor;
 import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.table.execute.CalciteExecutionContextGenerator;
 import 
org.apache.shardingsphere.infra.executor.sql.optimize.schema.table.execute.CalciteExecutionSQLGenerator;
-import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
+import org.apache.shardingsphere.infra.optimize.schema.LogicTableMetadata;
 
 import java.util.List;
 
@@ -38,9 +37,8 @@ import java.util.List;
  */
 public final class CalciteFilterableTable extends AbstractCalciteTable 
implements ProjectableFilterableTable {
     
-    public CalciteFilterableTable(final String name, final TableMetaData 
tableMetaData, final RelProtoDataType relProtoDataType,
-                                  final CalciteRowExecutor executor) {
-        super(name, tableMetaData, relProtoDataType, executor);
+    public CalciteFilterableTable(final LogicTableMetadata metadata, final 
CalciteRowExecutor executor) {
+        super(metadata, executor);
     }
     
     @Override
@@ -49,8 +47,8 @@ public final class CalciteFilterableTable extends 
AbstractCalciteTable implement
 
             @Override
             public Enumerator<Object[]> enumerator() {
-                CalciteExecutionContextGenerator generator =
-                        new CalciteExecutionContextGenerator(getName(), 
getExecutor().getInitialExecutionContext(), new 
CalciteExecutionSQLGenerator(root, filters, projects));
+                CalciteExecutionContextGenerator generator = new 
CalciteExecutionContextGenerator(getMetadata().getName(), 
+                        getExecutor().getInitialExecutionContext(), new 
CalciteExecutionSQLGenerator(root, filters, projects));
                 return new 
CalciteRowEnumerator(getExecutor().execute(generator.generate()));
             }
         };

Reply via email to