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

sunnianjun 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 88bbdf1fb79 Refactor TypedSPILoader (#27948)
88bbdf1fb79 is described below

commit 88bbdf1fb7969353f3f09a333c83ac22e6d235cf
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Aug 6 16:21:32 2023 +0800

    Refactor TypedSPILoader (#27948)
    
    * Remove default impl of TypedSPI.getType
    
    * Refactor TypedSPILoader
    
    * Refactor TypedSPILoader
    
    * Refactor TypedSPILoader
---
 agent/plugins/metrics/core/pom.xml                 |  6 +++
 .../infra/spi/type/typed/TypedSPI.java             |  4 +-
 .../infra/spi/type/typed/TypedSPILoader.java       |  2 +-
 kernel/single/distsql/handler/pom.xml              |  6 +++
 kernel/sql-federation/core/pom.xml                 |  6 +++
 .../parser/OptimizerParserContextFactory.java      | 16 ++----
 .../dialect/OptimizerSQLPropertiesBuilder.java     | 57 ++++++++++++++++++++++
 .../sqlfederation/rule/SQLFederationRule.java      |  6 ++-
 .../compiler/context/OptimizerContextTest.java     |  4 +-
 proxy/frontend/type/mysql/pom.xml                  |  6 +++
 10 files changed, 93 insertions(+), 20 deletions(-)

diff --git a/agent/plugins/metrics/core/pom.xml 
b/agent/plugins/metrics/core/pom.xml
index 8c16c6ecd47..2d6163ea8e3 100644
--- a/agent/plugins/metrics/core/pom.xml
+++ b/agent/plugins/metrics/core/pom.xml
@@ -80,6 +80,12 @@
             <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-fixture-database</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
             
<artifactId>shardingsphere-data-pipeline-distsql-statement</artifactId>
diff --git 
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPI.java
 
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPI.java
index 8abf10627e3..a306505b101 100644
--- 
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPI.java
+++ 
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPI.java
@@ -39,9 +39,7 @@ public interface TypedSPI {
      * 
      * @return type
      */
-    default Object getType() {
-        return null;
-    }
+    Object getType();
     
     /**
      * Get type aliases.
diff --git 
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
 
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
index b5634480fe4..718e09fd4b8 100644
--- 
a/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
+++ 
b/infra/spi/src/main/java/org/apache/shardingsphere/infra/spi/type/typed/TypedSPILoader.java
@@ -62,7 +62,7 @@ public final class TypedSPILoader {
                 return Optional.of(each);
             }
         }
-        return findDefaultService(serviceInterface);
+        return Optional.empty();
     }
     
     private static <T extends TypedSPI> Optional<T> findDefaultService(final 
Class<T> serviceInterface) {
diff --git a/kernel/single/distsql/handler/pom.xml 
b/kernel/single/distsql/handler/pom.xml
index 81d0808acdf..68bb6cd183c 100644
--- a/kernel/single/distsql/handler/pom.xml
+++ b/kernel/single/distsql/handler/pom.xml
@@ -48,6 +48,12 @@
             <version>${project.version}</version>
         </dependency>
         
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-fixture-database</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-test-util</artifactId>
diff --git a/kernel/sql-federation/core/pom.xml 
b/kernel/sql-federation/core/pom.xml
index 00abb1a2262..d5bab530c24 100644
--- a/kernel/sql-federation/core/pom.xml
+++ b/kernel/sql-federation/core/pom.xml
@@ -112,6 +112,12 @@
             <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-fixture-database</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-test-util</artifactId>
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/parser/OptimizerParserContextFactory.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/parser/OptimizerParserContextFactory.java
index 879e1014db3..527f241ed43 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/parser/OptimizerParserContextFactory.java
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/parser/OptimizerParserContextFactory.java
@@ -19,10 +19,9 @@ package 
org.apache.shardingsphere.sqlfederation.compiler.context.parser;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import 
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import 
org.apache.shardingsphere.sqlfederation.compiler.context.parser.dialect.OptimizerSQLDialectBuilder;
+import 
org.apache.shardingsphere.sqlfederation.compiler.context.parser.dialect.OptimizerSQLPropertiesBuilder;
 
 import java.util.Map;
 import java.util.Map.Entry;
@@ -43,18 +42,9 @@ public final class OptimizerParserContextFactory {
     public static Map<String, OptimizerParserContext> create(final Map<String, 
ShardingSphereDatabase> databases) {
         Map<String, OptimizerParserContext> result = new ConcurrentHashMap<>();
         for (Entry<String, ShardingSphereDatabase> entry : 
databases.entrySet()) {
-            result.put(entry.getKey(), 
create(entry.getValue().getProtocolType()));
+            DatabaseType databaseType = entry.getValue().getProtocolType();
+            result.put(entry.getKey(), new 
OptimizerParserContext(databaseType, new 
OptimizerSQLPropertiesBuilder(databaseType).build()));
         }
         return result;
     }
-    
-    /**
-     * Create optimizer parser context.
-     * 
-     * @param databaseType database type
-     * @return optimizer parser context
-     */
-    public static OptimizerParserContext create(final DatabaseType 
databaseType) {
-        return new OptimizerParserContext(databaseType, 
DatabaseTypedSPILoader.getService(OptimizerSQLDialectBuilder.class, 
databaseType).build());
-    }
 }
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/parser/dialect/OptimizerSQLPropertiesBuilder.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/parser/dialect/OptimizerSQLPropertiesBuilder.java
new file mode 100644
index 00000000000..149edd9c6b5
--- /dev/null
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/parser/dialect/OptimizerSQLPropertiesBuilder.java
@@ -0,0 +1,57 @@
+/*
+ * 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.sqlfederation.compiler.context.parser.dialect;
+
+import org.apache.calcite.config.CalciteConnectionProperty;
+import org.apache.calcite.config.Lex;
+import org.apache.calcite.sql.fun.SqlLibrary;
+import org.apache.calcite.sql.validate.SqlConformanceEnum;
+import 
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+
+import java.util.Properties;
+
+/**
+ * Optimizer SQL properties builder.
+ */
+public final class OptimizerSQLPropertiesBuilder {
+    
+    private final OptimizerSQLDialectBuilder dialectBuilder;
+    
+    public OptimizerSQLPropertiesBuilder(final DatabaseType databaseType) {
+        dialectBuilder = 
DatabaseTypedSPILoader.findService(OptimizerSQLDialectBuilder.class, 
databaseType).orElse(null);
+    }
+    
+    /**
+     * Build optimizer SQL properties.
+     * 
+     * @return built properties
+     */
+    public Properties build() {
+        return null == dialectBuilder ? buildStandardProperties() : 
dialectBuilder.build();
+    }
+    
+    private Properties buildStandardProperties() {
+        Properties result = new Properties();
+        result.setProperty(CalciteConnectionProperty.LEX.camelName(), 
Lex.JAVA.name());
+        result.setProperty(CalciteConnectionProperty.CONFORMANCE.camelName(), 
SqlConformanceEnum.LENIENT.name());
+        result.setProperty(CalciteConnectionProperty.FUN.camelName(), 
SqlLibrary.STANDARD.fun);
+        
result.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(), 
String.valueOf(Lex.JAVA.caseSensitive));
+        return result;
+    }
+}
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
index 16fde3d719e..b2ba12064d6 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.sqlfederation.rule;
 
 import lombok.Getter;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule;
 import org.apache.shardingsphere.infra.rule.identifier.type.MetaDataHeldRule;
@@ -26,7 +27,7 @@ import 
org.apache.shardingsphere.sqlfederation.api.config.SQLFederationRuleConfi
 import 
org.apache.shardingsphere.sqlfederation.compiler.context.OptimizerContext;
 import 
org.apache.shardingsphere.sqlfederation.compiler.context.OptimizerContextFactory;
 import 
org.apache.shardingsphere.sqlfederation.compiler.context.parser.OptimizerParserContext;
-import 
org.apache.shardingsphere.sqlfederation.compiler.context.parser.OptimizerParserContextFactory;
+import 
org.apache.shardingsphere.sqlfederation.compiler.context.parser.dialect.OptimizerSQLPropertiesBuilder;
 import 
org.apache.shardingsphere.sqlfederation.compiler.context.planner.OptimizerPlannerContext;
 import 
org.apache.shardingsphere.sqlfederation.compiler.context.planner.OptimizerPlannerContextFactory;
 
@@ -49,7 +50,8 @@ public final class SQLFederationRule implements GlobalRule, 
MetaDataHeldRule {
     
     @Override
     public void alterDatabase(final ShardingSphereDatabase database) {
-        OptimizerParserContext parserContext = 
OptimizerParserContextFactory.create(database.getProtocolType());
+        DatabaseType databaseType = database.getProtocolType();
+        OptimizerParserContext parserContext = new 
OptimizerParserContext(databaseType, new 
OptimizerSQLPropertiesBuilder(databaseType).build());
         optimizerContext.putParserContext(database.getName(), parserContext);
         OptimizerPlannerContext plannerContext = 
OptimizerPlannerContextFactory.create(database, parserContext, 
optimizerContext.getSqlParserRule());
         optimizerContext.putPlannerContext(database.getName(), plannerContext);
diff --git 
a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/context/OptimizerContextTest.java
 
b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/context/OptimizerContextTest.java
index 76ace435a67..86283a9c286 100644
--- 
a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/context/OptimizerContextTest.java
+++ 
b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/context/OptimizerContextTest.java
@@ -19,7 +19,9 @@ package 
org.apache.shardingsphere.sqlfederation.compiler.context;
 
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.parser.rule.SQLParserRule;
 import 
org.apache.shardingsphere.sqlfederation.compiler.context.parser.OptimizerParserContext;
 import 
org.apache.shardingsphere.sqlfederation.compiler.context.planner.OptimizerPlannerContext;
@@ -55,7 +57,7 @@ class OptimizerContextTest {
     
     private ShardingSphereDatabase createShardingSphereDatabase() {
         ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, 
RETURNS_DEEP_STUBS);
-        when(result.getProtocolType().getType()).thenReturn("MySQL");
+        
when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class,
 "MySQL"));
         return result;
     }
 }
diff --git a/proxy/frontend/type/mysql/pom.xml 
b/proxy/frontend/type/mysql/pom.xml
index 1c75937942c..5a9b54749ca 100644
--- a/proxy/frontend/type/mysql/pom.xml
+++ b/proxy/frontend/type/mysql/pom.xml
@@ -59,5 +59,11 @@
             <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-fixture-database</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Reply via email to