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 d49c1ef add CalciteExecutionContextGenerator
CalciteExecutionSQLGenerator (#8951)
d49c1ef is described below
commit d49c1efae7f207303e8b6197d4b81f09108a78af
Author: Juan Pan(Trista) <[email protected]>
AuthorDate: Fri Jan 8 17:38:57 2021 +0800
add CalciteExecutionContextGenerator CalciteExecutionSQLGenerator (#8951)
* add CalciteExecutionContextGenerator CalciteExecutionSQLGenerator
* check style
---
.../ShardingSQLRewriteContextDecorator.java | 3 +
.../optimize/schema/CalciteLogicSchemaFactory.java | 1 -
.../generator/CalciteLogicSchemaGenerator.java | 3 +-
.../generator/CalciteLogicTableGenerator.java | 7 +-
.../optimize/schema/row/CalciteRowExecutor.java | 9 ++-
.../schema/table/AbstractCalciteTable.java | 2 +
.../schema/table/CalciteFilterableTable.java | 13 ++--
.../execute/CalciteExecutionContextGenerator.java | 89 ++++++++++++++++++++++
.../execute/CalciteExecutionSQLGenerator.java | 44 +++++++++++
9 files changed, 159 insertions(+), 12 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
index 6b043dc..29a9402 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
@@ -37,6 +37,9 @@ public final class ShardingSQLRewriteContextDecorator
implements SQLRewriteConte
@SuppressWarnings("unchecked")
@Override
public void decorate(final ShardingRule shardingRule, final
ConfigurationProperties props, final SQLRewriteContext sqlRewriteContext, final
RouteContext routeContext) {
+ if (routeContext.isToCalcite()) {
+ return;
+ }
for (ParameterRewriter each : new
ShardingParameterRewriterBuilder(shardingRule,
routeContext).getParameterRewriters(sqlRewriteContext.getSchema())) {
if (!sqlRewriteContext.getParameters().isEmpty() &&
each.isNeedRewrite(sqlRewriteContext.getSqlStatementContext())) {
each.rewrite(sqlRewriteContext.getParameterBuilder(),
sqlRewriteContext.getSqlStatementContext(), sqlRewriteContext.getParameters());
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 5b332e5..0d9f8f2 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
@@ -59,4 +59,3 @@ public final class CalciteLogicSchemaFactory {
return schemas.get(name).create(executor);
}
}
-
diff --git
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/generator/CalciteLogicSchemaGenerator.java
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/generator/CalciteLogicSchemaGenerator.java
index 3752369..3f2ec8a 100644
---
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/generator/CalciteLogicSchemaGenerator.java
+++
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/generator/CalciteLogicSchemaGenerator.java
@@ -53,7 +53,8 @@ public final class CalciteLogicSchemaGenerator {
Map<String, Collection<DataNode>> tableDataNodes =
getTableDataNodes(dataNodeRules);
Map<String, Collection<String>> dataSourceRules =
getDataSourceRules(metaData);
for (Map.Entry<String, Collection<DataNode>> entry :
tableDataNodes.entrySet()) {
- tables.put(entry.getKey(), new
CalciteLogicTableGenerator(metaData.getResource().getDataSources(),
dataSourceRules, entry.getValue(), metaData.getResource().getDatabaseType()));
+ tables.put(entry.getKey(),
+ new CalciteLogicTableGenerator(entry.getKey(),
metaData.getResource().getDataSources(), dataSourceRules, entry.getValue(),
metaData.getResource().getDatabaseType()));
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/generator/CalciteLogicTableGenerator.java
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/generator/CalciteLogicTableGenerator.java
index f447f04..335a705 100644
---
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/generator/CalciteLogicTableGenerator.java
+++
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/generator/CalciteLogicTableGenerator.java
@@ -43,12 +43,15 @@ import java.util.Optional;
*/
public final class CalciteLogicTableGenerator {
+ private final String name;
+
private final TableMetaData tableMetaData;
private final RelProtoDataType relProtoDataType;
- public CalciteLogicTableGenerator(final Map<String, DataSource>
dataSources, final Map<String, Collection<String>> dataSourceRules,
+ 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();
}
@@ -87,6 +90,6 @@ public final class CalciteLogicTableGenerator {
* @return table
*/
public Table create(final CalciteRowExecutor executor) {
- return new CalciteFilterableTable(tableMetaData, relProtoDataType,
executor);
+ return new CalciteFilterableTable(name, tableMetaData,
relProtoDataType, executor);
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/row/CalciteRowExecutor.java
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/row/CalciteRowExecutor.java
index f81b382..b283a13 100644
---
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/row/CalciteRowExecutor.java
+++
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/row/CalciteRowExecutor.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.optimize.schema.row;
+import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
@@ -52,18 +53,20 @@ public final class CalciteRowExecutor {
private final JDBCExecutor jdbcExecutor;
- private final ExecutionContext executionContext;
+ @Getter
+ private final ExecutionContext initialExecutionContext;
private final JDBCExecutorCallback<? extends ExecuteResult> callback;
/**
* Execute.
*
+ * @param context context
* @return a query result list
*/
- public Collection<QueryResult> execute() {
+ public Collection<QueryResult> execute(final ExecutionContext context) {
try {
- Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups =
createExecutionGroups(executionContext);
+ Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups =
createExecutionGroups(context);
return jdbcExecutor.execute(executionGroups,
callback).stream().map(each -> (QueryResult) each).collect(Collectors.toList());
} catch (final SQLException ex) {
throw new ShardingSphereException(ex);
diff --git
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/AbstractCalciteTable.java
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/AbstractCalciteTable.java
index b421683..e325967 100644
---
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/AbstractCalciteTable.java
+++
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/AbstractCalciteTable.java
@@ -34,6 +34,8 @@ import
org.apache.shardingsphere.infra.optimize.schema.row.CalciteRowExecutor;
@RequiredArgsConstructor
public abstract class AbstractCalciteTable extends AbstractTable {
+ private final String name;
+
private final TableMetaData tableMetaData;
private final RelProtoDataType relProtoDataType;
diff --git
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/CalciteFilterableTable.java
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/CalciteFilterableTable.java
index ee0a171..381579f 100644
---
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/CalciteFilterableTable.java
+++
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/CalciteFilterableTable.java
@@ -25,8 +25,10 @@ import org.apache.calcite.rel.type.RelProtoDataType;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.schema.ProjectableFilterableTable;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
-import org.apache.shardingsphere.infra.optimize.schema.row.CalciteRowExecutor;
import
org.apache.shardingsphere.infra.optimize.schema.row.CalciteRowEnumerator;
+import org.apache.shardingsphere.infra.optimize.schema.row.CalciteRowExecutor;
+import
org.apache.shardingsphere.infra.optimize.schema.table.execute.CalciteExecutionContextGenerator;
+import
org.apache.shardingsphere.infra.optimize.schema.table.execute.CalciteExecutionSQLGenerator;
import java.util.List;
@@ -36,19 +38,20 @@ import java.util.List;
*/
public final class CalciteFilterableTable extends AbstractCalciteTable
implements ProjectableFilterableTable {
- public CalciteFilterableTable(final TableMetaData tableMetaData, final
RelProtoDataType relProtoDataType,
+ public CalciteFilterableTable(final String name, final TableMetaData
tableMetaData, final RelProtoDataType relProtoDataType,
final CalciteRowExecutor executor) {
- super(tableMetaData, relProtoDataType, executor);
+ super(name, tableMetaData, relProtoDataType, executor);
}
@Override
public Enumerable<Object[]> scan(final DataContext root, final
List<RexNode> filters, final int[] projects) {
- // TODO : use projects and filters
return new AbstractEnumerable<Object[]>() {
@Override
public Enumerator<Object[]> enumerator() {
- return new CalciteRowEnumerator(getExecutor().execute());
+ CalciteExecutionContextGenerator generator =
+ new CalciteExecutionContextGenerator(getName(),
getExecutor().getInitialExecutionContext(), new
CalciteExecutionSQLGenerator(root, filters, projects));
+ return new
CalciteRowEnumerator(getExecutor().execute(generator.generate()));
}
};
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/execute/CalciteExecutionContextGenerator.java
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/execute/CalciteExecutionContextGenerator.java
new file mode 100644
index 0000000..5a4beb1
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/execute/CalciteExecutionContextGenerator.java
@@ -0,0 +1,89 @@
+/*
+ * 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.schema.table.execute;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
+import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
+import org.apache.shardingsphere.infra.executor.sql.context.SQLUnit;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+
+/**
+ * Calcite table execution context generator.
+ */
+@RequiredArgsConstructor
+public final class CalciteExecutionContextGenerator {
+
+ private final String table;
+
+ private final ExecutionContext initialExecutionContext;
+
+ private final CalciteExecutionSQLGenerator filter;
+
+ /**
+ * Create execution context.
+ *
+ * @return execution context
+ */
+ public ExecutionContext generate() {
+ RouteContext routeContext =
getRouteContext(initialExecutionContext.getRouteContext());
+ return new
ExecutionContext(initialExecutionContext.getSqlStatementContext(),
+ getExecutionUnits(routeContext.getRouteUnits(), filter),
routeContext);
+ }
+
+ private Collection<ExecutionUnit> getExecutionUnits(final
Collection<RouteUnit> routeUnits, final CalciteExecutionSQLGenerator filter) {
+ Collection<ExecutionUnit> result = new LinkedHashSet<>();
+ for (RouteUnit each: routeUnits) {
+ for (RouteMapper mapper : each.getTableMappers()) {
+ if (mapper.getLogicName().equals(table)) {
+ result.add(new
ExecutionUnit(each.getDataSourceMapper().getActualName(),
+ new
SQLUnit(filter.generate(mapper.getActualName()), Collections.emptyList(),
Collections.singletonList(mapper))));
+ }
+ }
+ }
+ return result;
+ }
+
+ private RouteContext getRouteContext(final RouteContext routeContext) {
+ RouteContext result = new RouteContext();
+ result.getRouteUnits().addAll(getRouteUnits(routeContext));
+ return result;
+ }
+
+ private Collection<RouteUnit> getRouteUnits(final RouteContext
routeContext) {
+ Collection<RouteUnit> result = new LinkedHashSet<>();
+ for (RouteUnit each : routeContext.getRouteUnits()) {
+ RouteUnit routeUnit = new RouteUnit(each.getDataSourceMapper(),
new LinkedHashSet<>());
+ for (RouteMapper mapper : each.getTableMappers()) {
+ if (mapper.getLogicName().equals(table)) {
+ routeUnit.getTableMappers().add(mapper);
+ }
+ }
+ if (!routeUnit.getTableMappers().isEmpty()) {
+ result.add(routeUnit);
+ }
+ }
+ return result;
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/execute/CalciteExecutionSQLGenerator.java
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/execute/CalciteExecutionSQLGenerator.java
new file mode 100644
index 0000000..07ae151
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/schema/table/execute/CalciteExecutionSQLGenerator.java
@@ -0,0 +1,44 @@
+/*
+ * 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.schema.table.execute;
+
+import org.apache.calcite.DataContext;
+import org.apache.calcite.rex.RexNode;
+
+import java.util.List;
+
+/**
+ * Calcite execution sql generator.
+ */
+public final class CalciteExecutionSQLGenerator {
+
+ public CalciteExecutionSQLGenerator(final DataContext root, final
List<RexNode> filters, final int[] projects) {
+ // TODO
+ }
+
+ /**
+ * Create sql.
+ *
+ * @param table table
+ * @return sql
+ */
+ public String generate(final String table) {
+ // TODO
+ return String.format("SELECT * FROM %s", table);
+ }
+}