You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/01/06 05:28:09 UTC

[GitHub] [shardingsphere] JiekerTime commented on a change in pull request #8897: Support different Database types for Query optimization and fix some bugs

JiekerTime commented on a change in pull request #8897:
URL: https://github.com/apache/shardingsphere/pull/8897#discussion_r552377091



##########
File path: shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContextFactory.java
##########
@@ -54,68 +63,106 @@
  */
 public final class CalciteContextFactory {
 
-    private final Properties properties;
-    
+    private static final String LEX_CAMEL_NAME = CalciteConnectionProperty.LEX.camelName();
+
+    private static final String CONFORMANCE_CAMEL_NAME = CalciteConnectionProperty.CONFORMANCE.camelName();
+
+    private final Properties properties = new Properties();
+
     private final CalciteConnectionConfig connectionConfig;
-    
+
     private final Config parserConfig;
-    
+
     private final RelDataTypeFactory typeFactory;
-    
+
     private final CalciteLogicSchemaFactory factory;
-    
+
     private final RelOptCluster cluster;
-    
+
     public CalciteContextFactory(final Map<String, ShardingSphereMetaData> metaDataMap) {
-        properties = createProperties();
+        factory = new CalciteLogicSchemaFactory(metaDataMap);
+        DatabaseType databaseType = metaDataMap.values().iterator().next().getResource().getDatabaseType();
+        assert databaseType != null;
+        initProperties(databaseType);
         connectionConfig = new CalciteConnectionConfigImpl(properties);
         parserConfig = SqlParser.config()
                 .withLex(connectionConfig.lex())
                 .withIdentifierMaxLength(SqlParser.DEFAULT_IDENTIFIER_MAX_LENGTH)
                 .withConformance(connectionConfig.conformance())
                 .withParserFactory(SqlParserImpl.FACTORY);
         typeFactory = new JavaTypeFactoryImpl();
-        factory = new CalciteLogicSchemaFactory(metaDataMap);
         cluster = newCluster();
     }
-    
-    private Properties createProperties() {
-        // TODO Not only MySQL here.
-        Properties result = new Properties();
-        result.setProperty(CalciteConnectionProperty.LEX.camelName(), Lex.MYSQL.name());
-        result.setProperty(CalciteConnectionProperty.CONFORMANCE.camelName(), SqlConformanceEnum.MYSQL_5.name());
-        return result;
+
+    private void initProperties(final DatabaseType databaseType) {
+        // TODO Logic could be improved.
+        if (databaseType instanceof H2DatabaseType) {
+            // TODO No suitable type of Lex
+            this.properties.setProperty(LEX_CAMEL_NAME, Lex.MYSQL.name());
+            this.properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.DEFAULT.name());
+            return;
+        }
+        if (databaseType instanceof MariaDBDatabaseType) {
+            this.properties.setProperty(LEX_CAMEL_NAME, Lex.MYSQL.name());
+            this.properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.MYSQL_5.name());
+            return;
+        }
+        if (databaseType instanceof MySQLDatabaseType) {
+            this.properties.setProperty(LEX_CAMEL_NAME, Lex.MYSQL.name());
+            this.properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.MYSQL_5.name());
+            return;
+        }
+        if (databaseType instanceof OracleDatabaseType) {
+            this.properties.setProperty(LEX_CAMEL_NAME, Lex.ORACLE.name());
+            this.properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.ORACLE_12.name());
+            return;
+        }
+        if (databaseType instanceof PostgreSQLDatabaseType) {
+            // TODO No suitable type of Lex and conformance
+            this.properties.setProperty(LEX_CAMEL_NAME, Lex.MYSQL.name());
+            this.properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.DEFAULT.name());
+            return;
+        }
+        if (databaseType instanceof SQL92DatabaseType) {
+            // TODO No suitable type of Lex
+            this.properties.setProperty(LEX_CAMEL_NAME, Lex.MYSQL.name());
+            this.properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.STRICT_92.name());
+            return;
+        }
+        if (databaseType instanceof SQLServerDatabaseType) {
+            this.properties.setProperty(LEX_CAMEL_NAME, Lex.SQL_SERVER.name());
+            this.properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.SQL_SERVER_2008.name());
+            return;
+        }
+        throw new ShardingSphereException("No matching DatabaseType found");

Review comment:
       In order to maintain the robustness of the code and avoid some unknown errors, please allow me to keep this part....




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org