Skip to content

Commit

Permalink
Drop sharding table rule & binding table rules & broadcast table rules (
Browse files Browse the repository at this point in the history
apache#10312)

Co-authored-by: menghaoranss <[email protected]>
  • Loading branch information
menghaoranss and menghaoranss authored May 12, 2021
1 parent 22a41d7 commit ae18a1c
Show file tree
Hide file tree
Showing 18 changed files with 743 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public enum CommonErrorCode implements SQLErrorCode {
DUPLICATE_TABLE(1113, "C1113", "Duplicate table names %s."),

SHARDING_BROADCAST_EXIST(1114, "C1114", "Sharding broadcast table rules already exist in schema %s."),


SHARDING_BINDING_TABLE_RULES_NOT_EXIST(1115, "C1115", "Sharding binding table rules not exist in schema %s."),

SHARDING_BROADCAST_TABLE_RULES_NOT_EXIST(1116, "C1116", "Sharding broadcast table rules not exist in schema %s."),

SCALING_JOB_NOT_EXIST(1201, "C1201", "Scaling job %s does not exist."),

SCALING_OPERATE_FAILED(1209, "C1209", "Scaling Operate Failed: [%s]"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,16 @@ dropReplicaQueryRule
: DROP REPLICA_QUERY RULE LP IDENTIFIER (COMMA IDENTIFIER)* RP
;

dropShardingRule
: DROP SHARDING RULE LP tableName (COMMA tableName)* RP
dropShardingTableRule
: DROP SHARDING TABLE RULE tableName (COMMA tableName)*
;

dropShardingBindingTableRules
: DROP SHARDING BINDING TABLE RULES
;

dropShardingBroadcastTableRules
: DROP SHARDING BROADCAST TABLE RULES
;

showShardingRule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ execute
| alterShardingTableRule
| alterShardingBindingTableRules
| alterShardingBroadcastTableRules
| dropShardingRule
| dropShardingTableRule
| dropShardingBindingTableRules
| dropShardingBroadcastTableRules
| createReplicaQueryRule
| alterReplicaQueryRule
| dropReplicaQueryRule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropReplicaQueryRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropResourceContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropScalingJobContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropShardingRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.FunctionDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ReplicaQueryRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ResetScalingJobContext;
Expand Down Expand Up @@ -73,7 +72,9 @@
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBindingTableRulesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBroadcastTableRulesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingTableRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
Expand Down Expand Up @@ -229,7 +230,26 @@ public ASTNode visitDropReplicaQueryRule(final DropReplicaQueryRuleContext ctx)
}
return result;
}


@Override
public ASTNode visitDropShardingTableRule(final DistSQLStatementParser.DropShardingTableRuleContext ctx) {
DropShardingTableRuleStatement result = new DropShardingTableRuleStatement();
for (TableNameContext each : ctx.tableName()) {
result.getTableNames().add((TableNameSegment) visit(each));
}
return result;
}

@Override
public ASTNode visitDropShardingBindingTableRules(final DistSQLStatementParser.DropShardingBindingTableRulesContext ctx) {
return new DropShardingBindingTableRulesStatement();
}

@Override
public ASTNode visitDropShardingBroadcastTableRules(final DistSQLStatementParser.DropShardingBroadcastTableRulesContext ctx) {
return new DropShardingBroadcastTableRulesStatement();
}

@Override
public ASTNode visitAlterReplicaQueryRuleDefinition(final AlterReplicaQueryRuleDefinitionContext ctx) {
ReadwriteSplittingRuleSegment result = new ReadwriteSplittingRuleSegment();
Expand Down Expand Up @@ -289,15 +309,6 @@ public ASTNode visitFunctionDefinition(final FunctionDefinitionContext ctx) {
return result;
}

@Override
public ASTNode visitDropShardingRule(final DropShardingRuleContext ctx) {
DropShardingRuleStatement result = new DropShardingRuleStatement();
for (TableNameContext each : ctx.tableName()) {
result.getTableNames().add((TableNameSegment) visit(each));
}
return result;
}

@Override
public ASTNode visitTableName(final TableNameContext ctx) {
return new TableNameSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBroadcastTableRulesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBindingTableRulesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBroadcastTableRulesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingTableRuleStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -72,6 +76,12 @@ public final class DistSQLStatementParserEngineTest {

private static final String RDL_ALTER_SHARDING_BROADCAST_TABLE_RULES = "ALTER SHARDING BROADCAST TABLE RULES(t_1,t_2)";

private static final String RDL_DROP_SHARDING_TABLE_RULE = "DROP SHARDING TABLE RULE t_order,t_order_item";

private static final String RDL_DROP_SHARDING_BINDING_TABLE_RULES = "DROP SHARDING BINDING TABLE RULES";

private static final String RDL_DROP_SHARDING_BROADCAST_TABLE_RULES = "DROP SHARDING BROADCAST TABLE RULES";

private final DistSQLStatementParserEngine engine = new DistSQLStatementParserEngine();

@Test
Expand Down Expand Up @@ -200,4 +210,24 @@ public void assertParseAlterShardingBroadcastTableRules() {
assertTrue(sqlStatement instanceof AlterShardingBroadcastTableRulesStatement);
assertThat(((AlterShardingBroadcastTableRulesStatement) sqlStatement).getTables(), is(Arrays.asList("t_1", "t_2")));
}

@Test
public void assertParseDropShardingTableRule() {
SQLStatement sqlStatement = engine.parse(RDL_DROP_SHARDING_TABLE_RULE);
assertTrue(sqlStatement instanceof DropShardingTableRuleStatement);
assertThat(((DropShardingTableRuleStatement) sqlStatement).getTableNames().stream().map(each -> each.getIdentifier().getValue()).collect(Collectors.toList()),
is(Arrays.asList("t_order", "t_order_item")));
}

@Test
public void assertParseDropShardingBindingTableRules() {
SQLStatement sqlStatement = engine.parse(RDL_DROP_SHARDING_BINDING_TABLE_RULES);
assertTrue(sqlStatement instanceof DropShardingBindingTableRulesStatement);
}

@Test
public void assertParseDropShardingBroadcastTableRules() {
SQLStatement sqlStatement = engine.parse(RDL_DROP_SHARDING_BROADCAST_TABLE_RULES);
assertTrue(sqlStatement instanceof DropShardingBroadcastTableRulesStatement);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.distsql.parser.statement.rdl.drop.impl;

import lombok.Getter;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropRDLStatement;

/**
* Drop sharding binding table rules statement.
*/
@Getter
public final class DropShardingBindingTableRulesStatement extends DropRDLStatement {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.distsql.parser.statement.rdl.drop.impl;

import lombok.Getter;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropRDLStatement;

/**
* Drop sharding broadcast table rules statement.
*/
@Getter
public final class DropShardingBroadcastTableRulesStatement extends DropRDLStatement {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import java.util.LinkedList;

/**
* Drop sharding rule statement.
* Drop sharding table rule statement.
*/
@Getter
public final class DropShardingRuleStatement extends DropRDLStatement {
public final class DropShardingTableRuleStatement extends DropRDLStatement {

private final Collection<TableNameSegment> tableNames = new LinkedList<>();
}
Original file line number Diff line number Diff line change
@@ -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.proxy.backend.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Sharding binding table rules not exists exception.
*/
@RequiredArgsConstructor
@Getter
public final class ShardingBindingTableRulesNotExistsException extends BackendException {

private static final long serialVersionUID = -1930065143932541970L;

private final String schemaName;
}
Original file line number Diff line number Diff line change
@@ -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.proxy.backend.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Sharding broadcast table rules not exists exception.
*/
@RequiredArgsConstructor
@Getter
public final class ShardingBroadcastTableRulesNotExistsException extends BackendException {

private static final long serialVersionUID = 2816401356514609173L;

private final String schemaName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBindingTableRulesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingBroadcastTableRulesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingTableRuleStatement;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
Expand All @@ -49,7 +51,9 @@
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropDatabaseBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropReadwriteSplittingRuleBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropResourceBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropShardingRuleBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropShardingBindingTableRulesBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropShardingBroadcastTableRulesBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropShardingTableRuleBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabaseStatement;
Expand Down Expand Up @@ -129,8 +133,14 @@ private static Optional<TextProtocolBackendHandler> createRDLBackendHandler(fina
if (sqlStatement instanceof DropDatabaseStatement) {
return Optional.of(new DropDatabaseBackendHandler((DropDatabaseStatement) sqlStatement));
}
if (sqlStatement instanceof DropShardingRuleStatement) {
return Optional.of(new DropShardingRuleBackendHandler((DropShardingRuleStatement) sqlStatement, backendConnection));
if (sqlStatement instanceof DropShardingTableRuleStatement) {
return Optional.of(new DropShardingTableRuleBackendHandler((DropShardingTableRuleStatement) sqlStatement, backendConnection));
}
if (sqlStatement instanceof DropShardingBindingTableRulesStatement) {
return Optional.of(new DropShardingBindingTableRulesBackendHandler((DropShardingBindingTableRulesStatement) sqlStatement, backendConnection));
}
if (sqlStatement instanceof DropShardingBroadcastTableRulesStatement) {
return Optional.of(new DropShardingBroadcastTableRulesBackendHandler((DropShardingBroadcastTableRulesStatement) sqlStatement, backendConnection));
}
return Optional.empty();
}
Expand Down
Loading

0 comments on commit ae18a1c

Please sign in to comment.