This is an automated email from the ASF dual-hosted git repository.
tuichenchuxin 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 a893632 Fix exception when execute use information_schema statement
(#16497)
a893632 is described below
commit a8936320e150a41a2d753da0f01fa1e317bd29a0
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Mar 30 18:30:11 2022 +0800
Fix exception when execute use information_schema statement (#16497)
* Fix exception when execute use information_schema statement
* Fix checkstyle
---
.../metadata/schema/util/SystemSchemaUtil.java | 49 ++++++++++++++++++++++
.../metadata/schema/util/SystemSchemaUtilTest.java | 43 +++++++++++++++++++
.../jdbc/JDBCDatabaseCommunicationEngine.java | 23 ++++------
.../impl/SchemaAssignedDatabaseBackendHandler.java | 19 ++-------
4 files changed, 102 insertions(+), 32 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/util/SystemSchemaUtil.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/util/SystemSchemaUtil.java
new file mode 100644
index 0000000..089da34
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/util/SystemSchemaUtil.java
@@ -0,0 +1,49 @@
+/*
+ * 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.metadata.schema.util;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+
+import java.util.Collection;
+
+/**
+ * System schema utility class.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class SystemSchemaUtil {
+
+ /**
+ * Judge whether sql statement contains system schema or not.
+ *
+ * @param databaseType databaseType
+ * @param schemaNames schema names
+ * @param sessionSchemaName session schema name
+ * @return whether sql statement contains system schema or not
+ */
+ public static boolean containsSystemSchema(final DatabaseType
databaseType, final Collection<String> schemaNames, final String
sessionSchemaName) {
+ for (String each : schemaNames) {
+ if (!databaseType.getSystemSchemas().contains(each)) {
+ continue;
+ }
+ return true;
+ }
+ return databaseType.getSystemSchemas().contains(sessionSchemaName);
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/util/SystemSchemaUtilTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/util/SystemSchemaUtilTest.java
new file mode 100644
index 0000000..dacacb0
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/util/SystemSchemaUtilTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.metadata.schema.util;
+
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public final class SystemSchemaUtilTest {
+
+ @Test
+ public void assertContainsSystemSchemaForPostgreSQL() {
+ assertTrue(SystemSchemaUtil.containsSystemSchema(new
PostgreSQLDatabaseType(), Arrays.asList("information_schema", "pg_catalog"),
"information_schema"));
+ assertFalse(SystemSchemaUtil.containsSystemSchema(new
PostgreSQLDatabaseType(), Collections.singletonList("sharding_db"),
"sharding_db"));
+ }
+
+ @Test
+ public void assertContainsSystemSchemaForMySQL() {
+ assertTrue(SystemSchemaUtil.containsSystemSchema(new
MySQLDatabaseType(), Arrays.asList("information_schema", "mysql"),
"information_schema"));
+ assertFalse(SystemSchemaUtil.containsSystemSchema(new
MySQLDatabaseType(), Collections.singletonList("sharding_db"), "sharding_db"));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
index f98dc10..da3ef79 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
@@ -20,10 +20,10 @@ package
org.apache.shardingsphere.proxy.backend.communication.jdbc;
import lombok.SneakyThrows;
import org.apache.commons.lang3.concurrent.LazyInitializer;
import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import
org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
-import
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import
org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutorExceptionHandler;
import
org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
@@ -37,6 +37,7 @@ import
org.apache.shardingsphere.infra.federation.executor.FederationContext;
import org.apache.shardingsphere.infra.federation.executor.FederationExecutor;
import
org.apache.shardingsphere.infra.federation.executor.FederationExecutorFactory;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.schema.util.SystemSchemaUtil;
import
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import
org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
@@ -122,8 +123,10 @@ public final class JDBCDatabaseCommunicationEngine extends
DatabaseCommunication
ExecutionContext executionContext =
getKernelProcessor().generateExecutionContext(
logicSQL, getMetaData(),
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps());
// TODO move federation route logic to binder
- if (executionContext.getRouteContext().isFederated()
- ||
containsSystemSchema(logicSQL.getSqlStatementContext().getDatabaseType(),
logicSQL.getSqlStatementContext().getTablesContext().getSchemaNames())) {
+ SQLStatementContext<?> sqlStatementContext =
logicSQL.getSqlStatementContext();
+ String defaultSchemaName =
backendConnection.getConnectionSession().getSchemaName();
+ if (executionContext.getRouteContext().isFederated() ||
(sqlStatementContext instanceof SelectStatementContext
+ &&
SystemSchemaUtil.containsSystemSchema(sqlStatementContext.getDatabaseType(),
sqlStatementContext.getTablesContext().getSchemaNames(), defaultSchemaName))) {
MetaDataContexts metaDataContexts =
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
ResultSet resultSet = doExecuteFederation(logicSQL,
metaDataContexts);
return processExecuteFederation(resultSet, metaDataContexts);
@@ -141,18 +144,6 @@ public final class JDBCDatabaseCommunicationEngine extends
DatabaseCommunication
: processExecuteUpdate(executionContext, result);
}
- private boolean containsSystemSchema(final DatabaseType databaseType,
final Collection<String> schemaNames) {
- if (databaseType instanceof PostgreSQLDatabaseType || databaseType
instanceof OpenGaussDatabaseType) {
- for (String each : schemaNames) {
- if (!databaseType.getSystemSchemas().contains(each)) {
- continue;
- }
- return true;
- }
- }
- return
databaseType.getSystemSchemas().contains(backendConnection.getConnectionSession().getSchemaName());
- }
-
private ResultSet doExecuteFederation(final LogicSQL logicSQL, final
MetaDataContexts metaDataContexts) throws SQLException {
boolean isReturnGeneratedKeys =
logicSQL.getSqlStatementContext().getSqlStatement() instanceof
MySQLInsertStatement;
DatabaseType databaseType =
metaDataContexts.getMetaData(backendConnection.getConnectionSession().getSchemaName()).getResource().getDatabaseType();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/data/impl/SchemaAssignedDatabaseBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/data/impl/SchemaAssignedDatabaseBackendHandler.java
index d82d175..4614fe1 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/data/impl/SchemaAssignedDatabaseBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/data/impl/SchemaAssignedDatabaseBackendHandler.java
@@ -20,10 +20,8 @@ package
org.apache.shardingsphere.proxy.backend.text.data.impl;
import io.vertx.core.Future;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import
org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
-import
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
import
org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
+import org.apache.shardingsphere.infra.metadata.schema.util.SystemSchemaUtil;
import
org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import
org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.JDBCDatabaseCommunicationEngine;
@@ -71,7 +69,8 @@ public final class SchemaAssignedDatabaseBackendHandler
implements DatabaseBacke
}
private void prepareDatabaseCommunicationEngine() throws
RequiredResourceMissedException {
- boolean isSystemSchema =
containsSystemSchema(sqlStatementContext.getDatabaseType(),
sqlStatementContext.getTablesContext().getSchemaNames());
+ boolean isSystemSchema = SystemSchemaUtil.containsSystemSchema(
+ sqlStatementContext.getDatabaseType(),
sqlStatementContext.getTablesContext().getSchemaNames(),
connectionSession.getSchemaName());
if (!isSystemSchema &&
!ProxyContext.getInstance().getMetaData(connectionSession.getSchemaName()).hasDataSource())
{
throw new
RequiredResourceMissedException(connectionSession.getSchemaName());
}
@@ -81,18 +80,6 @@ public final class SchemaAssignedDatabaseBackendHandler
implements DatabaseBacke
databaseCommunicationEngine =
databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatementContext,
sql, connectionSession.getBackendConnection());
}
- private boolean containsSystemSchema(final DatabaseType databaseType,
final Collection<String> schemaNames) {
- if (databaseType instanceof PostgreSQLDatabaseType || databaseType
instanceof OpenGaussDatabaseType) {
- for (String each : schemaNames) {
- if (!databaseType.getSystemSchemas().contains(each)) {
- continue;
- }
- return true;
- }
- }
- return
databaseType.getSystemSchemas().contains(connectionSession.getSchemaName());
- }
-
@Override
public boolean next() throws SQLException {
return databaseCommunicationEngine.next();