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 2026/09/30 13:17:26 UTC
(shardingsphere) branch master updated: Refactor SQL federation processor into a concrete class (#40021)
This is an automated email from the ASF dual-hosted git repository.
terrymanu 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 ab2bdff429d Refactor SQL federation processor into a concrete class (#40021)
ab2bdff429d is described below
commit ab2bdff429dccc90b3c00cdea71b4f863ea264d1
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Sep 30 21:17:19 2026 +0800
Refactor SQL federation processor into a concrete class (#40021)
---
.../update/AlterSQLFederationRuleExecutor.java | 12 +-
.../engine/processor/SQLFederationProcessor.java | 168 ++++++++++++++++-
.../processor/SQLFederationProcessorFactory.java | 5 +-
.../impl/StandardSQLFederationProcessor.java | 204 ---------------------
...orTest.java => SQLFederationProcessorTest.java} | 9 +-
5 files changed, 174 insertions(+), 224 deletions(-)
diff --git a/kernel/sql-federation/distsql/handler/src/main/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutor.java b/kernel/sql-federation/distsql/handler/src/main/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutor.java
index f5b25b0764a..d9edeafd31c 100644
--- a/kernel/sql-federation/distsql/handler/src/main/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutor.java
+++ b/kernel/sql-federation/distsql/handler/src/main/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutor.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.sqlfederation.distsql.handler.update;
import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
+import org.apache.shardingsphere.infra.config.rule.validator.RuleConfigurationValidator;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
@@ -44,9 +45,7 @@ public final class AlterSQLFederationRuleExecutor implements GlobalRuleDefinitio
: createCacheOption(rule.getConfiguration().getExecutionPlanCache(), sqlStatement.getExecutionPlanCache());
String providerType = null == sqlStatement.getProviderType() ? rule.getConfiguration().getProviderType() : sqlStatement.getProviderType();
SQLFederationRuleConfiguration result = new SQLFederationRuleConfiguration(sqlFederationEnabled, allQueryUseSQLFederation, executionPlanCache, providerType);
- if (result.isSqlFederationEnabled()) {
- TypedSPILoader.getService(SQLFederationProvider.class, result.getProviderType());
- }
+ validate(result);
return result;
}
@@ -56,6 +55,13 @@ public final class AlterSQLFederationRuleExecutor implements GlobalRuleDefinitio
return new SQLFederationCacheOption(initialCapacity, maximumSize);
}
+ private void validate(final SQLFederationRuleConfiguration ruleConfig) {
+ RuleConfigurationValidator.validate(ruleConfig);
+ if (ruleConfig.isSqlFederationEnabled()) {
+ TypedSPILoader.getService(SQLFederationProvider.class, ruleConfig.getProviderType());
+ }
+ }
+
@Override
public Class<AlterSQLFederationRuleStatement> getType() {
return AlterSQLFederationRuleStatement.class;
diff --git a/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessor.java b/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessor.java
index 20816fc85c7..1ad1a082aa8 100644
--- a/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessor.java
+++ b/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessor.java
@@ -17,25 +17,71 @@
package org.apache.shardingsphere.sqlfederation.provider.calcite.engine.processor;
+import lombok.RequiredArgsConstructor;
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableRel.Prefer;
+import org.apache.calcite.linq4j.Enumerator;
import org.apache.calcite.plan.Convention;
+import org.apache.calcite.runtime.Bindable;
import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.Table;
+import org.apache.shardingsphere.database.connector.core.metadata.database.metadata.DialectDatabaseMetaData;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.infra.binder.context.segment.select.pagination.PaginationContext;
+import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
+import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.context.statement.type.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
+import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine;
+import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sqlfederation.compiler.SQLFederationExecutionPlan;
import org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContext;
+import org.apache.shardingsphere.sqlfederation.compiler.exception.SQLFederationSchemaNotFoundException;
+import org.apache.shardingsphere.sqlfederation.compiler.metadata.schema.SQLFederationTable;
import org.apache.shardingsphere.sqlfederation.compiler.rel.converter.SQLFederationRelConverter;
import org.apache.shardingsphere.sqlfederation.context.SQLFederationContext;
+import org.apache.shardingsphere.sqlfederation.executor.context.ExecutorBindContext;
+import org.apache.shardingsphere.sqlfederation.executor.context.ExecutorContext;
+import org.apache.shardingsphere.sqlfederation.executor.enumerable.implementor.EnumerableScanImplementor;
+import org.apache.shardingsphere.sqlfederation.provider.calcite.resultset.SQLFederationResultSet;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
/**
* SQL federation processor.
*/
-public interface SQLFederationProcessor {
+@RequiredArgsConstructor
+public final class SQLFederationProcessor {
+
+ private static final BigInteger MIN_PAGINATION_PARAMETER = BigInteger.valueOf(Integer.MIN_VALUE);
+
+ private static final BigInteger MAX_PAGINATION_PARAMETER = BigInteger.valueOf(Integer.MAX_VALUE);
+
+ private final ShardingSphereStatistics statistics;
+
+ private final JDBCExecutor jdbcExecutor;
+
+ private ExecutorContext executorContext;
+
+ public SQLFederationProcessor(final ShardingSphereStatistics statistics) {
+ this(statistics, null);
+ }
/**
* Prepare.
@@ -48,8 +94,46 @@ public interface SQLFederationProcessor {
* @param compilerContext compiler context
* @param schemaPlus SQL federation schema
*/
- void prepare(DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine, JDBCExecutorCallback<? extends ExecuteResult> queryCallback,
- String currentDatabaseName, String currentSchemaName, SQLFederationContext federationContext, CompilerContext compilerContext, SchemaPlus schemaPlus);
+ public void prepare(final DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine, final JDBCExecutorCallback<? extends ExecuteResult> queryCallback,
+ final String currentDatabaseName, final String currentSchemaName, final SQLFederationContext federationContext, final CompilerContext compilerContext,
+ final SchemaPlus schemaPlus) {
+ if (null == schemaPlus) {
+ return;
+ }
+ executorContext = federationContext.isPreview()
+ ? new ExecutorContext(prepareEngine, queryCallback, statistics, currentDatabaseName, currentSchemaName, federationContext.getProcessId())
+ : new ExecutorContext(prepareEngine, jdbcExecutor, queryCallback, statistics, currentDatabaseName, currentSchemaName, federationContext.getProcessId());
+ EnumerableScanImplementor scanImplementor = new EnumerableScanImplementor(federationContext.getQueryContext(), compilerContext, executorContext);
+ SQLStatementContext sqlStatementContext = federationContext.getQueryContext().getSqlStatementContext();
+ Collection<SimpleTableSegment> simpleTables = sqlStatementContext.getTablesContext().getSimpleTables();
+ for (SimpleTableSegment each : simpleTables) {
+ Table table = getTable(currentDatabaseName, currentSchemaName, schemaPlus, each, sqlStatementContext.getSqlStatement().getDatabaseType(), federationContext.getQueryContext().getSql());
+ if (table instanceof SQLFederationTable) {
+ ((SQLFederationTable) table).setScanImplementor(scanImplementor);
+ }
+ }
+ }
+
+ private Table getTable(final String currentDatabaseName, final String currentSchemaName, final SchemaPlus schemaPlus, final SimpleTableSegment tableSegment, final DatabaseType databaseType,
+ final String sql) {
+ DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData();
+ String originalDatabase = tableSegment.getTableName().getTableBoundInfo().map(optional -> optional.getOriginalDatabase().getValue()).orElse(currentDatabaseName);
+ String originalScheme = tableSegment.getTableName().getTableBoundInfo().map(optional -> optional.getOriginalSchema().getValue()).orElse(currentSchemaName);
+ SchemaPlus currentSchemaPlus = dialectDatabaseMetaData.getSchemaOption().getDefaultSchema().isPresent() ? getNestedSchemaPlus(schemaPlus, originalDatabase, originalScheme, sql)
+ : getSchemaPlus(schemaPlus, originalDatabase, sql);
+ return currentSchemaPlus.tables().get(tableSegment.getTableName().getIdentifier().getValue());
+ }
+
+ private SchemaPlus getNestedSchemaPlus(final SchemaPlus schemaPlus, final String databaseName, final String schemaName, final String sql) {
+ SchemaPlus databaseSchema = getSchemaPlus(schemaPlus, databaseName, sql);
+ return getSchemaPlus(databaseSchema, schemaName, sql);
+ }
+
+ private SchemaPlus getSchemaPlus(final SchemaPlus schemaPlus, final String schemaName, final String sql) {
+ SchemaPlus result = schemaPlus.subSchemas().get(schemaName);
+ ShardingSpherePreconditions.checkNotNull(result, () -> new SQLFederationSchemaNotFoundException(schemaName, sql));
+ return result;
+ }
/**
* Release.
@@ -59,7 +143,16 @@ public interface SQLFederationProcessor {
* @param queryContext query context
* @param schemaPlus SQL federation schema
*/
- void release(String currentDatabaseName, String currentSchemaName, QueryContext queryContext, SchemaPlus schemaPlus);
+ public void release(final String currentDatabaseName, final String currentSchemaName, final QueryContext queryContext, final SchemaPlus schemaPlus) {
+ Collection<SimpleTableSegment> simpleTables = queryContext.getSqlStatementContext().getTablesContext().getSimpleTables();
+ for (SimpleTableSegment each : simpleTables) {
+ Table table = getTable(currentDatabaseName, currentSchemaName, schemaPlus,
+ each, queryContext.getSqlStatementContext().getSqlStatement().getDatabaseType(), queryContext.getSql());
+ if (table instanceof SQLFederationTable) {
+ ((SQLFederationTable) table).clearScanImplementor();
+ }
+ }
+ }
/**
* Execute plan.
@@ -72,13 +165,70 @@ public interface SQLFederationProcessor {
* @param schemaPlus SQL federation schema
* @return resultset
*/
- ResultSet executePlan(DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine, JDBCExecutorCallback<? extends ExecuteResult> queryCallback,
- SQLFederationExecutionPlan executionPlan, SQLFederationRelConverter converter, SQLFederationContext federationContext, SchemaPlus schemaPlus);
+ public ResultSet executePlan(final DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine, final JDBCExecutorCallback<? extends ExecuteResult> queryCallback,
+ final SQLFederationExecutionPlan executionPlan, final SQLFederationRelConverter converter, final SQLFederationContext federationContext, final SchemaPlus schemaPlus) {
+ Bindable<Object> executablePlan = SQLFederationExecutionPlan.toBindable(executionPlan.getPhysicalPlan(), Collections.emptyMap(), null, Prefer.ARRAY);
+ SelectStatementContext selectStatementContext = (SelectStatementContext) federationContext.getQueryContext().getSqlStatementContext();
+ Map<String, Object> params = createParameters(selectStatementContext, federationContext.getQueryContext().getParameters());
+ Enumerator<Object> enumerator = executablePlan.bind(new ExecutorBindContext(converter, params)).enumerator();
+ List<Projection> expandProjections = selectStatementContext.getProjectionsContext().getExpandProjections();
+ SQLFederationResultSet result = new SQLFederationResultSet(enumerator, schemaPlus, expandProjections,
+ selectStatementContext.getSqlStatement().getDatabaseType(), executionPlan.getResultColumnType(), federationContext.getProcessId());
+ if (federationContext.isPreview()) {
+ federationContext.getPreviewExecutionUnits().addAll(executorContext.getPreviewExecutionUnits());
+ }
+ return result;
+ }
+
+ private Map<String, Object> createParameters(final SelectStatementContext selectStatementContext, final List<Object> params) {
+ Collection<Integer> paginationParameterIndexes = getPaginationParameterIndexes(selectStatementContext);
+ Map<String, Object> result = new HashMap<>(params.size(), 1F);
+ int index = 0;
+ for (Object each : params) {
+ result.put("?" + index, paginationParameterIndexes.contains(index) ? convertPaginationParameter(each) : each);
+ index++;
+ }
+ return result;
+ }
+
+ private Collection<Integer> getPaginationParameterIndexes(final SelectStatementContext selectStatementContext) {
+ Collection<Integer> result = new LinkedList<>();
+ collectPaginationParameterIndexes(result, selectStatementContext.getPaginationContext());
+ for (SelectStatementContext each : selectStatementContext.getSubqueryContexts().values()) {
+ result.addAll(getPaginationParameterIndexes(each));
+ }
+ return result;
+ }
+
+ private void collectPaginationParameterIndexes(final Collection<Integer> result, final PaginationContext paginationContext) {
+ paginationContext.getOffsetParameterIndex().ifPresent(result::add);
+ paginationContext.getRowCountParameterIndex().ifPresent(result::add);
+ }
+
+ private Object convertPaginationParameter(final Object value) {
+ if (!(value instanceof Number)) {
+ return value;
+ }
+ BigInteger integerValue = getIntegerValue((Number) value);
+ ShardingSpherePreconditions.checkState(integerValue.compareTo(MIN_PAGINATION_PARAMETER) >= 0 && integerValue.compareTo(MAX_PAGINATION_PARAMETER) <= 0,
+ () -> new IllegalArgumentException(String.format("SQL federation pagination parameter value `%s` is out of integer range.", value)));
+ return integerValue.intValue();
+ }
+
+ private BigInteger getIntegerValue(final Number value) {
+ try {
+ return new BigDecimal(value.toString()).toBigIntegerExact();
+ } catch (final NumberFormatException | ArithmeticException ex) {
+ throw new IllegalArgumentException(String.format("SQL federation pagination parameter value `%s` must be an integer.", value), ex);
+ }
+ }
/**
- * Get conversion.
+ * Get convention.
*
- * @return conversion
+ * @return convention
*/
- Convention getConvention();
+ public Convention getConvention() {
+ return EnumerableConvention.INSTANCE;
+ }
}
diff --git a/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessorFactory.java b/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessorFactory.java
index 5cae72822b8..55e72151121 100644
--- a/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessorFactory.java
+++ b/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessorFactory.java
@@ -21,7 +21,6 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.processor.impl.StandardSQLFederationProcessor;
/**
* SQL federation processor factory.
@@ -48,7 +47,7 @@ public final class SQLFederationProcessorFactory {
* @return created instance
*/
public SQLFederationProcessor newInstance(final ShardingSphereStatistics statistics, final JDBCExecutor jdbcExecutor) {
- return new StandardSQLFederationProcessor(statistics, jdbcExecutor);
+ return new SQLFederationProcessor(statistics, jdbcExecutor);
}
/**
@@ -58,6 +57,6 @@ public final class SQLFederationProcessorFactory {
* @return created processor
*/
public SQLFederationProcessor newPreviewInstance(final ShardingSphereStatistics statistics) {
- return new StandardSQLFederationProcessor(statistics);
+ return new SQLFederationProcessor(statistics);
}
}
diff --git a/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/impl/StandardSQLFederationProcessor.java b/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/impl/StandardSQLFederationProcessor.java
deleted file mode 100644
index 7273d28e84a..00000000000
--- a/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/impl/StandardSQLFederationProcessor.java
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- * 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.provider.calcite.engine.processor.impl;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.calcite.adapter.enumerable.EnumerableConvention;
-import org.apache.calcite.adapter.enumerable.EnumerableRel.Prefer;
-import org.apache.calcite.linq4j.Enumerator;
-import org.apache.calcite.plan.Convention;
-import org.apache.calcite.runtime.Bindable;
-import org.apache.calcite.schema.SchemaPlus;
-import org.apache.calcite.schema.Table;
-import org.apache.shardingsphere.database.connector.core.metadata.database.metadata.DialectDatabaseMetaData;
-import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
-import org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.binder.context.segment.select.pagination.PaginationContext;
-import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.binder.context.statement.type.dml.SelectStatementContext;
-import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
-import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
-import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
-import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
-import org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine;
-import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
-import org.apache.shardingsphere.infra.session.query.QueryContext;
-import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.sqlfederation.compiler.SQLFederationExecutionPlan;
-import org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContext;
-import org.apache.shardingsphere.sqlfederation.compiler.exception.SQLFederationSchemaNotFoundException;
-import org.apache.shardingsphere.sqlfederation.compiler.metadata.schema.SQLFederationTable;
-import org.apache.shardingsphere.sqlfederation.compiler.rel.converter.SQLFederationRelConverter;
-import org.apache.shardingsphere.sqlfederation.context.SQLFederationContext;
-import org.apache.shardingsphere.sqlfederation.executor.context.ExecutorBindContext;
-import org.apache.shardingsphere.sqlfederation.executor.context.ExecutorContext;
-import org.apache.shardingsphere.sqlfederation.executor.enumerable.implementor.EnumerableScanImplementor;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.processor.SQLFederationProcessor;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.resultset.SQLFederationResultSet;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Standard SQL federation processor.
- */
-@RequiredArgsConstructor
-public final class StandardSQLFederationProcessor implements SQLFederationProcessor {
-
- private static final BigInteger MIN_PAGINATION_PARAMETER = BigInteger.valueOf(Integer.MIN_VALUE);
-
- private static final BigInteger MAX_PAGINATION_PARAMETER = BigInteger.valueOf(Integer.MAX_VALUE);
-
- private final ShardingSphereStatistics statistics;
-
- private final JDBCExecutor jdbcExecutor;
-
- private ExecutorContext executorContext;
-
- public StandardSQLFederationProcessor(final ShardingSphereStatistics statistics) {
- this(statistics, null);
- }
-
- @Override
- public void prepare(final DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine, final JDBCExecutorCallback<? extends ExecuteResult> queryCallback,
- final String currentDatabaseName, final String currentSchemaName, final SQLFederationContext federationContext, final CompilerContext compilerContext,
- final SchemaPlus schemaPlus) {
- if (null == schemaPlus) {
- return;
- }
- executorContext = federationContext.isPreview()
- ? new ExecutorContext(prepareEngine, queryCallback, statistics, currentDatabaseName, currentSchemaName, federationContext.getProcessId())
- : new ExecutorContext(prepareEngine, jdbcExecutor, queryCallback, statistics, currentDatabaseName, currentSchemaName, federationContext.getProcessId());
- EnumerableScanImplementor scanImplementor = new EnumerableScanImplementor(federationContext.getQueryContext(), compilerContext, executorContext);
- SQLStatementContext sqlStatementContext = federationContext.getQueryContext().getSqlStatementContext();
- Collection<SimpleTableSegment> simpleTables = sqlStatementContext.getTablesContext().getSimpleTables();
- for (SimpleTableSegment each : simpleTables) {
- Table table = getTable(currentDatabaseName, currentSchemaName, schemaPlus, each, sqlStatementContext.getSqlStatement().getDatabaseType(), federationContext.getQueryContext().getSql());
- if (table instanceof SQLFederationTable) {
- ((SQLFederationTable) table).setScanImplementor(scanImplementor);
- }
- }
- }
-
- private Table getTable(final String currentDatabaseName, final String currentSchemaName, final SchemaPlus schemaPlus, final SimpleTableSegment tableSegment, final DatabaseType databaseType,
- final String sql) {
- DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData();
- String originalDatabase = tableSegment.getTableName().getTableBoundInfo().map(optional -> optional.getOriginalDatabase().getValue()).orElse(currentDatabaseName);
- String originalScheme = tableSegment.getTableName().getTableBoundInfo().map(optional -> optional.getOriginalSchema().getValue()).orElse(currentSchemaName);
- SchemaPlus currentSchemaPlus = dialectDatabaseMetaData.getSchemaOption().getDefaultSchema().isPresent() ? getNestedSchemaPlus(schemaPlus, originalDatabase, originalScheme, sql)
- : getSchemaPlus(schemaPlus, originalDatabase, sql);
- return currentSchemaPlus.tables().get(tableSegment.getTableName().getIdentifier().getValue());
- }
-
- private SchemaPlus getNestedSchemaPlus(final SchemaPlus schemaPlus, final String databaseName, final String schemaName, final String sql) {
- SchemaPlus databaseSchema = getSchemaPlus(schemaPlus, databaseName, sql);
- return getSchemaPlus(databaseSchema, schemaName, sql);
- }
-
- private SchemaPlus getSchemaPlus(final SchemaPlus schemaPlus, final String schemaName, final String sql) {
- SchemaPlus result = schemaPlus.subSchemas().get(schemaName);
- ShardingSpherePreconditions.checkNotNull(result, () -> new SQLFederationSchemaNotFoundException(schemaName, sql));
- return result;
- }
-
- @Override
- public void release(final String currentDatabaseName, final String currentSchemaName, final QueryContext queryContext, final SchemaPlus schemaPlus) {
- Collection<SimpleTableSegment> simpleTables = queryContext.getSqlStatementContext().getTablesContext().getSimpleTables();
- for (SimpleTableSegment each : simpleTables) {
- Table table = getTable(currentDatabaseName, currentSchemaName, schemaPlus,
- each, queryContext.getSqlStatementContext().getSqlStatement().getDatabaseType(), queryContext.getSql());
- if (table instanceof SQLFederationTable) {
- ((SQLFederationTable) table).clearScanImplementor();
- }
- }
- }
-
- @Override
- public ResultSet executePlan(final DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine, final JDBCExecutorCallback<? extends ExecuteResult> queryCallback,
- final SQLFederationExecutionPlan executionPlan, final SQLFederationRelConverter converter, final SQLFederationContext federationContext, final SchemaPlus schemaPlus) {
- Bindable<Object> executablePlan = SQLFederationExecutionPlan.toBindable(executionPlan.getPhysicalPlan(), Collections.emptyMap(), null, Prefer.ARRAY);
- SelectStatementContext selectStatementContext = (SelectStatementContext) federationContext.getQueryContext().getSqlStatementContext();
- Map<String, Object> params = createParameters(selectStatementContext, federationContext.getQueryContext().getParameters());
- Enumerator<Object> enumerator = executablePlan.bind(new ExecutorBindContext(converter, params)).enumerator();
- List<Projection> expandProjections = selectStatementContext.getProjectionsContext().getExpandProjections();
- SQLFederationResultSet result = new SQLFederationResultSet(enumerator, schemaPlus, expandProjections,
- selectStatementContext.getSqlStatement().getDatabaseType(), executionPlan.getResultColumnType(), federationContext.getProcessId());
- if (federationContext.isPreview()) {
- federationContext.getPreviewExecutionUnits().addAll(executorContext.getPreviewExecutionUnits());
- }
- return result;
- }
-
- private Map<String, Object> createParameters(final SelectStatementContext selectStatementContext, final List<Object> params) {
- Collection<Integer> paginationParameterIndexes = getPaginationParameterIndexes(selectStatementContext);
- Map<String, Object> result = new HashMap<>(params.size(), 1F);
- int index = 0;
- for (Object each : params) {
- result.put("?" + index, paginationParameterIndexes.contains(index) ? convertPaginationParameter(each) : each);
- index++;
- }
- return result;
- }
-
- private Collection<Integer> getPaginationParameterIndexes(final SelectStatementContext selectStatementContext) {
- Collection<Integer> result = new LinkedList<>();
- collectPaginationParameterIndexes(result, selectStatementContext.getPaginationContext());
- for (SelectStatementContext each : selectStatementContext.getSubqueryContexts().values()) {
- result.addAll(getPaginationParameterIndexes(each));
- }
- return result;
- }
-
- private void collectPaginationParameterIndexes(final Collection<Integer> result, final PaginationContext paginationContext) {
- paginationContext.getOffsetParameterIndex().ifPresent(result::add);
- paginationContext.getRowCountParameterIndex().ifPresent(result::add);
- }
-
- private Object convertPaginationParameter(final Object value) {
- if (!(value instanceof Number)) {
- return value;
- }
- BigInteger integerValue = getIntegerValue((Number) value);
- ShardingSpherePreconditions.checkState(integerValue.compareTo(MIN_PAGINATION_PARAMETER) >= 0 && integerValue.compareTo(MAX_PAGINATION_PARAMETER) <= 0,
- () -> new IllegalArgumentException(String.format("SQL federation pagination parameter value `%s` is out of integer range.", value)));
- return integerValue.intValue();
- }
-
- private BigInteger getIntegerValue(final Number value) {
- try {
- return new BigDecimal(value.toString()).toBigIntegerExact();
- } catch (final NumberFormatException | ArithmeticException ex) {
- throw new IllegalArgumentException(String.format("SQL federation pagination parameter value `%s` must be an integer.", value), ex);
- }
- }
-
- @Override
- public Convention getConvention() {
- return EnumerableConvention.INSTANCE;
- }
-}
diff --git a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/impl/StandardSQLFederationProcessorTest.java b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessorTest.java
similarity index 99%
rename from kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/impl/StandardSQLFederationProcessorTest.java
rename to kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessorTest.java
index 6ea6fdb69e0..c02898b11d8 100644
--- a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/impl/StandardSQLFederationProcessorTest.java
+++ b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/processor/SQLFederationProcessorTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sqlfederation.provider.calcite.engine.processor.impl;
+package org.apache.shardingsphere.sqlfederation.provider.calcite.engine.processor;
import org.apache.calcite.DataContext;
import org.apache.calcite.adapter.enumerable.EnumerableConvention;
@@ -65,7 +65,6 @@ import org.apache.shardingsphere.sqlfederation.compiler.metadata.schema.SQLFeder
import org.apache.shardingsphere.sqlfederation.compiler.rel.converter.SQLFederationRelConverter;
import org.apache.shardingsphere.sqlfederation.context.SQLFederationContext;
import org.apache.shardingsphere.sqlfederation.executor.context.ExecutorBindContext;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.processor.SQLFederationProcessor;
import org.apache.shardingsphere.sqlfederation.provider.calcite.resultset.SQLFederationResultSet;
import org.apache.shardingsphere.sqlfederation.resultset.converter.DialectSQLFederationColumnTypeConverter;
import org.junit.jupiter.api.BeforeEach;
@@ -102,7 +101,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
-class StandardSQLFederationProcessorTest {
+class SQLFederationProcessorTest {
private final String databaseName = "foo_db";
@@ -114,7 +113,7 @@ class StandardSQLFederationProcessorTest {
@BeforeEach
void setUp() {
- processor = new StandardSQLFederationProcessor(mock(), mock());
+ processor = new SQLFederationProcessor(mock(), mock());
}
@SuppressWarnings("unchecked")
@@ -126,7 +125,7 @@ class StandardSQLFederationProcessorTest {
@SuppressWarnings("unchecked")
@Test
void assertPrepareAndReleaseSkipNonFederationTable() {
- processor = new StandardSQLFederationProcessor(mock());
+ processor = new SQLFederationProcessor(mock());
SQLFederationContext federationContext = createFederationContext(true, null);
Table table = mock(Table.class);
SchemaPlus rootSchema = mockFlatSchemaWithTable(table);