You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/06/09 02:01:28 UTC
[shardingsphere] branch master updated: optimize some calcite
select error (#10719)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 34338c9 optimize some calcite select error (#10719)
34338c9 is described below
commit 34338c93c9e4b36c8ae7a11bfb1227e99176d6ff
Author: Zhengqiang Duan <st...@gmail.com>
AuthorDate: Wed Jun 9 10:00:45 2021 +0800
optimize some calcite select error (#10719)
* optimize calcite select logic
* fix test case error
* fix NPE
* extract trim semicolon to sql util class
* fix checkstyle
---
.../postgresql/constant/PostgreSQLBinaryColumnType.java | 1 +
.../executor/sql/federate/execute/FederateJDBCExecutor.java | 5 +++--
.../sql/federate/schema/row/FederateRowExecutor.java | 7 +++++--
.../infra/optimize/context/OptimizeContextFactory.java | 4 ++--
.../optimize/core/metadata/FederateSchemaMetadatas.java | 10 ++++++++++
.../shardingsphere/sql/parser/sql/common/util/SQLUtil.java | 12 ++++++++++++
6 files changed, 33 insertions(+), 6 deletions(-)
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLBinaryColumnType.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLBinaryColumnType.java
index 38336e0..e204ec0 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLBinaryColumnType.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLBinaryColumnType.java
@@ -161,6 +161,7 @@ public enum PostgreSQLBinaryColumnType implements BinaryColumnType {
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.INTEGER, POSTGRESQL_TYPE_INT4);
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.BIGINT, POSTGRESQL_TYPE_INT8);
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.NUMERIC, POSTGRESQL_TYPE_NUMERIC);
+ JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.DECIMAL, POSTGRESQL_TYPE_NUMERIC);
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.REAL, POSTGRESQL_TYPE_FLOAT4);
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.DOUBLE, POSTGRESQL_TYPE_FLOAT8);
JDBC_TYPE_AND_COLUMN_TYPE_MAP.put(Types.CHAR, POSTGRESQL_TYPE_CHAR);
diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/federate/execute/FederateJDBCExecutor.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/federate/execute/FederateJDBCExecutor.java
index bc17b25..824b275 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/federate/execute/FederateJDBCExecutor.java
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/federate/execute/FederateJDBCExecutor.java
@@ -32,6 +32,7 @@ import org.apache.shardingsphere.infra.executor.sql.federate.schema.row.Federate
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.ExecutorJDBCManager;
import org.apache.shardingsphere.infra.optimize.context.OptimizeContextFactory;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -107,7 +108,7 @@ public final class FederateJDBCExecutor implements FederateExecutor {
private ResultSet execute(final ExecutionContext executionContext, final JDBCExecutorCallback<? extends ExecuteResult> callback) throws SQLException {
SQLUnit sqlUnit = executionContext.getExecutionUnits().iterator().next().getSqlUnit();
- PreparedStatement statement = getConnection(executionContext, callback).prepareStatement(sqlUnit.getSql());
+ PreparedStatement statement = getConnection(executionContext, callback).prepareStatement(SQLUtil.trimSemicolon(sqlUnit.getSql()));
setParameters(statement, sqlUnit.getParameters());
this.statement = statement;
return statement.executeQuery();
@@ -129,7 +130,7 @@ public final class FederateJDBCExecutor implements FederateExecutor {
private void addSchema(final CalciteConnection calciteConnection, final ExecutionContext executionContext, final JDBCExecutorCallback<? extends ExecuteResult> callback) throws SQLException {
FederateRowExecutor executor = new FederateRowExecutor(rules, props, jdbcManager, jdbcExecutor, executionContext, callback);
- FederateLogicSchema logicSchema = new FederateLogicSchema(factory.getSchemaMetadatas().getDefaultSchemaMetadata(), executor);
+ FederateLogicSchema logicSchema = new FederateLogicSchema(factory.getSchemaMetadatas().getSchemaMetadataBySchemaName(schema), executor);
calciteConnection.getRootSchema().add(schema, logicSchema);
calciteConnection.setSchema(schema);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/federate/schema/row/FederateRowExecutor.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/federate/schema/row/FederateRowExecutor.java
index 30e1dd9..ef40fdc 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/federate/schema/row/FederateRowExecutor.java
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/federate/schema/row/FederateRowExecutor.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKe
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
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.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;
@@ -95,8 +96,10 @@ public final class FederateRowExecutor {
private ExecutionGroupContext<JDBCExecutionUnit> createExecutionGroupContext(final ExecutionContext executionContext) throws SQLException {
// TODO Set parameters for StatementOption
int maxConnectionsSizePerQuery = props.getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY);
+ Collection<ExecutionUnit> executionUnits = executionContext.getExecutionUnits();
+ String type = executionUnits.stream().anyMatch(each -> !each.getSqlUnit().getParameters().isEmpty()) ? JDBCDriverType.PREPARED_STATEMENT : JDBCDriverType.STATEMENT;
DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine = new DriverExecutionPrepareEngine<>(
- JDBCDriverType.STATEMENT, maxConnectionsSizePerQuery, jdbcManager, new StatementOption(true), rules);
- return prepareEngine.prepare(executionContext.getRouteContext(), executionContext.getExecutionUnits());
+ type, maxConnectionsSizePerQuery, jdbcManager, new StatementOption(true), rules);
+ return prepareEngine.prepare(executionContext.getRouteContext(), executionUnits);
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/OptimizeContextFactory.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/OptimizeContextFactory.java
index 25b290b..c3485a9 100644
--- a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/OptimizeContextFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/context/OptimizeContextFactory.java
@@ -51,8 +51,8 @@ import org.apache.shardingsphere.infra.database.type.dialect.SQL92DatabaseType;
import org.apache.shardingsphere.infra.database.type.dialect.SQLServerDatabaseType;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.optimize.core.plan.PlannerInitializer;
import org.apache.shardingsphere.infra.optimize.core.metadata.FederateSchemaMetadatas;
+import org.apache.shardingsphere.infra.optimize.core.plan.PlannerInitializer;
import java.util.Collections;
import java.util.Map;
@@ -120,7 +120,7 @@ public final class OptimizeContextFactory {
}
if (databaseType instanceof PostgreSQLDatabaseType) {
// TODO No suitable type of Lex and conformance
- properties.setProperty(LEX_CAMEL_NAME, Lex.ORACLE.name());
+ properties.setProperty(LEX_CAMEL_NAME, Lex.JAVA.name());
properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.BABEL.name());
// properties.setProperty(CONFORMANCE_CAMEL_NAME, SqlConformanceEnum.LENIENT.name());
return;
diff --git a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/metadata/FederateSchemaMetadatas.java b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/metadata/FederateSchemaMetadatas.java
index 8f59adc..878f7ad 100644
--- a/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/metadata/FederateSchemaMetadatas.java
+++ b/shardingsphere-infra/shardingsphere-infra-optimize/src/main/java/org/apache/shardingsphere/infra/optimize/core/metadata/FederateSchemaMetadatas.java
@@ -53,4 +53,14 @@ public final class FederateSchemaMetadatas {
public FederateSchemaMetadata getDefaultSchemaMetadata() {
return schemas.get(DefaultSchema.LOGIC_NAME);
}
+
+ /**
+ * Get schema metadata by schema name.
+ *
+ * @param schemaName schema name
+ * @return schema metadata
+ */
+ public FederateSchemaMetadata getSchemaMetadataBySchemaName(final String schemaName) {
+ return schemas.get(schemaName);
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLUtil.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLUtil.java
index bf32fd8..9fe01d3 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLUtil.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/util/SQLUtil.java
@@ -66,6 +66,8 @@ import java.util.List;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLUtil {
+ private static final String SQL_END = ";";
+
/**
* Get exactly number value and type.
*
@@ -244,4 +246,14 @@ public final class SQLUtil {
}
return new CommonExpressionSegment(startIndex, stopIndex, text);
}
+
+ /**
+ * Trim the semicolon of sql.
+ *
+ * @param sql SQL to be trim
+ * @return sql without semicolon
+ */
+ public static String trimSemicolon(final String sql) {
+ return sql.endsWith(SQL_END) ? sql.substring(0, sql.length() - 1) : sql;
+ }
}