You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/09/06 00:02:56 UTC
[shardingsphere] branch master updated: add valid for alter rename view (#20791)
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 fd06e555166 add valid for alter rename view (#20791)
fd06e555166 is described below
commit fd06e5551667fc54da2125e3139b9748ba95b3f4
Author: Chuxin Chen <ch...@qq.com>
AuthorDate: Tue Sep 6 08:02:47 2022 +0800
add valid for alter rename view (#20791)
* add valid for alter rename view
* add valid for alter rename view
---
.../user-manual/error-code/sql-error-code.cn.md | 1 +
.../user-manual/error-code/sql-error-code.en.md | 1 +
.../RenamedViewWithoutSameConfigException.java | 33 ++++++++++++++++++++++
.../impl/ShardingAlterViewStatementValidator.java | 20 +++++++++++++
.../ShardingAlterViewStatementValidatorTest.java | 26 +++++++++++++++++
.../queryable/ConvertYamlConfigurationHandler.java | 6 ++--
.../ConvertYamlConfigurationHandlerTest.java | 4 +--
7 files changed, 86 insertions(+), 5 deletions(-)
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index 4b7d6eca173..6251bf198e0 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -74,6 +74,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
| 42S02 | 20031 | Index \`%s\` does not exist |
| 44000 | 20032 | Actual tables \`%s\` are in use |
| 42S01 | 20033 | View name has to bind to %s tables |
+| 0A000 | 20034 | Alter view rename .. to .. statement should have same config for \`%s\` and \`%s\` |
| HY000 | 20050 | Routed target \`%s\` does not exist, available targets are \`%s\` |
| HY000 | 20051 | \`%s %s\` can not route correctly for %s \`%s\` |
| 42S02 | 20052 | Can not find data source in sharding rule, invalid actual data node \`%s\` |
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 1139e889803..f36e6d9edfb 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -74,6 +74,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
| 42S02 | 20031 | Index \`%s\` does not exist |
| 44000 | 20032 | Actual tables \`%s\` are in use |
| 42S01 | 20033 | View name has to bind to %s tables |
+| 0A000 | 20034 | Alter view rename .. to .. statement should have same config for \`%s\` and \`%s\` |
| HY000 | 20050 | Routed target \`%s\` does not exist, available targets are \`%s\` |
| HY000 | 20051 | \`%s %s\` can not route correctly for %s \`%s\` |
| 42S02 | 20052 | Can not find data source in sharding rule, invalid actual data node \`%s\` |
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/RenamedViewWithoutSameConfigException.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/RenamedViewWithoutSameConfigException.java
new file mode 100644
index 00000000000..a204a56a65a
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/exception/RenamedViewWithoutSameConfigException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.ShardingSphereSQLException;
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+
+/**
+ * Unsupported rename view exception.
+ */
+public final class RenamedViewWithoutSameConfigException extends ShardingSphereSQLException {
+
+ private static final long serialVersionUID = -8276278970848162458L;
+
+ public RenamedViewWithoutSameConfigException(final String originView, final String targetView) {
+ super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 20034, "Alter view rename .. to .. statement should have same config for %s and %s", originView, targetView);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
index 8f0796834b0..6329ff9021e 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
@@ -21,13 +21,16 @@ import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.exception.RenamedViewWithoutSameConfigException;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.sql.common.extractor.TableExtractor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterViewStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.AlterViewStatementHandler;
+import java.util.Arrays;
import java.util.List;
import java.util.Optional;
@@ -45,6 +48,23 @@ public final class ShardingAlterViewStatementValidator extends ShardingDDLStatem
extractor.extractTablesFromSelect(selectStatement.get());
validateShardingTable(shardingRule, "ALTER VIEW", extractor.getRewriteTables());
}
+ Optional<SimpleTableSegment> renamedView = AlterViewStatementHandler.getRenameView(sqlStatementContext.getSqlStatement());
+ if (renamedView.isPresent()) {
+ String targetView = renamedView.get().getTableName().getIdentifier().getValue();
+ String originView = sqlStatementContext.getSqlStatement().getView().getTableName().getIdentifier().getValue();
+ validateBroadcastShardingView(shardingRule, originView, targetView);
+ }
+ }
+
+ private void validateBroadcastShardingView(final ShardingRule shardingRule, final String originView, final String targetView) {
+ if (shardingRule.isBroadcastTable(originView) ^ shardingRule.isBroadcastTable(targetView)) {
+ throw new RenamedViewWithoutSameConfigException(originView, targetView);
+ }
+ if (shardingRule.isShardingTable(originView) || shardingRule.isShardingTable(targetView)) {
+ if (!shardingRule.isAllBindingTables(Arrays.asList(originView, targetView))) {
+ throw new RenamedViewWithoutSameConfigException(originView, targetView);
+ }
+ }
}
@Override
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
index bc13d3ef5ed..951db403d74 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.sharding.route.engine.validator.ddl;
import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.sharding.exception.RenamedViewWithoutSameConfigException;
import org.apache.shardingsphere.sharding.exception.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingAlterViewStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
@@ -29,6 +30,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterViewSt
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLAlterViewStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.ddl.OpenGaussAlterViewStatement;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -68,4 +70,28 @@ public final class ShardingAlterViewStatementValidatorTest {
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database);
}
+
+ @Test
+ public void assertPreValidateAlterRenamedView() {
+ OpenGaussAlterViewStatement selectStatement = new OpenGaussAlterViewStatement();
+ selectStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
+ selectStatement.setRenameView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_new"))));
+ SQLStatementContext<AlterViewStatement> sqlStatementContext = new CommonSQLStatementContext<>(selectStatement);
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(shardingRule.isBroadcastTable("t_order")).thenReturn(true);
+ when(shardingRule.isBroadcastTable("t_order_new")).thenReturn(true);
+ new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database);
+ }
+
+ @Test(expected = RenamedViewWithoutSameConfigException.class)
+ public void assertPreValidateAlterRenamedViewWithoutSameConfig() {
+ OpenGaussAlterViewStatement selectStatement = new OpenGaussAlterViewStatement();
+ selectStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
+ selectStatement.setRenameView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_new"))));
+ SQLStatementContext<AlterViewStatement> sqlStatementContext = new CommonSQLStatementContext<>(selectStatement);
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(shardingRule.isBroadcastTable("t_order")).thenReturn(true);
+ when(shardingRule.isBroadcastTable("t_order_new")).thenReturn(false);
+ new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database);
+ }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandler.java
index 26c1bf17519..57c91556890 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandler.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;
import com.google.common.base.Preconditions;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.lang3.StringUtils;
+import org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.yaml.config.rule.YamlDatabaseDiscoveryDataSourceRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.yaml.config.rule.YamlDatabaseDiscoveryHeartBeatConfiguration;
import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ConvertYamlConfigurationStatement;
@@ -42,6 +43,7 @@ import org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyData
import org.apache.shardingsphere.proxy.backend.exception.FileIOException;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.QueryableRALBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.constant.DistSQLScriptConstants;
+import org.apache.shardingsphere.readwritesplitting.yaml.config.YamlReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.yaml.config.rule.YamlReadwriteSplittingDataSourceRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.yaml.config.strategy.YamlStaticReadwriteSplittingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
@@ -52,8 +54,6 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingS
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.swapper.YamlShardingRuleConfigurationSwapper;
-import org.apache.shardingsphere.readwritesplitting.yaml.config.YamlReadwriteSplittingRuleConfiguration;
-import org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRuleConfiguration;
import java.io.File;
import java.io.IOException;
@@ -61,8 +61,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
-import java.util.Properties;
import java.util.Map.Entry;
+import java.util.Properties;
/**
* Convert YAML configuration handler.
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandlerTest.java
index 630fc3daaeb..6d6a02583e6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationHandlerTest.java
@@ -38,9 +38,9 @@ import java.util.Map;
import java.util.Objects;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;