This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 def6e714f1d Add test cases on ShardingStatisticsTableCollector (#33671)
def6e714f1d is described below

commit def6e714f1d4bc6f0bc88ccdcbf2f8297c8e323b
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Nov 15 12:51:07 2024 +0800

    Add test cases on ShardingStatisticsTableCollector (#33671)
    
    * Add test cases on ShardingStatisticsTableCollector
    
    * Add test cases on ShardingStatisticsTableCollector
    
    * Add test cases on ShardingStatisticsTableCollector
---
 .../data/ShardingStatisticsTableCollector.java     |  19 ++--
 .../MySQLShardingStatisticsTableCollector.java     |   4 +-
 .../OpenGaussShardingStatisticsTableCollector.java |   4 +-
 ...PostgreSQLShardingStatisticsTableCollector.java |   8 +-
 .../data/ShardingStatisticsTableCollectorTest.java |  89 +++++++++++++++
 .../MySQLShardingStatisticsTableCollectorTest.java | 100 +++++++++++++++++
 ...nGaussShardingStatisticsTableCollectorTest.java | 122 +++++++++++++++++++++
 ...greSQLShardingStatisticsTableCollectorTest.java | 101 +++++++++++++++++
 8 files changed, 430 insertions(+), 17 deletions(-)

diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollector.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollector.java
index 73a62d5bb33..b70c2280f3f 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollector.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollector.java
@@ -49,8 +49,8 @@ public final class ShardingStatisticsTableCollector 
implements ShardingSphereSta
     private static final String SHARDING_TABLE_STATISTICS = 
"sharding_table_statistics";
     
     @Override
-    public Optional<ShardingSphereTableData> collect(final String 
databaseName, final ShardingSphereTable table, final Map<String, 
ShardingSphereDatabase> databases,
-                                                     final RuleMetaData 
globalRuleMetaData) throws SQLException {
+    public Optional<ShardingSphereTableData> collect(final String 
databaseName, final ShardingSphereTable table,
+                                                     final Map<String, 
ShardingSphereDatabase> databases, final RuleMetaData globalRuleMetaData) 
throws SQLException {
         ShardingSphereTableData result = new 
ShardingSphereTableData(SHARDING_TABLE_STATISTICS);
         DatabaseType protocolType = 
databases.values().iterator().next().getProtocolType();
         DialectDatabaseMetaData dialectDatabaseMetaData = new 
DatabaseTypeRegistry(protocolType).getDialectDatabaseMetaData();
@@ -65,16 +65,16 @@ public final class ShardingStatisticsTableCollector 
implements ShardingSphereSta
     }
     
     private void collectFromDatabase(final ShardingSphereDatabase database, 
final ShardingSphereTableData tableData) throws SQLException {
-        Optional<ShardingRule> shardingRule = 
database.getRuleMetaData().findSingleRule(ShardingRule.class);
-        if (!shardingRule.isPresent()) {
+        Optional<ShardingRule> rule = 
database.getRuleMetaData().findSingleRule(ShardingRule.class);
+        if (!rule.isPresent()) {
             return;
         }
-        collectForShardingStatisticTable(database, shardingRule.get(), 
tableData);
+        collectForShardingStatisticTable(database, rule.get(), tableData);
     }
     
-    private void collectForShardingStatisticTable(final ShardingSphereDatabase 
database, final ShardingRule shardingRule, final ShardingSphereTableData 
tableData) throws SQLException {
+    private void collectForShardingStatisticTable(final ShardingSphereDatabase 
database, final ShardingRule rule, final ShardingSphereTableData tableData) 
throws SQLException {
         int count = 1;
-        for (ShardingTable each : shardingRule.getShardingTables().values()) {
+        for (ShardingTable each : rule.getShardingTables().values()) {
             for (DataNode dataNode : each.getActualDataNodes()) {
                 List<Object> row = new LinkedList<>();
                 row.add(count++);
@@ -89,11 +89,12 @@ public final class ShardingStatisticsTableCollector 
implements ShardingSphereSta
     }
     
     private void addTableRowsAndDataLength(final Map<String, StorageUnit> 
storageUnits, final DataNode dataNode, final List<Object> row) throws 
SQLException {
-        DatabaseType databaseType = 
storageUnits.get(dataNode.getDataSourceName()).getStorageType();
+        StorageUnit storageUnit = 
storageUnits.get(dataNode.getDataSourceName());
+        DatabaseType databaseType = storageUnit.getStorageType();
         Optional<DialectShardingStatisticsTableCollector> dialectCollector = 
DatabaseTypedSPILoader.findService(DialectShardingStatisticsTableCollector.class,
 databaseType);
         boolean isAppended = false;
         if (dialectCollector.isPresent()) {
-            try (Connection connection = 
storageUnits.get(dataNode.getDataSourceName()).getDataSource().getConnection()) 
{
+            try (Connection connection = 
storageUnit.getDataSource().getConnection()) {
                 isAppended = dialectCollector.get().appendRow(connection, 
dataNode, row);
             }
         }
diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollector.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollector.java
index efdc0afb093..890b5cd56dd 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollector.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollector.java
@@ -31,11 +31,11 @@ import java.util.List;
  */
 public final class MySQLShardingStatisticsTableCollector implements 
DialectShardingStatisticsTableCollector {
     
-    private static final String MYSQL_TABLE_ROWS_AND_DATA_LENGTH = "SELECT 
TABLE_ROWS, DATA_LENGTH FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? 
AND TABLE_NAME = ?";
+    private static final String FETCH_TABLE_ROWS_AND_DATA_LENGTH_SQL = "SELECT 
TABLE_ROWS, DATA_LENGTH FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? 
AND TABLE_NAME = ?";
     
     @Override
     public boolean appendRow(final Connection connection, final DataNode 
dataNode, final List<Object> row) throws SQLException {
-        try (PreparedStatement preparedStatement = 
connection.prepareStatement(MYSQL_TABLE_ROWS_AND_DATA_LENGTH)) {
+        try (PreparedStatement preparedStatement = 
connection.prepareStatement(FETCH_TABLE_ROWS_AND_DATA_LENGTH_SQL)) {
             preparedStatement.setString(1, connection.getCatalog());
             preparedStatement.setString(2, dataNode.getTableName());
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollector.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollector.java
index 2c783dddd86..0fb7127bd56 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollector.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollector.java
@@ -31,14 +31,14 @@ import java.util.List;
  */
 public final class OpenGaussShardingStatisticsTableCollector implements 
DialectShardingStatisticsTableCollector {
     
-    private static final String OPENGAUSS_TABLE_ROWS_AND_DATA_LENGTH = "SELECT 
RELTUPLES AS TABLE_ROWS, PG_TABLE_SIZE(?) AS DATA_LENGTH FROM PG_CLASS WHERE 
RELNAME = ?";
+    private static final String FETCH_TABLE_ROWS_AND_DATA_LENGTH_SQL = "SELECT 
RELTUPLES AS TABLE_ROWS, PG_TABLE_SIZE(?) AS DATA_LENGTH FROM PG_CLASS WHERE 
RELNAME = ?";
     
     @Override
     public boolean appendRow(final Connection connection, final DataNode 
dataNode, final List<Object> row) throws SQLException {
         if (!isTableExist(connection, dataNode.getTableName())) {
             return false;
         }
-        try (PreparedStatement preparedStatement = 
connection.prepareStatement(OPENGAUSS_TABLE_ROWS_AND_DATA_LENGTH)) {
+        try (PreparedStatement preparedStatement = 
connection.prepareStatement(FETCH_TABLE_ROWS_AND_DATA_LENGTH_SQL)) {
             preparedStatement.setString(1, dataNode.getTableName());
             preparedStatement.setString(2, dataNode.getTableName());
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollector.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollector.java
index 80c1daa8bc6..0e8c6fec01d 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollector.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollector.java
@@ -33,14 +33,14 @@ import java.util.Optional;
  */
 public final class PostgreSQLShardingStatisticsTableCollector implements 
DialectShardingStatisticsTableCollector {
     
-    private static final String POSTGRESQL_TABLE_ROWS_LENGTH = "SELECT 
RELTUPLES FROM PG_CLASS WHERE RELNAMESPACE = (SELECT OID FROM PG_NAMESPACE 
WHERE NSPNAME= ?) AND RELNAME = ?";
+    private static final String FETCH_TABLE_ROWS_LENGTH_SQL = "SELECT 
RELTUPLES FROM PG_CLASS WHERE RELNAMESPACE = (SELECT OID FROM PG_NAMESPACE 
WHERE NSPNAME= ?) AND RELNAME = ?";
     
-    private static final String POSTGRESQL_TABLE_DATA_LENGTH = "SELECT 
PG_RELATION_SIZE(RELID) as DATA_LENGTH FROM PG_STAT_ALL_TABLES T WHERE 
SCHEMANAME= ? AND RELNAME = ?";
+    private static final String FETCH_TABLE_DATA_LENGTH_SQL = "SELECT 
PG_RELATION_SIZE(RELID) as DATA_LENGTH FROM PG_STAT_ALL_TABLES T WHERE 
SCHEMANAME= ? AND RELNAME = ?";
     
     @Override
     public boolean appendRow(final Connection connection, final DataNode 
dataNode, final List<Object> row) throws SQLException {
-        row.add(getRowValue(connection, dataNode, 
POSTGRESQL_TABLE_ROWS_LENGTH, TABLE_ROWS_COLUMN_NAME).orElse(BigDecimal.ZERO));
-        row.add(getRowValue(connection, dataNode, 
POSTGRESQL_TABLE_DATA_LENGTH, DATA_LENGTH_COLUMN_NAME).orElse(BigDecimal.ZERO));
+        row.add(getRowValue(connection, dataNode, FETCH_TABLE_ROWS_LENGTH_SQL, 
TABLE_ROWS_COLUMN_NAME).orElse(BigDecimal.ZERO));
+        row.add(getRowValue(connection, dataNode, FETCH_TABLE_DATA_LENGTH_SQL, 
DATA_LENGTH_COLUMN_NAME).orElse(BigDecimal.ZERO));
         return true;
     }
     
diff --git 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollectorTest.java
 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollectorTest.java
new file mode 100644
index 00000000000..9f29fca9352
--- /dev/null
+++ 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollectorTest.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.sharding.metadata.data;
+
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
+import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
+import 
org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereStatisticsCollector;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.ShardingTable;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ShardingStatisticsTableCollectorTest {
+    
+    private final DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+    
+    private ShardingSphereStatisticsCollector statisticsCollector;
+    
+    @BeforeEach
+    void setUp() {
+        statisticsCollector = 
TypedSPILoader.getService(ShardingSphereStatisticsCollector.class, 
"sharding_table_statistics");
+    }
+    
+    @Test
+    void assertCollectWithoutShardingRule() throws SQLException {
+        ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, 
RETURNS_DEEP_STUBS);
+        when(database.getProtocolType()).thenReturn(databaseType);
+        Optional<ShardingSphereTableData> actual = 
statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), 
Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
+        assertFalse(actual.isPresent());
+    }
+    
+    @Test
+    void assertCollectWithShardingRule() throws SQLException {
+        ShardingRule rule = mock(ShardingRule.class);
+        
when(rule.getShardingTables()).thenReturn(Collections.singletonMap("foo_tbl", 
new ShardingTable(Arrays.asList("ds_0", "ds_1"), "foo_tbl")));
+        Map<String, StorageUnit> storageUnits = new HashMap<>(2, 1F);
+        storageUnits.put("ds_0", mock(StorageUnit.class, RETURNS_DEEP_STUBS));
+        storageUnits.put("ds_1", mock(StorageUnit.class, RETURNS_DEEP_STUBS));
+        ShardingSphereDatabase database = new ShardingSphereDatabase(
+                "foo_db", databaseType, new 
ResourceMetaData(Collections.emptyMap(), storageUnits), new 
RuleMetaData(Collections.singleton(rule)), Collections.emptyMap());
+        Optional<ShardingSphereTableData> actual = 
statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), 
Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
+        assertTrue(actual.isPresent());
+        assertThat(actual.get().getName(), is("sharding_table_statistics"));
+        List<ShardingSphereRowData> actualRows = new 
ArrayList<>(actual.get().getRows());
+        assertThat(actualRows.size(), is(2));
+        assertThat(actualRows.get(0).getRows(), is(Arrays.asList(1, "foo_db", 
"foo_tbl", "ds_0", "foo_tbl", new BigDecimal("0"), new BigDecimal("0"))));
+        assertThat(actualRows.get(1).getRows(), is(Arrays.asList(2, "foo_db", 
"foo_tbl", "ds_1", "foo_tbl", new BigDecimal("0"), new BigDecimal("0"))));
+    }
+}
diff --git 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollectorTest.java
 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollectorTest.java
new file mode 100644
index 00000000000..7abc71cd22b
--- /dev/null
+++ 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollectorTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.sharding.metadata.data.dialect.type;
+
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
+import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
+import 
org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereStatisticsCollector;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.ShardingTable;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class MySQLShardingStatisticsTableCollectorTest {
+    
+    private final DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "MySQL");
+    
+    private ShardingSphereStatisticsCollector statisticsCollector;
+    
+    @BeforeEach
+    void setUp() {
+        statisticsCollector = 
TypedSPILoader.getService(ShardingSphereStatisticsCollector.class, 
"sharding_table_statistics");
+    }
+    
+    @Test
+    void assertCollect() throws SQLException {
+        ShardingRule rule = mock(ShardingRule.class);
+        
when(rule.getShardingTables()).thenReturn(Collections.singletonMap("foo_tbl", 
new ShardingTable(Arrays.asList("ds_0", "ds_1"), "foo_tbl")));
+        Map<String, StorageUnit> storageUnits = new HashMap<>(2, 1F);
+        storageUnits.put("ds_0", mockStorageUnit(mock(ResultSet.class)));
+        storageUnits.put("ds_1", mockStorageUnit(mockResultSet()));
+        ShardingSphereDatabase database = new ShardingSphereDatabase(
+                "foo_db", databaseType, new 
ResourceMetaData(Collections.emptyMap(), storageUnits), new 
RuleMetaData(Collections.singleton(rule)), Collections.emptyMap());
+        Optional<ShardingSphereTableData> actual = 
statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), 
Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
+        assertTrue(actual.isPresent());
+        assertThat(actual.get().getName(), is("sharding_table_statistics"));
+        List<ShardingSphereRowData> actualRows = new 
ArrayList<>(actual.get().getRows());
+        assertThat(actualRows.size(), is(2));
+        assertThat(actualRows.get(0).getRows(), is(Arrays.asList(2, "foo_db", 
"foo_tbl", "ds_1", "foo_tbl", new BigDecimal("10"), new BigDecimal("100"))));
+        assertThat(actualRows.get(1).getRows(), is(Arrays.asList(1, "foo_db", 
"foo_tbl", "ds_0", "foo_tbl", new BigDecimal("0"), new BigDecimal("0"))));
+    }
+    
+    private StorageUnit mockStorageUnit(final ResultSet resultSet) throws 
SQLException {
+        StorageUnit result = mock(StorageUnit.class);
+        when(result.getStorageType()).thenReturn(databaseType);
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(connection.prepareStatement("SELECT TABLE_ROWS, DATA_LENGTH FROM 
information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 
?").executeQuery()).thenReturn(resultSet);
+        when(result.getDataSource()).thenReturn(new 
MockedDataSource(connection));
+        return result;
+    }
+    
+    private ResultSet mockResultSet() throws SQLException {
+        ResultSet result = mock(ResultSet.class);
+        when(result.next()).thenReturn(true);
+        when(result.getBigDecimal("TABLE_ROWS")).thenReturn(new 
BigDecimal("10"));
+        when(result.getBigDecimal("DATA_LENGTH")).thenReturn(new 
BigDecimal("100"));
+        return result;
+    }
+}
diff --git 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollectorTest.java
 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollectorTest.java
new file mode 100644
index 00000000000..8affb1e16a4
--- /dev/null
+++ 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollectorTest.java
@@ -0,0 +1,122 @@
+/*
+ * 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.sharding.metadata.data.dialect.type;
+
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
+import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
+import 
org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereStatisticsCollector;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.ShardingTable;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class OpenGaussShardingStatisticsTableCollectorTest {
+    
+    private final DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "openGauss");
+    
+    private ShardingSphereStatisticsCollector statisticsCollector;
+    
+    @BeforeEach
+    void setUp() {
+        statisticsCollector = 
TypedSPILoader.getService(ShardingSphereStatisticsCollector.class, 
"sharding_table_statistics");
+    }
+    
+    @Test
+    void assertCollectWithoutExistedTables() throws SQLException {
+        ShardingRule rule = mock(ShardingRule.class);
+        
when(rule.getShardingTables()).thenReturn(Collections.singletonMap("foo_tbl", 
new ShardingTable(Arrays.asList("ds_0", "ds_1"), "foo_tbl")));
+        Map<String, StorageUnit> storageUnits = new HashMap<>(2, 1F);
+        storageUnits.put("ds_0", mockStorageUnit(mock(ResultSet.class), 
false));
+        storageUnits.put("ds_1", mockStorageUnit(mockResultSet(), false));
+        ShardingSphereDatabase database = new ShardingSphereDatabase(
+                "foo_db", databaseType, new 
ResourceMetaData(Collections.emptyMap(), storageUnits), new 
RuleMetaData(Collections.singleton(rule)), Collections.emptyMap());
+        Optional<ShardingSphereTableData> actual = 
statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), 
Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
+        assertTrue(actual.isPresent());
+        assertThat(actual.get().getName(), is("sharding_table_statistics"));
+        List<ShardingSphereRowData> actualRows = new 
ArrayList<>(actual.get().getRows());
+        assertThat(actualRows.size(), is(2));
+        assertThat(actualRows.get(0).getRows(), is(Arrays.asList(1, "foo_db", 
"foo_tbl", "ds_0", "foo_tbl", new BigDecimal("0"), new BigDecimal("0"))));
+        assertThat(actualRows.get(1).getRows(), is(Arrays.asList(2, "foo_db", 
"foo_tbl", "ds_1", "foo_tbl", new BigDecimal("0"), new BigDecimal("0"))));
+    }
+    
+    @Test
+    void assertCollectWithExistedTables() throws SQLException {
+        ShardingRule rule = mock(ShardingRule.class);
+        
when(rule.getShardingTables()).thenReturn(Collections.singletonMap("foo_tbl", 
new ShardingTable(Arrays.asList("ds_0", "ds_1"), "foo_tbl")));
+        Map<String, StorageUnit> storageUnits = new HashMap<>(2, 1F);
+        storageUnits.put("ds_0", mockStorageUnit(mock(ResultSet.class), true));
+        storageUnits.put("ds_1", mockStorageUnit(mockResultSet(), true));
+        ShardingSphereDatabase database = new ShardingSphereDatabase(
+                "foo_db", databaseType, new 
ResourceMetaData(Collections.emptyMap(), storageUnits), new 
RuleMetaData(Collections.singleton(rule)), Collections.emptyMap());
+        Optional<ShardingSphereTableData> actual = 
statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), 
Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
+        assertTrue(actual.isPresent());
+        assertThat(actual.get().getName(), is("sharding_table_statistics"));
+        List<ShardingSphereRowData> actualRows = new 
ArrayList<>(actual.get().getRows());
+        assertThat(actualRows.size(), is(2));
+        assertThat(actualRows.get(0).getRows(), is(Arrays.asList(2, "foo_db", 
"foo_tbl", "ds_1", "foo_tbl", new BigDecimal("10"), new BigDecimal("100"))));
+        assertThat(actualRows.get(1).getRows(), is(Arrays.asList(1, "foo_db", 
"foo_tbl", "ds_0", "foo_tbl", new BigDecimal("0"), new BigDecimal("0"))));
+    }
+    
+    private StorageUnit mockStorageUnit(final ResultSet resultSet, final 
boolean tableExisted) throws SQLException {
+        StorageUnit result = mock(StorageUnit.class);
+        when(result.getStorageType()).thenReturn(databaseType);
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        ResultSet tableExistedResultSet = mock(ResultSet.class);
+        when(tableExistedResultSet.next()).thenReturn(tableExisted);
+        when(connection.getMetaData().getTables(any(), any(), any(), 
any())).thenReturn(tableExistedResultSet);
+        when(connection.prepareStatement("SELECT RELTUPLES AS TABLE_ROWS, 
PG_TABLE_SIZE(?) AS DATA_LENGTH FROM PG_CLASS WHERE RELNAME = 
?").executeQuery()).thenReturn(resultSet);
+        when(result.getDataSource()).thenReturn(new 
MockedDataSource(connection));
+        return result;
+    }
+    
+    private ResultSet mockResultSet() throws SQLException {
+        ResultSet result = mock(ResultSet.class);
+        when(result.next()).thenReturn(true);
+        when(result.getBigDecimal("TABLE_ROWS")).thenReturn(new 
BigDecimal("10"));
+        when(result.getBigDecimal("DATA_LENGTH")).thenReturn(new 
BigDecimal("100"));
+        return result;
+    }
+}
diff --git 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollectorTest.java
 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollectorTest.java
new file mode 100644
index 00000000000..2235738011b
--- /dev/null
+++ 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollectorTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.sharding.metadata.data.dialect.type;
+
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
+import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
+import 
org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereStatisticsCollector;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.ShardingTable;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class PostgreSQLShardingStatisticsTableCollectorTest {
+    
+    private final DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
+    
+    private ShardingSphereStatisticsCollector statisticsCollector;
+    
+    @BeforeEach
+    void setUp() {
+        statisticsCollector = 
TypedSPILoader.getService(ShardingSphereStatisticsCollector.class, 
"sharding_table_statistics");
+    }
+    
+    @Test
+    void assertCollect() throws SQLException {
+        ShardingRule rule = mock(ShardingRule.class);
+        
when(rule.getShardingTables()).thenReturn(Collections.singletonMap("foo_tbl", 
new ShardingTable(Arrays.asList("ds_0", "ds_1"), "foo_tbl")));
+        Map<String, StorageUnit> storageUnits = new HashMap<>(2, 1F);
+        storageUnits.put("ds_0", mockStorageUnit(mock(ResultSet.class)));
+        storageUnits.put("ds_1", mockStorageUnit(mockResultSet()));
+        ShardingSphereDatabase database = new ShardingSphereDatabase(
+                "foo_db", databaseType, new 
ResourceMetaData(Collections.emptyMap(), storageUnits), new 
RuleMetaData(Collections.singleton(rule)), Collections.emptyMap());
+        Optional<ShardingSphereTableData> actual = 
statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), 
Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
+        assertTrue(actual.isPresent());
+        assertThat(actual.get().getName(), is("sharding_table_statistics"));
+        List<ShardingSphereRowData> actualRows = new 
ArrayList<>(actual.get().getRows());
+        assertThat(actualRows.size(), is(2));
+        assertThat(actualRows.get(0).getRows(), is(Arrays.asList(2, "foo_db", 
"foo_tbl", "ds_1", "foo_tbl", new BigDecimal("10"), new BigDecimal("100"))));
+        assertThat(actualRows.get(1).getRows(), is(Arrays.asList(1, "foo_db", 
"foo_tbl", "ds_0", "foo_tbl", new BigDecimal("0"), new BigDecimal("0"))));
+    }
+    
+    private StorageUnit mockStorageUnit(final ResultSet resultSet) throws 
SQLException {
+        StorageUnit result = mock(StorageUnit.class);
+        when(result.getStorageType()).thenReturn(databaseType);
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(connection.prepareStatement("SELECT RELTUPLES FROM PG_CLASS WHERE 
RELNAMESPACE = (SELECT OID FROM PG_NAMESPACE WHERE NSPNAME= ?) AND RELNAME = 
?").executeQuery()).thenReturn(resultSet);
+        when(connection.prepareStatement("SELECT PG_RELATION_SIZE(RELID) as 
DATA_LENGTH FROM PG_STAT_ALL_TABLES T WHERE SCHEMANAME= ? AND RELNAME = 
?").executeQuery()).thenReturn(resultSet);
+        when(result.getDataSource()).thenReturn(new 
MockedDataSource(connection));
+        return result;
+    }
+    
+    private ResultSet mockResultSet() throws SQLException {
+        ResultSet result = mock(ResultSet.class);
+        when(result.next()).thenReturn(true);
+        when(result.getBigDecimal("TABLE_ROWS")).thenReturn(new 
BigDecimal("10"));
+        when(result.getBigDecimal("DATA_LENGTH")).thenReturn(new 
BigDecimal("100"));
+        return result;
+    }
+}


Reply via email to