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

menghaoran 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 27f06fa  Create CalciteContextFactory and CalciteContext (#8670)
27f06fa is described below

commit 27f06faee41821185b0401375f31c185c28eb5ba
Author: Juan Pan(Trista) <[email protected]>
AuthorDate: Thu Dec 17 17:45:45 2020 +0800

    Create CalciteContextFactory and CalciteContext (#8670)
---
 .../infra/optimize/context/CalciteContext.java     | 69 ++++++++++++++++
 .../optimize/context/CalciteContextFactory.java    | 94 ++++++++++++++++++++++
 2 files changed, 163 insertions(+)

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
new file mode 100644
index 0000000..0752261
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContext.java
@@ -0,0 +1,69 @@
+/*
+ * 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.optimize.context;
+
+import lombok.Getter;
+import org.apache.calcite.config.CalciteConnectionConfig;
+import org.apache.calcite.jdbc.CalciteSchema;
+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.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorUtil;
+import org.apache.calcite.sql2rel.SqlToRelConverter;
+import org.apache.calcite.sql2rel.SqlToRelConverter.Config;
+import org.apache.calcite.sql2rel.StandardConvertletTable;
+
+import java.util.Collections;
+
+/**
+ * Calcite context.
+ *
+ */
+@Getter
+public final class CalciteContext {
+    
+    private final CalciteSchema rootSchema;
+    
+    private final CalciteCatalogReader catalogReader;
+    
+    private final SqlValidator validator;
+    
+    private final SqlToRelConverter relConverter;
+    
+    public CalciteContext(final CalciteConnectionConfig config, final 
RelDataTypeFactory typeFactory, final RelOptCluster cluster, final Schema 
calciteSchema) {
+        rootSchema = CalciteSchema.createRootSchema(true);
+        rootSchema.add(((CalciteSchema) calciteSchema).name, calciteSchema);
+        catalogReader = new CalciteCatalogReader(rootSchema, 
Collections.singletonList(config.schema()), typeFactory, config);
+        validator = 
SqlValidatorUtil.newValidator(SqlStdOperatorTable.instance(), catalogReader, 
typeFactory, SqlValidator.Config.DEFAULT
+                .withLenientOperatorLookup(config.lenientOperatorLookup())
+                .withSqlConformance(config.conformance())
+                .withDefaultNullCollation(config.defaultNullCollation())
+                .withIdentifierExpansion(true));
+        relConverter = createSqlToRelConverter(cluster);
+    }
+    
+    private SqlToRelConverter createSqlToRelConverter(final RelOptCluster 
cluster) {
+        Config config = SqlToRelConverter.config().withTrimUnusedFields(true);
+        ViewExpander expander = (rowType, queryString, schemaPath, viewPath) 
-> null;
+        return new SqlToRelConverter(expander, validator, catalogReader, 
cluster, StandardConvertletTable.INSTANCE, config);
+    }
+}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContextFactory.java
 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContextFactory.java
new file mode 100644
index 0000000..5fe87d9
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/CalciteContextFactory.java
@@ -0,0 +1,94 @@
+/*
+ * 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.optimize.context;
+
+import org.apache.calcite.adapter.enumerable.EnumerableRules;
+import org.apache.calcite.config.CalciteConnectionConfig;
+import org.apache.calcite.config.CalciteConnectionConfigImpl;
+import org.apache.calcite.config.Lex;
+import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
+import org.apache.calcite.plan.ConventionTraitDef;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.volcano.VolcanoPlanner;
+import org.apache.calcite.rel.rules.CoreRules;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.sql.validate.SqlConformanceEnum;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.optimize.schema.CalciteSchemaFactory;
+
+import java.sql.SQLException;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Calcite context.
+ *
+ */
+public final class CalciteContextFactory {
+    
+    private final CalciteConnectionConfig config;
+    
+    private final RelDataTypeFactory typeFactory;
+    
+    private final CalciteSchemaFactory factory;
+    
+    private final RelOptCluster cluster;
+    
+    public CalciteContextFactory(final Map<String, ShardingSphereMetaData> 
metaDataMap) throws SQLException {
+        config = new CalciteConnectionConfigImpl(createProperties());
+        typeFactory = new JavaTypeFactoryImpl();
+        factory = new CalciteSchemaFactory(metaDataMap);
+        cluster = newCluster();
+    }
+    
+    private Properties createProperties() {
+        Properties result = new Properties();
+        result.setProperty("lex", Lex.MYSQL.name());
+        result.setProperty("conformance", SqlConformanceEnum.MYSQL_5.name());
+        return result;
+    }
+    
+    private RelOptCluster newCluster() {
+        RelOptPlanner planner = new VolcanoPlanner();
+        addPlanRules(planner);
+        planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
+        return RelOptCluster.create(planner, new RexBuilder(typeFactory));
+    }
+    
+    private void addPlanRules(final RelOptPlanner planner) {
+        planner.addRule(CoreRules.PROJECT_TO_CALC);
+        planner.addRule(CoreRules.FILTER_TO_CALC);
+        planner.addRule(EnumerableRules.ENUMERABLE_LIMIT_RULE);
+        planner.addRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
+        planner.addRule(EnumerableRules.ENUMERABLE_SORT_RULE);
+        planner.addRule(EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE);
+        planner.addRule(EnumerableRules.ENUMERABLE_CALC_RULE);
+    }
+    
+    /**
+     * Create.
+     *
+     * @param schema schema
+     * @return calcite context
+     */
+    public CalciteContext create(final String schema) {
+        return new CalciteContext(config, typeFactory, cluster, 
factory.create(schema));
+    }
+}

Reply via email to