You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/12/30 02:46:24 UTC

[shardingsphere] branch master updated: Refactor Calcite executor (#8812)

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

zhangliang 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 5959420  Refactor Calcite executor (#8812)
5959420 is described below

commit 5959420f1d29787271428088a5d99dccc1a5abbc
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Wed Dec 30 10:45:55 2020 +0800

    Refactor Calcite executor (#8812)
    
    * Refactor Calcite executor
---
 .../shardingsphere/infra/optimize/context/CalciteContext.java  | 10 +++++-----
 .../infra/optimize/context/CalciteDataContext.java             |  2 +-
 .../infra/optimize/execute/CalciteJDBCExecutor.java            |  8 ++++----
 .../infra/optimize/schema/CalciteLogicSchemaFactory.java       |  3 +--
 .../infra/optimize/schema/CalciteRowEnumerator.java            | 10 +++++-----
 5 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContext.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContext.java
index b94f0a8..8f85199 100644
--- a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContext.java
@@ -24,7 +24,6 @@ import org.apache.calcite.plan.RelOptCluster;
 import org.apache.calcite.plan.RelOptTable.ViewExpander;
 import org.apache.calcite.prepare.CalciteCatalogReader;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
-import org.apache.calcite.schema.Schema;
 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
 import org.apache.calcite.sql.parser.SqlParser;
 import org.apache.calcite.sql.validate.SqlValidator;
@@ -43,7 +42,7 @@ import java.util.Collections;
 @Getter
 public final class CalciteContext {
     
-    private final CalciteSchema rootSchema;
+    private final CalciteLogicSchema calciteLogicSchema;
     
     private final CalciteCatalogReader catalogReader;
     
@@ -54,9 +53,10 @@ public final class CalciteContext {
     private final SqlToRelConverter relConverter;
     
     public CalciteContext(final CalciteConnectionConfig config,
-                          final SqlParser.Config parserConfig, final RelDataTypeFactory typeFactory, final RelOptCluster cluster, final Schema calciteSchema) {
-        rootSchema = CalciteSchema.createRootSchema(true);
-        rootSchema.add(((CalciteLogicSchema) calciteSchema).getName(), calciteSchema);
+                          final SqlParser.Config parserConfig, final RelDataTypeFactory typeFactory, final RelOptCluster cluster, final CalciteLogicSchema calciteLogicSchema) {
+        this.calciteLogicSchema = calciteLogicSchema;
+        CalciteSchema rootSchema = CalciteSchema.createRootSchema(true);
+        rootSchema.add(calciteLogicSchema.getName(), calciteLogicSchema);
         catalogReader = new CalciteCatalogReader(rootSchema, Collections.singletonList(config.schema()), typeFactory, config);
         this.parserConfig = parserConfig;
         validator = SqlValidatorUtil.newValidator(SqlStdOperatorTable.instance(), catalogReader, typeFactory, SqlValidator.Config.DEFAULT
diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteDataContext.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteDataContext.java
index cd59b3b..0ad163d 100644
--- a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteDataContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteDataContext.java
@@ -33,7 +33,7 @@ public final class CalciteDataContext implements DataContext {
     
     @Override
     public SchemaPlus getRootSchema() {
-        return context.getRootSchema().plus();
+        return context.getCatalogReader().getRootSchema().plus();
     }
     
     @Override
diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/execute/CalciteJDBCExecutor.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/execute/CalciteJDBCExecutor.java
index eafe95e..88bf5af 100644
--- a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/execute/CalciteJDBCExecutor.java
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/execute/CalciteJDBCExecutor.java
@@ -24,7 +24,7 @@ import org.apache.calcite.schema.SchemaPlus;
 import org.apache.calcite.sql.validate.SqlConformanceEnum;
 import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
 import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
-import org.apache.shardingsphere.infra.optimize.schema.CalciteLogicSchema;
+import org.apache.shardingsphere.infra.optimize.context.CalciteContext;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
@@ -47,7 +47,7 @@ public final class CalciteJDBCExecutor {
     
     public static final Properties PROPERTIES = new Properties();
     
-    private final CalciteLogicSchema schema;
+    private final CalciteContext context;
     
     static {
         try {
@@ -82,8 +82,8 @@ public final class CalciteJDBCExecutor {
         Connection result = DriverManager.getConnection(CONNECTION_URL, PROPERTIES);
         CalciteConnection calciteConnection = result.unwrap(CalciteConnection.class);
         SchemaPlus rootSchema = calciteConnection.getRootSchema();
-        rootSchema.add(schema.getName(), schema);
-        calciteConnection.setSchema(schema.getName());
+        rootSchema.add(context.getCalciteLogicSchema().getName(), context.getCalciteLogicSchema());
+        calciteConnection.setSchema(context.getCalciteLogicSchema().getName());
         return result;
     }
     
diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/CalciteLogicSchemaFactory.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/CalciteLogicSchemaFactory.java
index 1f2bb14..648b127 100644
--- a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/CalciteLogicSchemaFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/CalciteLogicSchemaFactory.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.infra.optimize.schema;
 
-import org.apache.calcite.schema.Schema;
 import org.apache.commons.collections4.map.LinkedMap;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
@@ -54,7 +53,7 @@ public final class CalciteLogicSchemaFactory {
      * @return schema
      * @exception SQLException sql exception
      */
-    public Schema create(final String name, final CalciteInternalExecutor executor) throws SQLException {
+    public CalciteLogicSchema create(final String name, final CalciteInternalExecutor executor) throws SQLException {
         if (!schemas.containsKey(name)) {
             throw new ShardingSphereException("No `%s` schema.", name);
         }
diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/CalciteRowEnumerator.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/CalciteRowEnumerator.java
index 19f751c..799780f 100644
--- a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/CalciteRowEnumerator.java
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/CalciteRowEnumerator.java
@@ -32,7 +32,7 @@ import java.util.LinkedList;
  */
 public final class CalciteRowEnumerator implements Enumerator<Object[]> {
     
-    private final Collection<QueryResult> resultSets = new LinkedList<>();
+    private final Collection<QueryResult> queryResults = new LinkedList<>();
     
     private final Iterator<QueryResult> iterator;
     
@@ -40,9 +40,9 @@ public final class CalciteRowEnumerator implements Enumerator<Object[]> {
     
     private Object[] currentRow;
     
-    public CalciteRowEnumerator(final Collection<QueryResult> resultSets) {
-        this.resultSets.addAll(resultSets);
-        iterator = this.resultSets.iterator();
+    public CalciteRowEnumerator(final Collection<QueryResult> queryResults) {
+        this.queryResults.addAll(queryResults);
+        iterator = this.queryResults.iterator();
         currentResultSet = iterator.next();
     }
     
@@ -92,7 +92,7 @@ public final class CalciteRowEnumerator implements Enumerator<Object[]> {
     @Override
     public void close() {
         try {
-            for (QueryResult each : resultSets) {
+            for (QueryResult each : queryResults) {
                 each.close();
             }
             currentRow = null;