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 5e0ef10e1a3 Add OpenGauss and PostgreSQL schema option implementations (#35278) 5e0ef10e1a3 is described below commit 5e0ef10e1a3d830cb7181f17bf311f72854cf68b Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Mon Apr 28 14:09:02 2025 +0800 Add OpenGauss and PostgreSQL schema option implementations (#35278) --- .../dml/from/type/SimpleTableSegmentBinder.java | 14 +++--- .../option/schema/DialectSchemaOption.java | 9 ++++ .../database/OpenGaussDatabaseMetaData.java | 4 +- .../database/option/OpenGaussSchemaOption.java | 52 ++++++++++++++++++++++ .../database/PostgreSQLDatabaseMetaData.java | 4 +- .../database/option/PostgreSQLSchemaOption.java | 52 ++++++++++++++++++++++ 6 files changed, 123 insertions(+), 12 deletions(-) diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java index 09dfd4f9b74..9a70db1e247 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java @@ -25,12 +25,10 @@ import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.Ta import org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.type.SimpleTableSegmentBinderContext; import org.apache.shardingsphere.infra.binder.engine.segment.util.SubqueryTableBindUtils; import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; -import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter; +import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; -import org.apache.shardingsphere.infra.database.opengauss.type.OpenGaussDatabaseType; -import org.apache.shardingsphere.infra.database.postgresql.type.PostgreSQLDatabaseType; 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; @@ -125,11 +123,11 @@ public final class SimpleTableSegmentBinder { } // TODO getSchemaName according to search path DatabaseType databaseType = binderContext.getSqlStatement().getDatabaseType(); - if ((databaseType instanceof PostgreSQLDatabaseType || databaseType instanceof OpenGaussDatabaseType) - && SystemSchemaManager.isSystemTable(databaseType.getType(), PG_CATALOG, segment.getTableName().getIdentifier().getValue())) { - return new IdentifierValue(PG_CATALOG); - } - return new IdentifierValue(new DatabaseTypeRegistry(databaseType).getDefaultSchemaName(binderContext.getCurrentDatabaseName())); + DatabaseTypeRegistry databaseTypeRegistry = new DatabaseTypeRegistry(databaseType); + Optional<String> defaultSystemSchema = databaseTypeRegistry.getDialectDatabaseMetaData().getSchemaOption().getDefaultSystemSchema(); + return defaultSystemSchema.isPresent() && SystemSchemaManager.isSystemTable(databaseType.getType(), defaultSystemSchema.get(), segment.getTableName().getIdentifier().getValue()) + ? new IdentifierValue(defaultSystemSchema.get()) + : new IdentifierValue(databaseTypeRegistry.getDefaultSchemaName(binderContext.getCurrentDatabaseName())); } private static void checkTableExists(final SQLStatementBinderContext binderContext, final ShardingSphereSchema schema, final String schemaName, final String tableName) { diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/database/metadata/option/schema/DialectSchemaOption.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/database/metadata/option/schema/DialectSchemaOption.java index c09c6a54cfb..153f83d9a5f 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/database/metadata/option/schema/DialectSchemaOption.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/database/metadata/option/schema/DialectSchemaOption.java @@ -46,4 +46,13 @@ public interface DialectSchemaOption { * @return default schema name */ Optional<String> getDefaultSchema(); + + /** + * Get default system schema name. + * + * @return default system schema name + */ + default Optional<String> getDefaultSystemSchema() { + return Optional.empty(); + } } diff --git a/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/OpenGaussDatabaseMetaData.java b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/OpenGaussDatabaseMetaData.java index aee01bd36b4..82ab7cec3c5 100644 --- a/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/OpenGaussDatabaseMetaData.java +++ b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/OpenGaussDatabaseMetaData.java @@ -22,12 +22,12 @@ import org.apache.shardingsphere.infra.database.core.metadata.database.enums.Quo import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.datatype.DialectDataTypeOption; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.index.DialectIndexOption; -import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.schema.DefaultSchemaOption; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.schema.DialectSchemaOption; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.table.DialectSystemTableOption; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.table.TableNamePatternType; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.transaction.DialectTransactionOption; import org.apache.shardingsphere.infra.database.opengauss.metadata.database.option.OpenGaussDataTypeOption; +import org.apache.shardingsphere.infra.database.opengauss.metadata.database.option.OpenGaussSchemaOption; import org.apache.shardingsphere.infra.database.opengauss.metadata.database.option.OpenGaussSystemTableOption; /** @@ -52,7 +52,7 @@ public final class OpenGaussDatabaseMetaData implements DialectDatabaseMetaData @Override public DialectSchemaOption getSchemaOption() { - return new DefaultSchemaOption(true, "public"); + return new OpenGaussSchemaOption(); } @Override diff --git a/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/option/OpenGaussSchemaOption.java b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/option/OpenGaussSchemaOption.java new file mode 100644 index 00000000000..4b743f1d5fb --- /dev/null +++ b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/option/OpenGaussSchemaOption.java @@ -0,0 +1,52 @@ +/* + * 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.database.opengauss.metadata.database.option; + +import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.schema.DefaultSchemaOption; +import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.schema.DialectSchemaOption; + +import java.sql.Connection; +import java.util.Optional; + +/** + * Schema option for openGauss. + */ +public final class OpenGaussSchemaOption implements DialectSchemaOption { + + private final DialectSchemaOption defaultSchemaOption = new DefaultSchemaOption(true, "public"); + + @Override + public boolean isSchemaAvailable() { + return defaultSchemaOption.isSchemaAvailable(); + } + + @Override + public String getSchema(final Connection connection) { + return defaultSchemaOption.getSchema(connection); + } + + @Override + public Optional<String> getDefaultSchema() { + return defaultSchemaOption.getDefaultSchema(); + } + + @Override + public Optional<String> getDefaultSystemSchema() { + return Optional.of("pg_catalog"); + } +} diff --git a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/PostgreSQLDatabaseMetaData.java b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/PostgreSQLDatabaseMetaData.java index a92bc720b23..17cb175a769 100644 --- a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/PostgreSQLDatabaseMetaData.java +++ b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/PostgreSQLDatabaseMetaData.java @@ -22,11 +22,11 @@ import org.apache.shardingsphere.infra.database.core.metadata.database.enums.Quo import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.datatype.DialectDataTypeOption; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.index.DialectIndexOption; -import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.schema.DefaultSchemaOption; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.schema.DialectSchemaOption; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.table.TableNamePatternType; import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.transaction.DialectTransactionOption; import org.apache.shardingsphere.infra.database.postgresql.metadata.database.option.PostgreSQLDataTypeOption; +import org.apache.shardingsphere.infra.database.postgresql.metadata.database.option.PostgreSQLSchemaOption; /** * Database meta data of PostgreSQL. @@ -50,7 +50,7 @@ public final class PostgreSQLDatabaseMetaData implements DialectDatabaseMetaData @Override public DialectSchemaOption getSchemaOption() { - return new DefaultSchemaOption(true, "public"); + return new PostgreSQLSchemaOption(); } @Override diff --git a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/option/PostgreSQLSchemaOption.java b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/option/PostgreSQLSchemaOption.java new file mode 100644 index 00000000000..6bbba13d7cc --- /dev/null +++ b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/option/PostgreSQLSchemaOption.java @@ -0,0 +1,52 @@ +/* + * 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.database.postgresql.metadata.database.option; + +import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.schema.DefaultSchemaOption; +import org.apache.shardingsphere.infra.database.core.metadata.database.metadata.option.schema.DialectSchemaOption; + +import java.sql.Connection; +import java.util.Optional; + +/** + * Schema option for PostgreSQL. + */ +public final class PostgreSQLSchemaOption implements DialectSchemaOption { + + private final DialectSchemaOption defaultSchemaOption = new DefaultSchemaOption(true, "public"); + + @Override + public boolean isSchemaAvailable() { + return defaultSchemaOption.isSchemaAvailable(); + } + + @Override + public String getSchema(final Connection connection) { + return defaultSchemaOption.getSchema(connection); + } + + @Override + public Optional<String> getDefaultSchema() { + return defaultSchemaOption.getDefaultSchema(); + } + + @Override + public Optional<String> getDefaultSystemSchema() { + return Optional.of("pg_catalog"); + } +}