Skip to content

Commit

Permalink
Fix apache#29578, add RenameTableStatement to UnsupportedSQLStatement…
Browse files Browse the repository at this point in the history
…s in Proxy (apache#29582)

* Add RenameTableStatement to UnsupportedSQLStatements

* Add test cases for unsupported SQL statements in Proxy.
  • Loading branch information
RaigorJiang authored Dec 28, 2023
1 parent d5a7575 commit d6df81c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void refresh(final ModeContextManager modeContextManager, final ShardingS
private ShardingSphereTable getTable(final ShardingSphereDatabase database, final Collection<String> logicDataSourceNames, final String schemaName, final String tableName,
final ConfigurationProperties props) throws SQLException {
RuleMetaData ruleMetaData = new RuleMetaData(new LinkedList<>(database.getRuleMetaData().getRules()));
if (TableRefreshUtils.isSingleTable(tableName, database)) {
if (TableRefreshUtils.isSingleTable(tableName, database) && !logicDataSourceNames.isEmpty()) {
ruleMetaData.findRules(MutableDataNodeRule.class).forEach(each -> each.put(logicDataSourceNames.iterator().next(), schemaName, tableName));
}
GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.statement.dcl.DCLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabaseStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.RenameTableStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtils;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowCreateUserStatement;
Expand Down Expand Up @@ -222,8 +223,8 @@ private static Optional<ProxyBackendHandler> findDatabaseOperateBackendHandler(f
}

private static void checkUnsupportedSQLStatement(final SQLStatement sqlStatement) {
if (sqlStatement instanceof DCLStatement || sqlStatement instanceof FlushStatement || sqlStatement instanceof MySQLShowCreateUserStatement) {
throw new UnsupportedSQLOperationException("Unsupported operation");
if (sqlStatement instanceof DCLStatement || sqlStatement instanceof FlushStatement || sqlStatement instanceof MySQLShowCreateUserStatement || sqlStatement instanceof RenameTableStatement) {
throw new UnsupportedSQLOperationException(String.format("unsupported SQL statement `%s`", sqlStatement.getClass().getSimpleName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
Expand Down Expand Up @@ -194,30 +194,48 @@ void assertNewInstanceWithErrorRDL() {
}

@Test
void assertUnsupportedNonQueryDistSQLInTransaction() {
void assertNewInstanceWithUnsupportedDCLSQLStatement() {
String sql = "CREATE USER 'foo'@'%' IDENTIFIED BY 'bar';";
assertThrows(UnsupportedSQLOperationException.class, () -> ProxyBackendHandlerFactory.newInstance(databaseType, sql, connectionSession, new HintValueContext()));
}

@Test
void assertNewInstanceWithUnsupportedDALSQLStatement() {
assertThrows(UnsupportedSQLOperationException.class, () -> ProxyBackendHandlerFactory.newInstance(databaseType, "FLUSH PRIVILEGES;", connectionSession, new HintValueContext()));
assertThrows(UnsupportedSQLOperationException.class, () -> ProxyBackendHandlerFactory.newInstance(databaseType, "SHOW CREATE USER foo;", connectionSession, new HintValueContext()));
}

@Test
void assertNewInstanceWithUnsupportedDDLSQLStatement() {
String sql = "RENAME TABLE foo TO bar;";
assertThrows(UnsupportedSQLOperationException.class, () -> ProxyBackendHandlerFactory.newInstance(databaseType, sql, connectionSession, new HintValueContext()));
}

@Test
void assertNewInstanceWithUnsupportedNonQueryDistSQLInTransaction() {
when(connectionSession.getTransactionStatus().isInTransaction()).thenReturn(true);
String sql = "CREATE SHARDING TABLE RULE t_order (STORAGE_UNITS(ms_group_0,ms_group_1), SHARDING_COLUMN=order_id, TYPE(NAME='hash_mod', PROPERTIES('sharding-count'='4')));";
assertThrows(UnsupportedSQLOperationException.class, () -> ProxyBackendHandlerFactory.newInstance(databaseType, sql, connectionSession, new HintValueContext()));
}

@Test
void assertUnsupportedQueryableRALStatementInTransaction() throws SQLException {
void assertNewInstanceWithQueryableRALStatementInTransaction() throws SQLException {
when(connectionSession.getTransactionStatus().isInTransaction()).thenReturn(true);
String sql = "SHOW TRANSACTION RULE;";
ProxyBackendHandler actual = ProxyBackendHandlerFactory.newInstance(databaseType, sql, connectionSession, new HintValueContext());
assertThat(actual, instanceOf(QueryableRALBackendHandler.class));
}

@Test
void assertUnsupportedRQLStatementInTransaction() throws SQLException {
void assertNewInstanceWithRQLStatementInTransaction() throws SQLException {
when(connectionSession.getTransactionStatus().isInTransaction()).thenReturn(true);
String sql = "SHOW DEFAULT SINGLE TABLE STORAGE UNIT";
ProxyBackendHandler actual = ProxyBackendHandlerFactory.newInstance(databaseType, sql, connectionSession, new HintValueContext());
assertThat(actual, instanceOf(RQLBackendHandler.class));
}

@Test
void assertDistSQLRULStatementInTransaction() throws SQLException {
void assertNewInstanceWithRULStatementInTransaction() throws SQLException {
when(connectionSession.getTransactionStatus().isInTransaction()).thenReturn(true);
String sql = "PREVIEW INSERT INTO account VALUES(1, 1, 1)";
ProxyBackendHandler actual = ProxyBackendHandlerFactory.newInstance(databaseType, sql, connectionSession, new HintValueContext());
Expand Down

0 comments on commit d6df81c

Please sign in to comment.