Skip to content

Commit

Permalink
For apache#26717, fix load single table and add test (apache#26718)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaigorJiang authored Jun 30, 2023
1 parent e548705 commit 2e47519
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.single.distsql.handler.update;

import org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
Expand All @@ -33,17 +34,13 @@
import org.apache.shardingsphere.single.distsql.handler.exception.MissingRequiredSingleTableException;
import org.apache.shardingsphere.single.distsql.segment.SingleTableSegment;
import org.apache.shardingsphere.single.distsql.statement.rdl.LoadSingleTableStatement;
import org.apache.shardingsphere.single.exception.InvalidSingleRuleConfigurationException;
import org.apache.shardingsphere.single.rule.SingleRule;
import org.apache.shardingsphere.single.util.SingleTableLoadUtils;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand All @@ -54,14 +51,12 @@ public final class LoadSingleTableStatementUpdater implements RuleDefinitionCrea
@Override
public void checkSQLStatement(final ShardingSphereDatabase database, final LoadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) {
String defaultSchemaName = DatabaseTypeEngine.getDefaultSchemaName(database.getProtocolType(), database.getName());
checkTables(database, sqlStatement, defaultSchemaName);
checkDuplicatedTables(database, sqlStatement, defaultSchemaName);
checkStorageUnits(database, sqlStatement);
checkActualTableExist(database, sqlStatement, defaultSchemaName);
}

private void checkTables(final ShardingSphereDatabase database, final LoadSingleTableStatement sqlStatement, final String defaultSchemaName) {
Optional<SingleRule> currentSingleRule = database.getRuleMetaData().findSingleRule(SingleRule.class);
Collection<String> currentSingleTables = currentSingleRule.isPresent() ? currentSingleRule.get().getSingleTableDataNodes().keySet() : Collections.emptyList();
private void checkDuplicatedTables(final ShardingSphereDatabase database, final LoadSingleTableStatement sqlStatement, final String defaultSchemaName) {
Collection<SingleTableSegment> tableSegments = sqlStatement.getTables();
boolean isSchemaSupportedDatabaseType = database.getProtocolType() instanceof SchemaSupportedDatabaseType;
ShardingSphereSchema schema = database.getSchema(defaultSchemaName);
Expand All @@ -70,9 +65,7 @@ private void checkTables(final ShardingSphereDatabase database, final LoadSingle
if (SingleTableConstants.ASTERISK.equals(each.getTableName())) {
continue;
}
boolean isNotSingleTable = schema.containsTable(each.getTableName()) && !currentSingleTables.contains(each.getTableName());
ShardingSpherePreconditions.checkState(isNotSingleTable, () -> new InvalidSingleRuleConfigurationException(String.format("Table `%s` existed and is not a single table in database `%s`",
each.getTableName(), database.getName())));
ShardingSpherePreconditions.checkState(!schema.containsTable(each.getTableName()), () -> new TableExistsException(each.getTableName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.single.distsql.handler.update;

import org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
import org.apache.shardingsphere.single.distsql.segment.SingleTableSegment;
import org.apache.shardingsphere.single.distsql.statement.rdl.LoadSingleTableStatement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class LoadSingleTableStatementUpdaterTest {

private ShardingSphereDatabase database;

private ShardingSphereSchema schema;

private final LoadSingleTableStatementUpdater updater = new LoadSingleTableStatementUpdater();

@BeforeEach
void setUp() {
database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.emptyList());
when(database.getProtocolType()).thenReturn(mock(MySQLDatabaseType.class));
schema = mock(ShardingSphereSchema.class);
when(database.getSchema("foo_db")).thenReturn(schema);
}

@Test
void assertCheckWithDuplicatedTables() {
when(database.getName()).thenReturn("foo_db");
when(schema.containsTable("foo")).thenReturn(true);
LoadSingleTableStatement sqlStatement = new LoadSingleTableStatement(Collections.singletonList(new SingleTableSegment("ds_0", null, "foo")));
assertThrows(TableExistsException.class, () -> updater.checkSQLStatement(database, sqlStatement, mock(SingleRuleConfiguration.class)));
}

@Test
void assertCheckWithInvalidStorageUnit() {
when(database.getName()).thenReturn("foo_db");
LoadSingleTableStatement sqlStatement = new LoadSingleTableStatement(Collections.singletonList(new SingleTableSegment("ds_0", null, "foo")));
assertThrows(MissingRequiredStorageUnitsException.class, () -> updater.checkSQLStatement(database, sqlStatement, mock(SingleRuleConfiguration.class)));
}

@Test
void assertBuild() {
LoadSingleTableStatement sqlStatement = new LoadSingleTableStatement(Collections.singletonList(new SingleTableSegment("ds_0", null, "foo")));
SingleRuleConfiguration actual = updater.buildToBeCreatedRuleConfiguration(mock(SingleRuleConfiguration.class), sqlStatement);
assertThat(actual.getTables().iterator().next(), is("ds_0.foo"));
}

@Test
void assertUpdate() {
Collection<String> currentTables = new LinkedList<>(Collections.singletonList("ds_0.foo"));
SingleRuleConfiguration currentConfig = new SingleRuleConfiguration(currentTables, null);
SingleRuleConfiguration toBeCreatedRuleConfig = new SingleRuleConfiguration(Collections.singletonList("ds_0.bar"), null);
updater.updateCurrentRuleConfiguration(currentConfig, toBeCreatedRuleConfig);
Iterator<String> iterator = currentConfig.getTables().iterator();
assertThat(iterator.next(), is("ds_0.foo"));
assertThat(iterator.next(), is("ds_0.bar"));
}
}

0 comments on commit 2e47519

Please sign in to comment.