This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 3212112bf7b Add PostgreSQLSelectAdminExecutorFactory (#36884)
3212112bf7b is described below
commit 3212112bf7bf97bd5bc214d913921e931ac64a28
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Oct 16 00:35:11 2025 +0800
Add PostgreSQLSelectAdminExecutorFactory (#36884)
* Add PostgreSQLSelectAdminExecutorFactory
* Add PostgreSQLSelectAdminExecutorFactory
---
.../admin/PostgreSQLAdminExecutorCreator.java | 98 ++--------------------
.../PostgreSQLSelectAdminExecutorFactory.java} | 96 ++++++++++-----------
2 files changed, 49 insertions(+), 145 deletions(-)
diff --git
a/proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/PostgreSQLAdminExecutorCreator.java
b/proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/PostgreSQLAdminExecutorCreator.java
index ecf7b7af248..0d57d7f111a 100644
---
a/proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/PostgreSQLAdminExecutorCreator.java
+++
b/proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/PostgreSQLAdminExecutorCreator.java
@@ -17,37 +17,20 @@
package org.apache.shardingsphere.proxy.backend.postgresql.handler.admin;
-import com.cedarsoftware.util.CaseInsensitiveMap;
-import com.cedarsoftware.util.CaseInsensitiveSet;
-import
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
-import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import
org.apache.shardingsphere.infra.metadata.database.schema.manager.SystemSchemaManager;
-import
org.apache.shardingsphere.infra.metadata.statistics.collector.DialectDatabaseStatisticsCollector;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminExecutor;
import
org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminExecutorCreator;
-import
org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseMetaDataExecutor;
import
org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.executor.PostgreSQLResetVariableAdminExecutor;
import
org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.executor.PostgreSQLSetVariableAdminExecutor;
import
org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.executor.PostgreSQLShowVariableExecutor;
-import
org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SubqueryTableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
+import
org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.factory.PostgreSQLSelectAdminExecutorFactory;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.SetStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ShowStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
import
org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLResetParameterStatement;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
import java.util.Optional;
/**
@@ -55,92 +38,25 @@ import java.util.Optional;
*/
public final class PostgreSQLAdminExecutorCreator implements
DatabaseAdminExecutorCreator {
- private static final Map<String, Collection<String>> SCHEMA_TABLES = new
CaseInsensitiveMap<>();
-
- static {
- SCHEMA_TABLES.put("shardingsphere", new
CaseInsensitiveSet<>(Collections.singletonList("cluster_information")));
- }
-
@Override
public Optional<DatabaseAdminExecutor> create(final SQLStatementContext
sqlStatementContext, final String sql, final String databaseName, final
List<Object> parameters) {
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
if (sqlStatement instanceof SelectStatement) {
- Map<String, Collection<String>> selectedSchemaTables =
getSelectedSchemaTables((SelectStatement) sqlStatement);
- if (isSelectedStatisticsSystemTable(selectedSchemaTables) ||
isSelectedShardingSphereSystemTable(selectedSchemaTables)) {
- return Optional.empty();
- }
- if (isSelectSystemTable(selectedSchemaTables)) {
- return Optional.of(new DatabaseMetaDataExecutor(sql,
parameters));
+ Optional<DatabaseAdminExecutor> selectAdminExecutor =
PostgreSQLSelectAdminExecutorFactory.newInstance((SelectStatement)
sqlStatement, sql, parameters);
+ if (selectAdminExecutor.isPresent()) {
+ return selectAdminExecutor;
}
}
if (sqlStatement instanceof SetStatement) {
return Optional.of(new
PostgreSQLSetVariableAdminExecutor((SetStatement) sqlStatement));
}
- if (sqlStatement instanceof PostgreSQLResetParameterStatement) {
- return Optional.of(new
PostgreSQLResetVariableAdminExecutor((PostgreSQLResetParameterStatement)
sqlStatement));
- }
if (sqlStatement instanceof ShowStatement) {
return Optional.of(new
PostgreSQLShowVariableExecutor((ShowStatement) sqlStatement));
}
- return Optional.empty();
- }
-
- private Map<String, Collection<String>> getSelectedSchemaTables(final
SelectStatement sqlStatement) {
- TableExtractor extractor = new TableExtractor();
- extractor.extractTablesFromSelect(sqlStatement);
- List<TableSegment> extracted = new
LinkedList<>(extractor.getTableContext());
- for (TableSegment each : extractor.getTableContext()) {
- if (each instanceof SubqueryTableSegment) {
- TableExtractor subExtractor = new TableExtractor();
- subExtractor.extractTablesFromSelect(((SubqueryTableSegment)
each).getSubquery().getSelect());
- extracted.addAll(subExtractor.getTableContext());
- }
- }
- Map<String, Collection<String>> result = new CaseInsensitiveMap<>();
- for (TableSegment each : extracted) {
- if (each instanceof SimpleTableSegment) {
- Optional<OwnerSegment> ownerSegment = ((SimpleTableSegment)
each).getOwner();
- if (ownerSegment.isPresent()) {
- Collection<String> tables =
result.getOrDefault(ownerSegment.get().getIdentifier().getValue(), new
CaseInsensitiveSet<>());
- tables.add(((SimpleTableSegment)
each).getTableName().getIdentifier().getValue());
- result.put(ownerSegment.get().getIdentifier().getValue(),
tables);
- }
- }
- }
- return result;
- }
-
- private boolean isSelectedStatisticsSystemTable(final Map<String,
Collection<String>> selectedSchemaTables) {
- DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
- Optional<DialectDatabaseStatisticsCollector>
dialectStatisticsCollector =
DatabaseTypedSPILoader.findService(DialectDatabaseStatisticsCollector.class,
databaseType);
- return
dialectStatisticsCollector.map(dialectDatabaseStatisticsCollector ->
dialectDatabaseStatisticsCollector.isStatisticsTables(selectedSchemaTables)).orElse(false);
- }
-
- private boolean isSelectedShardingSphereSystemTable(final Map<String,
Collection<String>> selectedSchemaTables) {
- if (selectedSchemaTables.isEmpty()) {
- return false;
- }
- for (Entry<String, Collection<String>> each :
selectedSchemaTables.entrySet()) {
- if (!SCHEMA_TABLES.containsKey(each.getKey())) {
- return false;
- }
- if
(!SCHEMA_TABLES.get(each.getKey()).containsAll(each.getValue())) {
- return false;
- }
- }
- return true;
- }
-
- private boolean isSelectSystemTable(final Map<String, Collection<String>>
selectedSchemaTables) {
- if (selectedSchemaTables.isEmpty()) {
- return false;
- }
- for (Entry<String, Collection<String>> each :
selectedSchemaTables.entrySet()) {
- if (!SystemSchemaManager.isSystemTable("postgresql",
each.getKey(), each.getValue())) {
- return false;
- }
+ if (sqlStatement instanceof PostgreSQLResetParameterStatement) {
+ return Optional.of(new
PostgreSQLResetVariableAdminExecutor((PostgreSQLResetParameterStatement)
sqlStatement));
}
- return true;
+ return Optional.empty();
}
@Override
diff --git
a/proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/PostgreSQLAdminExecutorCreator.java
b/proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/factory/PostgreSQLSelectAdminExecutorFactory.java
similarity index 52%
copy from
proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/PostgreSQLAdminExecutorCreator.java
copy to
proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/factory/PostgreSQLSelectAdminExecutorFactory.java
index ecf7b7af248..b9343d685ec 100644
---
a/proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/PostgreSQLAdminExecutorCreator.java
+++
b/proxy/backend/dialect/postgresql/src/main/java/org/apache/shardingsphere/proxy/backend/postgresql/handler/admin/factory/PostgreSQLSelectAdminExecutorFactory.java
@@ -15,32 +15,25 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.postgresql.handler.admin;
+package
org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.factory;
import com.cedarsoftware.util.CaseInsensitiveMap;
import com.cedarsoftware.util.CaseInsensitiveSet;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
-import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.metadata.database.schema.manager.SystemSchemaManager;
import
org.apache.shardingsphere.infra.metadata.statistics.collector.DialectDatabaseStatisticsCollector;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminExecutor;
-import
org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminExecutorCreator;
import
org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseMetaDataExecutor;
-import
org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.executor.PostgreSQLResetVariableAdminExecutor;
-import
org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.executor.PostgreSQLSetVariableAdminExecutor;
-import
org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.executor.PostgreSQLShowVariableExecutor;
import
org.apache.shardingsphere.sql.parser.statement.core.extractor.TableExtractor;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SubqueryTableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.SetStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ShowStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
-import
org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLResetParameterStatement;
import java.util.Collection;
import java.util.Collections;
@@ -51,9 +44,12 @@ import java.util.Map.Entry;
import java.util.Optional;
/**
- * Database admin executor creator for PostgreSQL.
+ * Select admin executor factory for PostgreSQL.
*/
-public final class PostgreSQLAdminExecutorCreator implements
DatabaseAdminExecutorCreator {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class PostgreSQLSelectAdminExecutorFactory {
+
+ private static final DatabaseType DATABASE_TYPE =
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
private static final Map<String, Collection<String>> SCHEMA_TABLES = new
CaseInsensitiveMap<>();
@@ -61,43 +57,28 @@ public final class PostgreSQLAdminExecutorCreator
implements DatabaseAdminExecut
SCHEMA_TABLES.put("shardingsphere", new
CaseInsensitiveSet<>(Collections.singletonList("cluster_information")));
}
- @Override
- public Optional<DatabaseAdminExecutor> create(final SQLStatementContext
sqlStatementContext, final String sql, final String databaseName, final
List<Object> parameters) {
- SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
- if (sqlStatement instanceof SelectStatement) {
- Map<String, Collection<String>> selectedSchemaTables =
getSelectedSchemaTables((SelectStatement) sqlStatement);
- if (isSelectedStatisticsSystemTable(selectedSchemaTables) ||
isSelectedShardingSphereSystemTable(selectedSchemaTables)) {
- return Optional.empty();
- }
- if (isSelectSystemTable(selectedSchemaTables)) {
- return Optional.of(new DatabaseMetaDataExecutor(sql,
parameters));
- }
- }
- if (sqlStatement instanceof SetStatement) {
- return Optional.of(new
PostgreSQLSetVariableAdminExecutor((SetStatement) sqlStatement));
+ /**
+ * Create new instance of database admin executor.
+ *
+ * @param sqlStatement select statement
+ * @param sql SQL
+ * @param parameters SQL parameters
+ * @return created instance
+ */
+ public static Optional<DatabaseAdminExecutor> newInstance(final
SelectStatement sqlStatement, final String sql, final List<Object> parameters) {
+ Map<String, Collection<String>> selectedSchemaTables =
getSelectedSchemaTables(sqlStatement);
+ if (isSelectedStatisticsSystemTable(selectedSchemaTables) ||
isSelectedShardingSphereSystemTable(selectedSchemaTables)) {
+ return Optional.empty();
}
- if (sqlStatement instanceof PostgreSQLResetParameterStatement) {
- return Optional.of(new
PostgreSQLResetVariableAdminExecutor((PostgreSQLResetParameterStatement)
sqlStatement));
- }
- if (sqlStatement instanceof ShowStatement) {
- return Optional.of(new
PostgreSQLShowVariableExecutor((ShowStatement) sqlStatement));
+ if (isSelectSystemTable(selectedSchemaTables)) {
+ return Optional.of(new DatabaseMetaDataExecutor(sql, parameters));
}
return Optional.empty();
}
- private Map<String, Collection<String>> getSelectedSchemaTables(final
SelectStatement sqlStatement) {
- TableExtractor extractor = new TableExtractor();
- extractor.extractTablesFromSelect(sqlStatement);
- List<TableSegment> extracted = new
LinkedList<>(extractor.getTableContext());
- for (TableSegment each : extractor.getTableContext()) {
- if (each instanceof SubqueryTableSegment) {
- TableExtractor subExtractor = new TableExtractor();
- subExtractor.extractTablesFromSelect(((SubqueryTableSegment)
each).getSubquery().getSelect());
- extracted.addAll(subExtractor.getTableContext());
- }
- }
+ private static Map<String, Collection<String>>
getSelectedSchemaTables(final SelectStatement sqlStatement) {
Map<String, Collection<String>> result = new CaseInsensitiveMap<>();
- for (TableSegment each : extracted) {
+ for (TableSegment each : extractTables(sqlStatement)) {
if (each instanceof SimpleTableSegment) {
Optional<OwnerSegment> ownerSegment = ((SimpleTableSegment)
each).getOwner();
if (ownerSegment.isPresent()) {
@@ -110,13 +91,25 @@ public final class PostgreSQLAdminExecutorCreator
implements DatabaseAdminExecut
return result;
}
- private boolean isSelectedStatisticsSystemTable(final Map<String,
Collection<String>> selectedSchemaTables) {
- DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
- Optional<DialectDatabaseStatisticsCollector>
dialectStatisticsCollector =
DatabaseTypedSPILoader.findService(DialectDatabaseStatisticsCollector.class,
databaseType);
- return
dialectStatisticsCollector.map(dialectDatabaseStatisticsCollector ->
dialectDatabaseStatisticsCollector.isStatisticsTables(selectedSchemaTables)).orElse(false);
+ private static Collection<TableSegment> extractTables(final
SelectStatement sqlStatement) {
+ TableExtractor tableExtractor = new TableExtractor();
+ tableExtractor.extractTablesFromSelect(sqlStatement);
+ Collection<TableSegment> result = new
LinkedList<>(tableExtractor.getTableContext());
+ for (TableSegment each : tableExtractor.getTableContext()) {
+ if (each instanceof SubqueryTableSegment) {
+ TableExtractor subTableExtractor = new TableExtractor();
+
subTableExtractor.extractTablesFromSelect(((SubqueryTableSegment)
each).getSubquery().getSelect());
+ result.addAll(subTableExtractor.getTableContext());
+ }
+ }
+ return result;
}
- private boolean isSelectedShardingSphereSystemTable(final Map<String,
Collection<String>> selectedSchemaTables) {
+ private static boolean isSelectedStatisticsSystemTable(final Map<String,
Collection<String>> selectedSchemaTables) {
+ return
DatabaseTypedSPILoader.findService(DialectDatabaseStatisticsCollector.class,
DATABASE_TYPE).map(optional ->
optional.isStatisticsTables(selectedSchemaTables)).orElse(false);
+ }
+
+ private static boolean isSelectedShardingSphereSystemTable(final
Map<String, Collection<String>> selectedSchemaTables) {
if (selectedSchemaTables.isEmpty()) {
return false;
}
@@ -131,7 +124,7 @@ public final class PostgreSQLAdminExecutorCreator
implements DatabaseAdminExecut
return true;
}
- private boolean isSelectSystemTable(final Map<String, Collection<String>>
selectedSchemaTables) {
+ private static boolean isSelectSystemTable(final Map<String,
Collection<String>> selectedSchemaTables) {
if (selectedSchemaTables.isEmpty()) {
return false;
}
@@ -142,9 +135,4 @@ public final class PostgreSQLAdminExecutorCreator
implements DatabaseAdminExecut
}
return true;
}
-
- @Override
- public String getDatabaseType() {
- return "PostgreSQL";
- }
}