Skip to content

Commit

Permalink
Move ShardingValidator preValidate logic to ShardingSupportedChecker (#…
Browse files Browse the repository at this point in the history
…33832)

* Move ShardingValidator preValidate logic to ShardingSupportedChecker

* fix unit test

* fix unit test

* fix unit test
  • Loading branch information
strongduanmu authored Nov 28, 2024
1 parent e0a7bd1 commit 0825ea8
Show file tree
Hide file tree
Showing 77 changed files with 2,067 additions and 1,242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.checker;
package org.apache.shardingsphere.sharding.checker.config;

import com.google.common.base.Joiner;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.checker;
package org.apache.shardingsphere.sharding.checker.config;

import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurationEmptyChecker;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.checker.sql;

import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
import org.apache.shardingsphere.infra.checker.SupportedSQLCheckersBuilder;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterIndexSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterTableSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingAlterViewSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateFunctionSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateIndexSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateProcedureSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateTableSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingCreateViewSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingDropIndexSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingDropTableSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.ddl.ShardingRenameTableSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingCopySupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingDeleteSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingInsertSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingLoadDataSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingLoadXmlSupportedChecker;
import org.apache.shardingsphere.sharding.checker.sql.dml.ShardingUpdateSupportedChecker;
import org.apache.shardingsphere.sharding.constant.ShardingOrder;
import org.apache.shardingsphere.sharding.rule.ShardingRule;

import java.util.Arrays;
import java.util.Collection;

/**
* Sharding SQL supported checker factory.
*/
public final class ShardingSupportedSQLCheckersBuilder implements SupportedSQLCheckersBuilder<ShardingRule> {

@Override
public Collection<SupportedSQLChecker<?, ShardingRule>> getSupportedSQLCheckers() {
return Arrays.asList(
new ShardingAlterIndexSupportedChecker(),
new ShardingAlterTableSupportedChecker(),
new ShardingAlterViewSupportedChecker(),
new ShardingCreateFunctionSupportedChecker(),
new ShardingCreateIndexSupportedChecker(),
new ShardingCreateProcedureSupportedChecker(),
new ShardingCreateTableSupportedChecker(),
new ShardingCreateViewSupportedChecker(),
new ShardingDropIndexSupportedChecker(),
new ShardingDropTableSupportedChecker(),
new ShardingRenameTableSupportedChecker(),
new ShardingCopySupportedChecker(),
new ShardingDeleteSupportedChecker(),
new ShardingInsertSupportedChecker(),
new ShardingLoadDataSupportedChecker(),
new ShardingLoadXmlSupportedChecker(),
new ShardingUpdateSupportedChecker());
}

@Override
public int getOrder() {
return ShardingOrder.ORDER;
}

@Override
public Class<ShardingRule> getTypeClass() {
return ShardingRule.class;
}
}
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.sharding.checker.sql.common;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;

import java.util.Collection;

/**
* Sharding supported common checker.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingSupportedCommonChecker {

/**
* Check sharding table.
*
* @param shardingRule sharding rule
* @param operation operation
* @param tables tables
* @throws UnsupportedShardingOperationException unsupported sharding operation exception
*/
public static void checkShardingTable(final ShardingRule shardingRule, final String operation, final Collection<SimpleTableSegment> tables) {
for (SimpleTableSegment each : tables) {
String tableName = each.getTableName().getIdentifier().getValue();
ShardingSpherePreconditions.checkState(!shardingRule.isShardingTable(tableName), () -> new UnsupportedShardingOperationException(operation, tableName));
}
}

/**
* Check table exist.
*
* @param schema ShardingSphere schema
* @param tables tables
* @throws NoSuchTableException no such table exception
*/
public static void checkTableExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) {
for (SimpleTableSegment each : tables) {
String tableName = each.getTableName().getIdentifier().getValue();
ShardingSpherePreconditions.checkState(schema.containsTable(tableName), () -> new NoSuchTableException(tableName));
}
}

/**
* Check table not exist.
*
* @param schema ShardingSphere schema
* @param tables tables
* @throws TableExistsException table exists exception
*/
public static void checkTableNotExist(final ShardingSphereSchema schema, final Collection<SimpleTableSegment> tables) {
for (SimpleTableSegment each : tables) {
String tableName = each.getTableName().getIdentifier().getValue();
ShardingSpherePreconditions.checkState(!schema.containsTable(tableName), () -> new TableExistsException(tableName));
}
}

/**
* Check multiple table.
*
* @param shardingRule sharding rule
* @param sqlStatementContext sqlStatementContext
*/
public static void checkMultipleTable(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext) {
Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames();
boolean isAllShardingTables = shardingRule.isAllShardingTables(tableNames) && (1 == tableNames.size() || shardingRule.isAllBindingTables(tableNames));
boolean isAllSingleTables = !shardingRule.containsShardingTable(tableNames);
ShardingSpherePreconditions.checkState(isAllShardingTables || isAllSingleTables, () -> new DMLWithMultipleShardingTablesException(tableNames));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,43 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
package org.apache.shardingsphere.sharding.checker.sql.ddl;

import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterIndexStatementContext;
import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.checker.sql.util.ShardingSupportedCheckUtils;
import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException;
import org.apache.shardingsphere.sharding.exception.metadata.IndexNotExistedException;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.ShardingDDLStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterIndexStatement;

import java.util.List;
import java.util.Optional;

/**
* Sharding alter index statement validator.
* Alter index supported checker for sharding.
*/
public final class ShardingAlterIndexStatementValidator extends ShardingDDLStatementValidator {
public final class ShardingAlterIndexSupportedChecker implements SupportedSQLChecker<AlterIndexStatementContext, ShardingRule> {

@Override
public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext,
final List<Object> params, final ShardingSphereDatabase database, final ConfigurationProperties props) {
AlterIndexStatement alterIndexStatement = (AlterIndexStatement) sqlStatementContext.getSqlStatement();
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
return sqlStatementContext instanceof AlterIndexStatementContext;
}

@Override
public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final AlterIndexStatementContext sqlStatementContext) {
AlterIndexStatement alterIndexStatement = sqlStatementContext.getSqlStatement();
Optional<IndexSegment> index = alterIndexStatement.getIndex();
String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName());
ShardingSphereSchema schema = index.flatMap(optional -> optional.getOwner()
.map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> database.getSchema(defaultSchemaName));
if (index.isPresent() && !isSchemaContainsIndex(schema, index.get())) {
ShardingSphereSchema schema = index.flatMap(optional -> optional.getOwner().map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElse(currentSchema);
if (index.isPresent() && !ShardingSupportedCheckUtils.isSchemaContainsIndex(schema, index.get())) {
throw new IndexNotExistedException(index.get().getIndexName().getIdentifier().getValue());
}
Optional<IndexSegment> renameIndex = alterIndexStatement.getRenameIndex();
if (renameIndex.isPresent() && isSchemaContainsIndex(schema, renameIndex.get())) {
if (renameIndex.isPresent() && ShardingSupportedCheckUtils.isSchemaContainsIndex(schema, renameIndex.get())) {
throw new DuplicateIndexException(renameIndex.get().getIndexName().getIdentifier().getValue());
}
}

@Override
public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
}
}
Original file line number Diff line number Diff line change
@@ -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.sharding.checker.sql.ddl;

import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterTableStatementContext;
import org.apache.shardingsphere.infra.checker.SupportedSQLChecker;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Alter table supported checker for sharding.
*/
public final class ShardingAlterTableSupportedChecker implements SupportedSQLChecker<AlterTableStatementContext, ShardingRule> {

@Override
public boolean isCheck(final SQLStatementContext sqlStatementContext) {
return sqlStatementContext instanceof AlterTableStatementContext;
}

@Override
public void check(final ShardingRule rule, final ShardingSphereDatabase database, final ShardingSphereSchema currentSchema, final AlterTableStatementContext sqlStatementContext) {
Collection<String> tableNames = sqlStatementContext.getTablesContext().getSimpleTables().stream()
.map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList());
Optional<SimpleTableSegment> renameTable = sqlStatementContext.getSqlStatement().getRenameTable();
ShardingSpherePreconditions.checkState(!renameTable.isPresent() || !rule.containsShardingTable(tableNames),
() -> new UnsupportedShardingOperationException("ALTER TABLE ... RENAME TO ...", renameTable.map(optional -> optional.getTableName().getIdentifier().getValue()).orElse("")));
}
}
Loading

0 comments on commit 0825ea8

Please sign in to comment.