You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ch...@apache.org on 2024/12/27 05:39:58 UTC
(shardingsphere) branch master updated: Move multiple logic database check to QueryContext (#34174)
This is an automated email from the ASF dual-hosted git repository.
chengzhang 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 bd99347c129 Move multiple logic database check to QueryContext (#34174)
bd99347c129 is described below
commit bd99347c129842546cddbd2aa98c75dcccc55b68
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Fri Dec 27 13:39:51 2024 +0800
Move multiple logic database check to QueryContext (#34174)
* Move multiple logic database check to QueryContext
* fix unit test
* fix unit test
* fix unit test
* fix pipeline error
---
.../context/segment/table/TablesContext.java | 2 --
.../context/segment/table/TablesContextTest.java | 19 ---------------
.../executor/sql/process/ProcessEngineTest.java | 2 --
.../infra/session/query/QueryContext.java | 28 +++++++++++++++-------
.../proxy/backend/session/ConnectionSession.java | 2 +-
.../bind/OpenGaussComBatchBindExecutorTest.java | 2 --
6 files changed, 20 insertions(+), 35 deletions(-)
diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
index b274f0d8eaa..d7804d9e06a 100644
--- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
+++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.infra.binder.context.segment.table;
import com.cedarsoftware.util.CaseInsensitiveSet;
-import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.ToString;
@@ -110,7 +109,6 @@ public final class TablesContext {
* @return database name
*/
public Optional<String> getDatabaseName() {
- Preconditions.checkState(databaseNames.size() <= 1, "Can not support multiple different database.");
return databaseNames.isEmpty() ? Optional.empty() : Optional.of(databaseNames.iterator().next());
}
diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java
index 20fd15c36b2..d0510d024d6 100644
--- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java
+++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java
@@ -33,7 +33,6 @@ import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
@@ -99,24 +98,6 @@ class TablesContextTest {
assertThat(tablesContext.getDatabaseName().get(), is("sharding_db_1"));
}
- @Test
- void assertGetSchemaNameWithDifferentSchemaAndSameTable() {
- SimpleTableSegment tableSegment1 = createTableSegment("table_1", "tbl_1", "sharding_db_1");
- tableSegment1.setOwner(new OwnerSegment(0, 0, new IdentifierValue("sharding_db_1")));
- SimpleTableSegment tableSegment2 = createTableSegment("table_1", "tbl_1", "sharding_db_2");
- tableSegment2.setOwner(new OwnerSegment(0, 0, new IdentifierValue("sharding_db_2")));
- assertThrows(IllegalStateException.class, () -> new TablesContext(Arrays.asList(tableSegment1, tableSegment2)).getDatabaseName());
- }
-
- @Test
- void assertGetSchemaNameWithDifferentSchemaAndDifferentTable() {
- SimpleTableSegment tableSegment1 = createTableSegment("table_1", "tbl_1", "sharding_db_1");
- tableSegment1.setOwner(new OwnerSegment(0, 0, new IdentifierValue("sharding_db_1")));
- SimpleTableSegment tableSegment2 = createTableSegment("table_2", "tbl_2", "sharding_db_2");
- tableSegment2.setOwner(new OwnerSegment(0, 0, new IdentifierValue("sharding_db_2")));
- assertThrows(IllegalStateException.class, () -> new TablesContext(Arrays.asList(tableSegment1, tableSegment2)).getDatabaseName());
- }
-
@Test
void assertGetSchemaName() {
SimpleTableSegment tableSegment1 = createTableSegment("table_1", "tbl_1", "sharding_db_1");
diff --git a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/process/ProcessEngineTest.java b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/process/ProcessEngineTest.java
index 88450ae0fbd..79f01bd8481 100644
--- a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/process/ProcessEngineTest.java
+++ b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/process/ProcessEngineTest.java
@@ -39,7 +39,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import java.util.Collections;
-import java.util.Optional;
import java.util.UUID;
import static org.mockito.ArgumentMatchers.any;
@@ -62,7 +61,6 @@ class ProcessEngineTest {
@Test
void assertExecuteSQL() {
ConnectionContext connectionContext = mock(ConnectionContext.class);
- when(connectionContext.getCurrentDatabaseName()).thenReturn(Optional.of("foo_db"));
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
ExecutionGroupContext<? extends SQLExecutionUnit> executionGroupContext = mockExecutionGroupContext();
new ProcessEngine().executeSQL(executionGroupContext,
diff --git a/infra/session/src/main/java/org/apache/shardingsphere/infra/session/query/QueryContext.java b/infra/session/src/main/java/org/apache/shardingsphere/infra/session/query/QueryContext.java
index cf092c7cd59..aee2d8fe593 100644
--- a/infra/session/src/main/java/org/apache/shardingsphere/infra/session/query/QueryContext.java
+++ b/infra/session/src/main/java/org/apache/shardingsphere/infra/session/query/QueryContext.java
@@ -17,17 +17,20 @@
package org.apache.shardingsphere.infra.session.query;
+import com.google.common.base.Joiner;
import lombok.Getter;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.NoDatabaseSelectedException;
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException;
+import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
/**
@@ -48,7 +51,7 @@ public final class QueryContext {
private final ShardingSphereMetaData metaData;
- private final String usedDatabaseName;
+ private final Collection<String> usedDatabaseNames;
private final boolean useCache;
@@ -65,15 +68,20 @@ public final class QueryContext {
this.hintValueContext = hintValueContext;
this.connectionContext = connectionContext;
this.metaData = metaData;
- usedDatabaseName = findUsedDatabaseNameFromSQLStatement(sqlStatementContext, connectionContext);
+ usedDatabaseNames = getUsedDatabaseNames(sqlStatementContext, connectionContext);
this.useCache = useCache;
}
- private String findUsedDatabaseNameFromSQLStatement(final SQLStatementContext sqlStatementContext, final ConnectionContext connectionContext) {
+ private Collection<String> getUsedDatabaseNames(final SQLStatementContext sqlStatementContext, final ConnectionContext connectionContext) {
if (sqlStatementContext instanceof TableAvailable) {
- return ((TableAvailable) sqlStatementContext).getTablesContext().getDatabaseName().orElse(connectionContext.getCurrentDatabaseName().orElse(null));
+ Collection<String> result = ((TableAvailable) sqlStatementContext).getTablesContext().getDatabaseNames();
+ return result.isEmpty() ? getCurrentDatabaseNames(connectionContext) : result;
}
- return connectionContext.getCurrentDatabaseName().orElse(null);
+ return getCurrentDatabaseNames(connectionContext);
+ }
+
+ private Collection<String> getCurrentDatabaseNames(final ConnectionContext connectionContext) {
+ return connectionContext.getCurrentDatabaseName().isPresent() ? Collections.singleton(connectionContext.getCurrentDatabaseName().get()) : Collections.emptyList();
}
/**
@@ -82,8 +90,10 @@ public final class QueryContext {
* @return used database
*/
public ShardingSphereDatabase getUsedDatabase() {
- ShardingSpherePreconditions.checkNotNull(usedDatabaseName, NoDatabaseSelectedException::new);
- ShardingSpherePreconditions.checkState(metaData.containsDatabase(usedDatabaseName), () -> new UnknownDatabaseException(usedDatabaseName));
- return metaData.getDatabase(usedDatabaseName);
+ ShardingSpherePreconditions.checkState(usedDatabaseNames.size() <= 1,
+ () -> new UnsupportedSQLOperationException(String.format("Can not support multiple logic databases [%s]", Joiner.on(", ").join(usedDatabaseNames))));
+ String databaseName = usedDatabaseNames.iterator().next();
+ ShardingSpherePreconditions.checkState(metaData.containsDatabase(databaseName), () -> new UnknownDatabaseException(databaseName));
+ return metaData.getDatabase(databaseName);
}
}
diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java
index 636c0ccdc8d..1ea99637676 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSession.java
@@ -119,7 +119,7 @@ public final class ConnectionSession {
* @return used database name
*/
public String getUsedDatabaseName() {
- return null == queryContext ? currentDatabaseName : queryContext.getUsedDatabaseName();
+ return null == queryContext || queryContext.getUsedDatabaseNames().isEmpty() ? currentDatabaseName : queryContext.getUsedDatabaseNames().iterator().next();
}
/**
diff --git a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
index 88f942dd632..ea5629c58c3 100644
--- a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
+++ b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
@@ -62,7 +62,6 @@ import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
-import java.util.Optional;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
@@ -124,7 +123,6 @@ class OpenGaussComBatchBindExecutorTest {
private ConnectionContext mockConnectionContext() {
ConnectionContext result = mock(ConnectionContext.class);
- when(result.getCurrentDatabaseName()).thenReturn(Optional.of("foo_db"));
when(result.getTransactionContext()).thenReturn(mock(TransactionConnectionContext.class));
return result;
}