From bafd5d985a6bcb268a382a3dbac953e99442df56 Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 8 Oct 2024 18:34:25 +0800 Subject: [PATCH 1/3] Fix table exist exception when execute preview create table statement with exist table --- .../user-manual/common-config/sql-hint.cn.md | 29 +++++++++++++------ .../user-manual/common-config/sql-hint.en.md | 29 +++++++++++++------ .../route/engine/ShardingSQLRouter.java | 2 +- .../validator/ShardingStatementValidator.java | 4 ++- .../ShardingAlterIndexStatementValidator.java | 2 +- .../ShardingAlterTableStatementValidator.java | 2 +- .../ShardingAlterViewStatementValidator.java | 2 +- ...rdingCreateFunctionStatementValidator.java | 16 +++++----- ...ShardingCreateIndexStatementValidator.java | 2 +- ...dingCreateProcedureStatementValidator.java | 18 +++++++----- ...ShardingCreateTableStatementValidator.java | 4 +-- .../ShardingCreateViewStatementValidator.java | 2 +- .../ShardingDropIndexStatementValidator.java | 2 +- .../ShardingDropTableStatementValidator.java | 2 +- .../ShardingPrepareStatementValidator.java | 4 +-- ...ShardingRenameTableStatementValidator.java | 4 +-- .../impl/ShardingCopyStatementValidator.java | 2 +- .../ShardingDeleteStatementValidator.java | 2 +- .../ShardingInsertStatementValidator.java | 2 +- .../ShardingLoadDataStatementValidator.java | 2 +- .../ShardingLoadXMLStatementValidator.java | 2 +- .../ShardingUpdateStatementValidator.java | 4 +-- ...rdingAlterIndexStatementValidatorTest.java | 15 +++++----- ...rdingAlterTableStatementValidatorTest.java | 3 +- ...ardingAlterViewStatementValidatorTest.java | 10 +++++-- ...gCreateFunctionStatementValidatorTest.java | 17 ++++++----- ...dingCreateIndexStatementValidatorTest.java | 29 +++++++++---------- ...CreateProcedureStatementValidatorTest.java | 17 ++++++----- ...dingCreateTableStatementValidatorTest.java | 6 ++-- ...rdingCreateViewStatementValidatorTest.java | 9 +++--- ...ardingDropIndexStatementValidatorTest.java | 9 +++--- ...ardingDropTableStatementValidatorTest.java | 2 +- ...dingRenameTableStatementValidatorTest.java | 6 ++-- .../ShardingCopyStatementValidatorTest.java | 13 +++++---- .../ShardingDeleteStatementValidatorTest.java | 3 +- .../ShardingInsertStatementValidatorTest.java | 20 ++++++------- ...hardingLoadDataStatementValidatorTest.java | 9 +++--- ...ShardingLoadXMLStatementValidatorTest.java | 7 +++-- .../ShardingUpdateStatementValidatorTest.java | 8 ++--- .../infra/hint/HintValueContext.java | 2 ++ .../infra/hint/SQLHintPropertiesKey.java | 5 ++++ .../infra/hint/SQLHintUtils.java | 3 ++ .../infra/hint/SQLHintUtilsTest.java | 6 ++++ .../single/route/SingleSQLRouter.java | 4 +-- .../engine/SingleRouteEngineFactory.java | 6 ++-- .../engine/SingleStandardRouteEngine.java | 5 +++- .../engine/SingleRouteEngineFactoryTest.java | 11 +++---- .../engine/SingleStandardRouteEngineTest.java | 13 +++++---- .../handler/distsql/rul/PreviewExecutor.java | 1 + 49 files changed, 220 insertions(+), 157 deletions(-) diff --git a/docs/document/content/user-manual/common-config/sql-hint.cn.md b/docs/document/content/user-manual/common-config/sql-hint.cn.md index 6f73610d6a034..b3c22b825457e 100644 --- a/docs/document/content/user-manual/common-config/sql-hint.cn.md +++ b/docs/document/content/user-manual/common-config/sql-hint.cn.md @@ -24,15 +24,16 @@ ShardingSphere 的 SQL Hint 语法格式如下: ShardingSphere SQL Hint 中可以定义如下的属性,为了兼容低版本 SQL Hint 语法,也可以使用别名中定义的属性: -| *名称* | *别名* | *数据类型* | *说明* | *默认值* | -|-----------------------------|-----------------------|------------|---------------------------------------|-------| -| SHARDING_DATABASE_VALUE (?) | shardingDatabaseValue | Comparable | 数据分片分库值,和 Hint 分片策略配合使用 | - | -| SHARDING_TABLE_VALUE (?) | shardingTableValue | Comparable | 数据分片分表值,和 Hint 分片策略配合使用 | - | -| WRITE_ROUTE_ONLY (?) | writeRouteOnly | boolean | 读写分离强制路由到主库执行 | false | -| DATA_SOURCE_NAME (?) | dataSourceName | String | 数据源透传,将 SQL 直接路由到指定数据源 | - | -| SKIP_SQL_REWRITE (?) | skipSQLRewrite | boolean | 跳过 SQL 改写阶段 | false | -| DISABLE_AUDIT_NAMES (?) | disableAuditNames | String | 禁用指定 SQL 审计算法 | - | -| SHADOW (?) | shadow | boolean | 影子库强制路由到影子库数据源执行,和影子库 SQL_HINT 算法配合使用 | false | +| *名称* | *别名* | *数据类型* | *说明* | *默认值* | +|------------------------------------|------------------------|------------|---------------------------------------|-------| +| SHARDING_DATABASE_VALUE (?) | shardingDatabaseValue | Comparable | 数据分片分库值,和 Hint 分片策略配合使用 | - | +| SHARDING_TABLE_VALUE (?) | shardingTableValue | Comparable | 数据分片分表值,和 Hint 分片策略配合使用 | - | +| WRITE_ROUTE_ONLY (?) | writeRouteOnly | boolean | 读写分离强制路由到主库执行 | false | +| DATA_SOURCE_NAME (?) | dataSourceName | String | 数据源透传,将 SQL 直接路由到指定数据源 | - | +| SKIP_SQL_REWRITE (?) | skipSQLRewrite | boolean | 跳过 SQL 改写阶段 | false | +| SKIP_METADATA_VALIDATE (?) | skipMetadataValidate | boolean | 跳过 SQL 执行元数据校验 | false | +| DISABLE_AUDIT_NAMES (?) | disableAuditNames | String | 禁用指定 SQL 审计算法 | - | +| SHADOW (?) | shadow | boolean | 影子库强制路由到影子库数据源执行,和影子库 SQL_HINT 算法配合使用 | false | ## SQL Hint @@ -82,6 +83,16 @@ ShardingSphere SQL Hint 中可以定义如下的属性,为了兼容低版本 S /* SHARDINGSPHERE_HINT: SKIP_SQL_REWRITE=true */ SELECT * FROM t_order; ``` +### 跳过 SQL 执行元数据校验 + +跳过 SQL 执行元数据校验 SQL Hint 功能可选属性为 `SKIP_METADATA_VALIDATE`,`true` 表示跳过当前 SQL 执行的元数据校验。 + +跳过 SQL 执行元数据校验 SQL Hint 功能的使用示例: + +```sql +/* SHARDINGSPHERE_HINT: SKIP_METADATA_VALIDATE=true */ SELECT * FROM t_order; +``` + ### 禁用 SQL 审计 禁用 SQL 审计 SQL Hint 功能可选属性为 `DISABLE_AUDIT_NAMES`,需要指定需要禁用的 SQL 审计算法名称,多个 SQL 审计算法需要使用逗号分隔。 diff --git a/docs/document/content/user-manual/common-config/sql-hint.en.md b/docs/document/content/user-manual/common-config/sql-hint.en.md index 6374fe5aa2659..a21d9a995ff69 100644 --- a/docs/document/content/user-manual/common-config/sql-hint.en.md +++ b/docs/document/content/user-manual/common-config/sql-hint.en.md @@ -24,15 +24,16 @@ If you use the MySQL client to connect, you need to add the `-c` option to retai The following attributes can be defined in ShardingSphere SQL Hint. In order to be compatible with the lower version SQL Hint syntax, the attributes defined in the alias can also be used: -| *Name* | *Alias* | *Data Type* | *Description* | *Default Value* | -|-----------------------------|-----------------------|-------------|---------------------------------------------------------------------------|-----------------| -| SHARDING_DATABASE_VALUE (?) | shardingDatabaseValue | Comparable | Database sharding value, used when config Hint sharding strategy | - | -| SHARDING_TABLE_VALUE (?) | shardingTableValue | Comparable | Table sharding value, used when config Hint sharding strategy | - | -| WRITE_ROUTE_ONLY (?) | writeRouteOnly | boolean | Route to the write datasource when use readwrite-splitting | false | -| DATA_SOURCE_NAME (?) | dataSourceName | String | Data source pass through, route SQL directly to the specified data source | - | -| SKIP_SQL_REWRITE (?) | skipSQLRewrite | boolean | Skip the SQL rewrite phase | false | -| DISABLE_AUDIT_NAMES (?) | disableAuditNames | String | Disable the specified SQL audit algorithm | - | -| SHADOW (?) | shadow | boolean | Route to the shadow datasource when use shadow | false | +| *Name* | *Alias* | *Data Type* | *Description* | *Default Value* | +|-------------------------------|-----------------------|-------------|------------------------------------------------------------------------|-----------------| +| SHARDING_DATABASE_VALUE (?) | shardingDatabaseValue | Comparable | Database sharding value, used when config Hint sharding strategy | - | +| SHARDING_TABLE_VALUE (?) | shardingTableValue | Comparable | Table sharding value, used when config Hint sharding strategy | - | +| WRITE_ROUTE_ONLY (?) | writeRouteOnly | boolean | Route to the write datasource when use readwrite-splitting | false | +| DATA_SOURCE_NAME (?) | dataSourceName | String | Data source pass through, route SQL directly to the specified data source | - | +| SKIP_SQL_REWRITE (?) | skipSQLRewrite | boolean | Skip the SQL rewrite phase | false | +| SKIP_METADATA_VALIDATE (?) | skipMetadataValidate | boolean | Skip the SQL metadata validate | false | +| DISABLE_AUDIT_NAMES (?) | disableAuditNames | String | Disable the specified SQL audit algorithm | - | +| SHADOW (?) | shadow | boolean | Route to the shadow datasource when use shadow | false | ## SQL Hint @@ -82,6 +83,16 @@ An example of skipping SQL rewrite SQL Hint: /* SHARDINGSPHERE_HINT: SKIP_SQL_REWRITE=true */ SELECT * FROM t_order; ``` +### SKIP METADATA VALIDATE + +The optional attribute of skip SQL metadata validate SQL Hint is `SKIP_METADATA_VALIDATE`, and `true` means skipping the current SQL metadata validate. + +An example of skipping SQL metadata validate SQL Hint: + +```sql +/* SHARDINGSPHERE_HINT: SKIP_METADATA_VALIDATE=true */ SELECT * FROM t_order; +``` + ### DISABLE SQL AUDIT The optional attribute of disable SQL audit is `DISABLE_AUDIT_NAMES`, you need to specify names of SQL audit algorithm that needs to be disabled, and multiple SQL audit algorithms need to be separated by commas. diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java index bdf45aa7baa4e..0d597ca5d5669 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java @@ -65,7 +65,7 @@ private RouteContext createRouteContext0(final QueryContext queryContext, final SQLStatement sqlStatement = queryContext.getSqlStatementContext().getSqlStatement(); ShardingConditions shardingConditions = createShardingConditions(queryContext, globalRuleMetaData, database, rule); Optional validator = ShardingStatementValidatorFactory.newInstance(sqlStatement, shardingConditions); - validator.ifPresent(optional -> optional.preValidate(rule, queryContext.getSqlStatementContext(), queryContext.getParameters(), database, props)); + validator.ifPresent(optional -> optional.preValidate(rule, queryContext.getSqlStatementContext(), queryContext.getHintValueContext(), queryContext.getParameters(), database, props)); if (sqlStatement instanceof DMLStatement && shardingConditions.isNeedMerge()) { shardingConditions.merge(); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java index 45680dd4605a7..dac1b31d3bc04 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java @@ -36,11 +36,13 @@ public interface ShardingStatementValidator { * * @param shardingRule sharding rule * @param sqlStatementContext SQL statement context + * @param hintValueContext hint value context * @param params SQL parameters * @param database database * @param props props */ - void preValidate(ShardingRule shardingRule, SQLStatementContext sqlStatementContext, List params, ShardingSphereDatabase database, ConfigurationProperties props); + void preValidate(ShardingRule shardingRule, SQLStatementContext sqlStatementContext, HintValueContext hintValueContext, List params, ShardingSphereDatabase database, + ConfigurationProperties props); /** * Validate whether sharding operation is supported after route. diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidator.java index db9dd340716ab..33cb119fd40c1 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterIndexStatementValidator.java @@ -40,7 +40,7 @@ public final class ShardingAlterIndexStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { AlterIndexStatement alterIndexStatement = (AlterIndexStatement) sqlStatementContext.getSqlStatement(); Optional index = alterIndexStatement.getIndex(); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java index 4f8b57ddc32a3..3532a111cca2e 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java @@ -42,7 +42,7 @@ public final class ShardingAlterTableStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { AlterTableStatementContext alterTableStatementContext = (AlterTableStatementContext) sqlStatementContext; Collection tableNames = alterTableStatementContext.getTablesContext().getSimpleTables().stream() diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java index 58393a9b82a7c..cb54e57a417da 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java @@ -42,7 +42,7 @@ public final class ShardingAlterViewStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { AlterViewStatement alterViewStatement = (AlterViewStatement) sqlStatementContext.getSqlStatement(); Optional selectStatement = alterViewStatement.getSelectStatement(); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java index 2c089eecfbe46..2a1f85a0499ea 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java @@ -26,10 +26,10 @@ import org.apache.shardingsphere.infra.route.context.RouteContext; 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.util.TableExtractor; import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateFunctionStatement; +import org.apache.shardingsphere.sql.parser.statement.core.util.TableExtractor; import java.util.Collection; import java.util.List; @@ -41,7 +41,7 @@ public final class ShardingCreateFunctionStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { CreateFunctionStatement createFunctionStatement = (CreateFunctionStatement) sqlStatementContext.getSqlStatement(); Optional routineBodySegment = createFunctionStatement.getRoutineBody(); @@ -51,11 +51,13 @@ public void preValidate(final ShardingRule shardingRule, final SQLStatementConte TableExtractor extractor = new TableExtractor(); Collection existTables = extractor.extractExistTableFromRoutineBody(routineBodySegment.get()); validateShardingTable(shardingRule, "CREATE FUNCTION", existTables); - String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName()); - ShardingSphereSchema schema = createFunctionStatement.getFunctionName().flatMap(optional -> optional.getOwner() - .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> database.getSchema(defaultSchemaName)); - validateTableExist(schema, existTables); - validateTableNotExist(schema, extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get())); + if (!hintValueContext.isSkipMetadataValidate()) { + String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName()); + ShardingSphereSchema schema = createFunctionStatement.getFunctionName().flatMap(optional -> optional.getOwner() + .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> database.getSchema(defaultSchemaName)); + validateTableExist(schema, existTables); + validateTableNotExist(schema, extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get())); + } } @Override diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java index a669b216d13ea..ce60d5ed7e50b 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java @@ -39,7 +39,7 @@ public final class ShardingCreateIndexStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { CreateIndexStatementContext createIndexStatementContext = (CreateIndexStatementContext) sqlStatementContext; CreateIndexStatement createIndexStatement = createIndexStatementContext.getSqlStatement(); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java index 8a4ed47b8a858..d852a2843f5a0 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java @@ -26,10 +26,10 @@ import org.apache.shardingsphere.infra.route.context.RouteContext; 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.util.TableExtractor; import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.routine.RoutineBodySegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateProcedureStatement; +import org.apache.shardingsphere.sql.parser.statement.core.util.TableExtractor; import java.util.Collection; import java.util.List; @@ -41,7 +41,7 @@ public final class ShardingCreateProcedureStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { CreateProcedureStatement createProcedureStatement = (CreateProcedureStatement) sqlStatementContext.getSqlStatement(); Optional routineBodySegment = createProcedureStatement.getRoutineBody(); @@ -49,14 +49,16 @@ public void preValidate(final ShardingRule shardingRule, final SQLStatementConte return; } TableExtractor extractor = new TableExtractor(); - String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName()); - ShardingSphereSchema schema = createProcedureStatement.getProcedureName().flatMap(optional -> optional.getOwner() - .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> database.getSchema(defaultSchemaName)); Collection existTables = extractor.extractExistTableFromRoutineBody(routineBodySegment.get()); validateShardingTable(shardingRule, "CREATE PROCEDURE", existTables); - validateTableExist(schema, existTables); - Collection notExistTables = extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get()); - validateTableNotExist(schema, notExistTables); + if (!hintValueContext.isSkipMetadataValidate()) { + String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName()); + ShardingSphereSchema schema = createProcedureStatement.getProcedureName().flatMap(optional -> optional.getOwner() + .map(owner -> database.getSchema(owner.getIdentifier().getValue()))).orElseGet(() -> database.getSchema(defaultSchemaName)); + validateTableExist(schema, existTables); + Collection notExistTables = extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get()); + validateTableNotExist(schema, notExistTables); + } } @Override diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java index 6d12af3cd5359..3aa71182621ee 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java @@ -39,11 +39,11 @@ public final class ShardingCreateTableStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { CreateTableStatementContext createTableStatementContext = (CreateTableStatementContext) sqlStatementContext; CreateTableStatement createTableStatement = createTableStatementContext.getSqlStatement(); - if (!createTableStatement.isIfNotExists()) { + if (!createTableStatement.isIfNotExists() && !hintValueContext.isSkipMetadataValidate()) { String defaultSchemaName = new DatabaseTypeRegistry(sqlStatementContext.getDatabaseType()).getDefaultSchemaName(database.getName()); ShardingSphereSchema schema = createTableStatementContext.getTablesContext().getSchemaName() .map(database::getSchema).orElseGet(() -> database.getSchema(defaultSchemaName)); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java index b2a3bfc7dbcb4..af46513690d33 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java @@ -44,7 +44,7 @@ public final class ShardingCreateViewStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { TableExtractor extractor = new TableExtractor(); extractor.extractTablesFromSelect(((CreateViewStatement) sqlStatementContext.getSqlStatement()).getSelect()); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidator.java index b282b33d3d355..561ffb6eea6ec 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropIndexStatementValidator.java @@ -42,7 +42,7 @@ public final class ShardingDropIndexStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { DropIndexStatement dropIndexStatement = (DropIndexStatement) sqlStatementContext.getSqlStatement(); if (dropIndexStatement.isIfExists()) { diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java index 071f15474cc95..7b26e30d75054 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java @@ -48,7 +48,7 @@ public final class ShardingDropTableStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { DropTableStatementContext dropTableStatementContext = (DropTableStatementContext) sqlStatementContext; DropTableStatement dropTableStatement = dropTableStatementContext.getSqlStatement(); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingPrepareStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingPrepareStatementValidator.java index 277adc1e31d39..8d0a4cfb6a79f 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingPrepareStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingPrepareStatementValidator.java @@ -37,8 +37,8 @@ public final class ShardingPrepareStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final List params, final ShardingSphereDatabase database, - final ConfigurationProperties props) { + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, + final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { } @Override diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidator.java index 6d4311e9e19a5..e77753919466e 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingRenameTableStatementValidator.java @@ -42,8 +42,8 @@ public final class ShardingRenameTableStatementValidator extends ShardingDDLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final List params, final ShardingSphereDatabase database, - final ConfigurationProperties props) { + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, + final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { RenameTableStatementContext renameTableStatementContext = (RenameTableStatementContext) sqlStatementContext; Collection tableNames = renameTableStatementContext.getTablesContext().getSimpleTables().stream() .map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidator.java index ad743178b7cda..cab25e0081a55 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingCopyStatementValidator.java @@ -35,7 +35,7 @@ public final class ShardingCopyStatementValidator extends ShardingDMLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { String tableName = ((CopyStatement) sqlStatementContext.getSqlStatement()).getTableSegment().getTableName().getIdentifier().getValue(); if (shardingRule.isShardingTable(tableName)) { diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidator.java index 8d45496f88b44..ed3388c18a062 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingDeleteStatementValidator.java @@ -35,7 +35,7 @@ public final class ShardingDeleteStatementValidator extends ShardingDMLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { validateMultipleTable(shardingRule, sqlStatementContext); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java index 832ae891b026d..c79d9347c643d 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java @@ -55,7 +55,7 @@ public final class ShardingInsertStatementValidator extends ShardingDMLStatement private final ShardingConditions shardingConditions; @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { if (null == ((InsertStatementContext) sqlStatementContext).getInsertSelectContext()) { validateMultipleTable(shardingRule, sqlStatementContext); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidator.java index ca79c2f4c1a89..e3d6e7fcccf47 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadDataStatementValidator.java @@ -35,7 +35,7 @@ public final class ShardingLoadDataStatementValidator extends ShardingDMLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { String tableName = ((LoadDataStatement) sqlStatementContext.getSqlStatement()).getTableSegment().getTableName().getIdentifier().getValue(); if (shardingRule.isShardingTable(tableName)) { diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidator.java index 5bcb06d1fa7b6..310588c987952 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingLoadXMLStatementValidator.java @@ -35,7 +35,7 @@ public final class ShardingLoadXMLStatementValidator extends ShardingDMLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { String tableName = ((LoadXMLStatement) sqlStatementContext.getSqlStatement()).getTableSegment().getTableName().getIdentifier().getValue(); if (shardingRule.isShardingTable(tableName)) { diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidator.java index 0395254223225..42b81c372b819 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidator.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingUpdateStatementValidator.java @@ -41,8 +41,8 @@ public final class ShardingUpdateStatementValidator extends ShardingDMLStatementValidator { @Override - public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final List params, final ShardingSphereDatabase database, - final ConfigurationProperties props) { + public void preValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, + final List params, final ShardingSphereDatabase database, final ConfigurationProperties props) { validateMultipleTable(shardingRule, sqlStatementContext); } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterIndexStatementValidatorTest.java index ae2ba3913fa76..bcf17a54279f9 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterIndexStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterIndexStatementValidatorTest.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterIndexStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException; @@ -62,8 +63,8 @@ void assertPreValidateAlterIndexWhenIndexExistRenameIndexNotExistForPostgreSQL() when(database.getSchema("public").getTable("t_order")).thenReturn(table); when(table.containsIndex("t_order_index")).thenReturn(true); when(table.containsIndex("t_order_index_new")).thenReturn(false); - assertDoesNotThrow(() -> new ShardingAlterIndexStatementValidator().preValidate( - shardingRule, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingAlterIndexStatementValidator().preValidate(shardingRule, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -75,9 +76,8 @@ void assertPreValidateAlterIndexWhenIndexNotExistRenameIndexNotExistForPostgreSQ when(database.getSchema("public").getAllTableNames()).thenReturn(Collections.singletonList("t_order")); when(database.getSchema("public").getTable("t_order")).thenReturn(table); when(table.containsIndex("t_order_index")).thenReturn(false); - assertThrows(IndexNotExistedException.class, - () -> new ShardingAlterIndexStatementValidator().preValidate( - shardingRule, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(IndexNotExistedException.class, () -> new ShardingAlterIndexStatementValidator().preValidate(shardingRule, + new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -90,8 +90,7 @@ void assertPreValidateAlterIndexWhenIndexExistRenameIndexExistForPostgreSQL() { when(database.getSchema("public").getTable("t_order")).thenReturn(table); when(table.containsIndex("t_order_index")).thenReturn(true); when(table.containsIndex("t_order_index_new")).thenReturn(true); - assertThrows(DuplicateIndexException.class, - () -> new ShardingAlterIndexStatementValidator().preValidate( - shardingRule, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(DuplicateIndexException.class, () -> new ShardingAlterIndexStatementValidator().preValidate(shardingRule, new AlterIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterTableStatementValidatorTest.java index 5e98f915fd98e..731f49277e8fd 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterTableStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterTableStatementValidatorTest.java @@ -71,7 +71,8 @@ void assertPreValidateAlterTableWithRenameTableWithShardingTableForPostgreSQL() SQLStatementContext sqlStatementContext = new AlterTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); when(shardingRule.containsShardingTable(Arrays.asList("t_order", "t_order_new"))).thenReturn(true); assertThrows(UnsupportedShardingOperationException.class, - () -> new ShardingAlterTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + () -> new ShardingAlterTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java index fea488fdbdb60..2c7d799b8b3db 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.ddl.AlterViewStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.exception.metadata.EngagedViewException; import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingAlterViewStatementValidator; @@ -59,7 +60,8 @@ void assertPreValidateAlterViewForMySQL() { SQLStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); when(shardingRule.isShardingTable("t_order")).thenReturn(false); - assertDoesNotThrow(() -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } @Test @@ -73,7 +75,8 @@ void assertPreValidateAlterViewWithShardingTableForMySQL() { SQLStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); when(shardingRule.isShardingTable("t_order")).thenReturn(true); assertThrows(EngagedViewException.class, - () -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + () -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } @Test @@ -83,6 +86,7 @@ void assertPreValidateAlterRenamedView() { sqlStatement.setRenameView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order_new")))); SQLStatementContext sqlStatementContext = new AlterViewStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); - assertDoesNotThrow(() -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingAlterViewStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java index 4416dc529c515..3ce61e553b7ed 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateFunctionStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateFunctionStatementValidator; import org.apache.shardingsphere.sharding.rule.ShardingRule; @@ -72,8 +73,8 @@ void assertPreValidateCreateFunctionForMySQL() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order_item")).thenReturn(true); - assertDoesNotThrow(() -> new ShardingCreateFunctionStatementValidator().preValidate( - shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } @Test @@ -89,8 +90,8 @@ void assertPreValidateCreateFunctionWithShardingTableForMySQL() { SQLStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("db_schema"); - assertThrows(NoSuchTableException.class, - () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(NoSuchTableException.class, () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -106,8 +107,8 @@ void assertPreValidateCreateFunctionWithNoSuchTableForMySQL() { SQLStatementContext sqlStatementContext = new CreateFunctionStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("db_schema"); - assertThrows(NoSuchTableException.class, - () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(NoSuchTableException.class, () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -124,7 +125,7 @@ void assertPreValidateCreateFunctionWithTableExistsForMySQL() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order")).thenReturn(true); - assertThrows(TableExistsException.class, - () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(TableExistsException.class, () -> new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java index 62e1e2b662ea8..4196818e64e37 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException; import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateIndexStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.sharding.exception.metadata.DuplicateIndexException; @@ -63,8 +64,8 @@ void assertPreValidateCreateIndexWhenTableExistIndexNotExistForPostgreSQL() { when(database.getSchema("public").containsTable("t_order")).thenReturn(true); ShardingSphereTable table = mock(ShardingSphereTable.class); when(database.getSchema("public").getTable("t_order")).thenReturn(table); - assertDoesNotThrow(() -> new ShardingCreateIndexStatementValidator().preValidate( - shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -73,9 +74,8 @@ void assertPreValidateCreateIndexWhenTableNotExistIndexNotExistForPostgreSQL() { sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); sqlStatement.setIndex(new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("t_order_index")))); when(database.getSchema("public").containsTable("t_order")).thenReturn(false); - assertThrows(NoSuchTableException.class, - () -> new ShardingCreateIndexStatementValidator().preValidate( - shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(NoSuchTableException.class, () -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -87,9 +87,8 @@ void assertPreValidateCreateIndexWhenTableExistIndexExistForPostgreSQL() { ShardingSphereTable table = mock(ShardingSphereTable.class); when(database.getSchema("public").getTable("t_order")).thenReturn(table); when(database.getSchema("public").containsIndex("t_order", "t_order_index")).thenReturn(true); - assertThrows(DuplicateIndexException.class, - () -> new ShardingCreateIndexStatementValidator().preValidate( - shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(DuplicateIndexException.class, () -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, + new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -101,8 +100,8 @@ void assertPreValidateCreateIndexWithoutIndexNameWhenTableExistIndexNotExistForP when(database.getSchema("public").containsTable("t_order")).thenReturn(true); ShardingSphereTable table = mock(ShardingSphereTable.class); when(database.getSchema("public").getTable("t_order")).thenReturn(table); - assertDoesNotThrow(() -> new ShardingCreateIndexStatementValidator().preValidate( - shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -112,9 +111,8 @@ void assertPreValidateCreateIndexWithoutIndexNameWhenTableNotExistIndexNotExistF sqlStatement.getColumns().add(new ColumnSegment(0, 0, new IdentifierValue("content"))); sqlStatement.setGeneratedIndexStartIndex(10); when(database.getSchema("public").containsTable("t_order")).thenReturn(false); - assertThrows(NoSuchTableException.class, - () -> new ShardingCreateIndexStatementValidator().preValidate( - shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(NoSuchTableException.class, () -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -127,8 +125,7 @@ void assertPreValidateCreateIndexWithoutIndexNameWhenTableExistIndexExistForPost ShardingSphereTable table = mock(ShardingSphereTable.class); when(database.getSchema("public").getTable("t_order")).thenReturn(table); when(database.getSchema("public").containsIndex("t_order", "content_idx")).thenReturn(true); - assertThrows(DuplicateIndexException.class, - () -> new ShardingCreateIndexStatementValidator().preValidate( - shardingRule, new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(DuplicateIndexException.class, () -> new ShardingCreateIndexStatementValidator().preValidate(shardingRule, + new CreateIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java index 480318fe09001..d2a845c1bbe4f 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateProcedureStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateProcedureStatementValidator; import org.apache.shardingsphere.sharding.rule.ShardingRule; @@ -73,8 +74,8 @@ void assertPreValidateCreateProcedureForMySQL() { when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order_item")).thenReturn(true); when(shardingRule.isShardingTable("t_order_item")).thenReturn(false); SQLStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); - assertDoesNotThrow(() -> new ShardingCreateProcedureStatementValidator().preValidate( - shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } @Test @@ -90,8 +91,8 @@ void assertPreValidateCreateProcedureWithShardingTableForMySQL() { SQLStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("db_schema"); - assertThrows(NoSuchTableException.class, - () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(NoSuchTableException.class, () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -107,8 +108,8 @@ void assertPreValidateCreateProcedureWithNoSuchTableForMySQL() { SQLStatementContext sqlStatementContext = new CreateProcedureStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("db_schema"); - assertThrows(NoSuchTableException.class, - () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(NoSuchTableException.class, () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -125,7 +126,7 @@ void assertPreValidateCreateProcedureWithTableExistsForMySQL() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("t_order")).thenReturn(true); - assertThrows(TableExistsException.class, - () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(TableExistsException.class, () -> new ShardingCreateProcedureStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java index 4c0dc55eeb85f..440fa65954d00 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java @@ -109,7 +109,8 @@ private void assertPreValidateCreateTable(final CreateTableStatement sqlStatemen ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("sharding_db"); when(database.getSchema(schemaName).containsTable("t_order")).thenReturn(true); - new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)); + new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class)); } @Test @@ -129,7 +130,8 @@ void assertPreValidateCreateTableIfNotExistsForPostgreSQL() { private void assertPreValidateCreateTableIfNotExists(final CreateTableStatement sqlStatement) { SQLStatementContext sqlStatementContext = new CreateTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)); + new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class)); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java index d0bc1c084ece2..a3bbbc6f2aca8 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java @@ -80,8 +80,8 @@ void setUp() { @Test void assertPreValidateCreateView() { - assertDoesNotThrow(() -> new ShardingCreateViewStatementValidator().preValidate( - shardingRule, createViewStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, mock(HintValueContext.class), Collections.emptyList(), + mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class))); } @Test @@ -89,9 +89,8 @@ void assertPreValidateCreateViewWithException() { when(shardingRule.isShardingTable(any())).thenReturn(true); when(shardingRule.isAllBindingTables(any())).thenReturn(false); ConfigurationProperties props = mock(ConfigurationProperties.class); - assertThrows(EngagedViewException.class, - () -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), - props)); + assertThrows(EngagedViewException.class, () -> new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, mock(HintValueContext.class), + Collections.emptyList(), mock(ShardingSphereDatabase.class), props)); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropIndexStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropIndexStatementValidatorTest.java index ce1c33657ffcc..8e34f1e75a2f1 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropIndexStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropIndexStatementValidatorTest.java @@ -73,8 +73,8 @@ void assertPreValidateDropIndexWhenIndexExistForPostgreSQL() { when(database.getSchema("public").getTable("t_order")).thenReturn(table); when(table.containsIndex("t_order_index")).thenReturn(true); when(table.containsIndex("t_order_index_new")).thenReturn(true); - assertDoesNotThrow(() -> new ShardingDropIndexStatementValidator().preValidate( - shardingRule, new DropIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingDropIndexStatementValidator().preValidate(shardingRule, new DropIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -86,9 +86,8 @@ void assertPreValidateDropIndexWhenIndexNotExistForPostgreSQL() { when(database.getSchema("public").getAllTableNames()).thenReturn(Collections.singletonList("t_order")); when(database.getSchema("public").getTable("t_order")).thenReturn(table); when(table.containsIndex("t_order_index")).thenReturn(false); - assertThrows(IndexNotExistedException.class, - () -> new ShardingDropIndexStatementValidator().preValidate( - shardingRule, new DropIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(IndexNotExistedException.class, () -> new ShardingDropIndexStatementValidator().preValidate(shardingRule, new DropIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java index 61b9f41c47056..11487bc4fa3ac 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java @@ -87,7 +87,7 @@ void assertPreValidateDropTableForMySQL() { when(database.getName()).thenReturn("db_schema"); when(database.getSchema("db_schema").containsTable("t_order_item")).thenReturn(true); ShardingDropTableStatementValidator validator = new ShardingDropTableStatementValidator(); - validator.preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)); + validator.preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)); Collection routeUnits = new LinkedList<>(); RouteMapper dataSourceMapper = new RouteMapper("db", "db1"); Collection tableMapper = new LinkedList<>(); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingRenameTableStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingRenameTableStatementValidatorTest.java index 1ce800f7e951d..3fb3693b02b27 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingRenameTableStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingRenameTableStatementValidatorTest.java @@ -62,14 +62,16 @@ void assertPreValidateShardingTable() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); when(shardingRule.containsShardingTable(argThat(tableNames -> tableNames.contains("t_order") || tableNames.contains("t_user_order")))).thenReturn(true); assertThrows(UnsupportedShardingOperationException.class, - () -> new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + () -> new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } @Test void assertPreValidateNormalCase() { SQLStatementContext sqlStatementContext = createRenameTableStatementContext("t_not_sharding_table", "t_not_sharding_table_new"); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); - assertDoesNotThrow(() -> new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingRenameTableStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingCopyStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingCopyStatementValidatorTest.java index de487b56b5d85..e3eb004011c40 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingCopyStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingCopyStatementValidatorTest.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.dml.CopyStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException; import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingCopyStatementValidator; @@ -56,16 +57,16 @@ class ShardingCopyStatementValidatorTest { void assertPreValidateWhenTableSegmentForPostgreSQL() { PostgreSQLCopyStatement sqlStatement = new PostgreSQLCopyStatement(); sqlStatement.setTableSegment(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); - assertDoesNotThrow(() -> new ShardingCopyStatementValidator().preValidate( - shardingRule, new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingCopyStatementValidator().preValidate(shardingRule, new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test void assertPreValidateWhenTableSegmentForOpenGauss() { OpenGaussCopyStatement sqlStatement = new OpenGaussCopyStatement(); sqlStatement.setTableSegment(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); - assertDoesNotThrow(() -> new ShardingCopyStatementValidator().preValidate( - shardingRule, new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingCopyStatementValidator().preValidate(shardingRule, new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -83,7 +84,7 @@ private void assertPreValidateCopyTable(final CopyStatement sqlStatement) { CopyStatementContext sqlStatementContext = new CopyStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); String tableName = "t_order"; when(shardingRule.isShardingTable(tableName)).thenReturn(true); - new ShardingCopyStatementValidator().preValidate( - shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class)); + new ShardingCopyStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), mock(ShardingSphereDatabase.class), + mock(ConfigurationProperties.class)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDeleteStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDeleteStatementValidatorTest.java index 72e70009918d7..e9ae60140699d 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDeleteStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDeleteStatementValidatorTest.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.exception.syntax.DMLWithMultipleShardingTablesException; import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingDeleteStatementValidator; @@ -90,6 +91,6 @@ private void assertPreValidateWhenDeleteMultiTables(final DeleteStatement sqlSta when(shardingRule.containsShardingTable(tableNames)).thenReturn(true); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); DeleteStatementContext sqlStatementContext = new DeleteStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME); - new ShardingDeleteStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class)); + new ShardingDeleteStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingInsertStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingInsertStatementValidatorTest.java index 4ecd7136d50e4..b01de0bb5acdf 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingInsertStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingInsertStatementValidatorTest.java @@ -100,8 +100,8 @@ void assertPreValidateWhenInsertMultiTables() { Collection tableNames = sqlStatementContext.getTablesContext().getTableNames(); when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false); when(shardingRule.containsShardingTable(tableNames)).thenReturn(true); - assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, - sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext, + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } private InsertStatementContext createInsertStatementContext(final List params, final InsertStatement insertStatement) { @@ -117,8 +117,8 @@ void assertPreValidateWhenInsertSelectWithoutKeyGenerateColumn() { when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false); InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement()); sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames()); - assertThrows(MissingGenerateKeyColumnWithInsertSelectException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, - sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(MissingGenerateKeyColumnWithInsertSelectException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext, + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -127,8 +127,8 @@ void assertPreValidateWhenInsertSelectWithKeyGenerateColumn() { when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true); InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement()); sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames()); - assertDoesNotThrow(() -> new ShardingInsertStatementValidator(shardingConditions).preValidate( - shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), + database, mock(ConfigurationProperties.class))); } @Test @@ -140,8 +140,8 @@ void assertPreValidateWhenInsertSelectWithoutBindingTables() { when(shardingRule.containsShardingTable(multiTablesContext.getTableNames())).thenReturn(true); InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement()); sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames()); - assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate( - shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext, + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -151,8 +151,8 @@ void assertPreValidateWhenInsertSelectWithBindingTables() { TablesContext multiTablesContext = createMultiTablesContext(); InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement()); sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames()); - assertDoesNotThrow(() -> new ShardingInsertStatementValidator(shardingConditions).preValidate( - shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingInsertStatementValidator(shardingConditions).preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), + database, mock(ConfigurationProperties.class))); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingLoadDataStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingLoadDataStatementValidatorTest.java index 5308375efe72e..f28ca0a172a99 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingLoadDataStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingLoadDataStatementValidatorTest.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.dml.LoadDataStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException; import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingLoadDataStatementValidator; @@ -36,8 +37,8 @@ import java.util.Collections; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -53,8 +54,8 @@ class ShardingLoadDataStatementValidatorTest { @Test void assertPreValidateLoadDataWithSingleTable() { MySQLLoadDataStatement sqlStatement = new MySQLLoadDataStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); - assertDoesNotThrow(() -> new ShardingLoadDataStatementValidator().preValidate( - shardingRule, new LoadDataStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingLoadDataStatementValidator().preValidate(shardingRule, new LoadDataStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + mock(HintValueContext.class), Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -62,7 +63,7 @@ void assertPreValidateLoadDataWithShardingTable() { MySQLLoadDataStatement sqlStatement = new MySQLLoadDataStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); when(shardingRule.isShardingTable("t_order")).thenReturn(true); assertThrows(UnsupportedShardingOperationException.class, - () -> new ShardingLoadDataStatementValidator().preValidate(shardingRule, new LoadDataStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + () -> new ShardingLoadDataStatementValidator().preValidate(shardingRule, new LoadDataStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingLoadXMLStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingLoadXMLStatementValidatorTest.java index 73ca762963fbc..d66f1930aae7c 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingLoadXMLStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingLoadXMLStatementValidatorTest.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.dml.LoadXMLStatementContext; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.exception.syntax.UnsupportedShardingOperationException; import org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingLoadXMLStatementValidator; @@ -53,8 +54,8 @@ class ShardingLoadXMLStatementValidatorTest { @Test void assertPreValidateLoadXMLWithSingleTable() { MySQLLoadXMLStatement sqlStatement = new MySQLLoadXMLStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); - assertDoesNotThrow(() -> new ShardingLoadXMLStatementValidator().preValidate( - shardingRule, new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingLoadXMLStatementValidator().preValidate(shardingRule, new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test @@ -62,7 +63,7 @@ void assertPreValidateLoadXMLWithShardingTable() { MySQLLoadXMLStatement sqlStatement = new MySQLLoadXMLStatement(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); when(shardingRule.isShardingTable("t_order")).thenReturn(true); assertThrows(UnsupportedShardingOperationException.class, - () -> new ShardingLoadXMLStatementValidator().preValidate(shardingRule, new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), + () -> new ShardingLoadXMLStatementValidator().preValidate(shardingRule, new LoadXMLStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME), mock(HintValueContext.class), Collections.emptyList(), mock(ShardingSphereDatabase.class), mock(ConfigurationProperties.class))); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingUpdateStatementValidatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingUpdateStatementValidatorTest.java index 089cd25bd54cb..2febca8d1c2a8 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingUpdateStatementValidatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingUpdateStatementValidatorTest.java @@ -80,8 +80,8 @@ void assertPreValidateWhenUpdateSingleTable() { Collection tableNames = sqlStatementContext.getTablesContext().getTableNames(); when(shardingRule.isAllShardingTables(tableNames)).thenReturn(true); when(shardingRule.containsShardingTable(tableNames)).thenReturn(true); - assertDoesNotThrow(() -> new ShardingUpdateStatementValidator().preValidate( - shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertDoesNotThrow(() -> new ShardingUpdateStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), Collections.emptyList(), database, + mock(ConfigurationProperties.class))); } @Test @@ -95,8 +95,8 @@ void assertPreValidateWhenUpdateMultipleTables() { Collection tableNames = sqlStatementContext.getTablesContext().getTableNames(); when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false); when(shardingRule.containsShardingTable(tableNames)).thenReturn(true); - assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingUpdateStatementValidator().preValidate( - shardingRule, sqlStatementContext, Collections.emptyList(), database, mock(ConfigurationProperties.class))); + assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingUpdateStatementValidator().preValidate(shardingRule, sqlStatementContext, mock(HintValueContext.class), + Collections.emptyList(), database, mock(ConfigurationProperties.class))); } @Test diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/HintValueContext.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/HintValueContext.java index 3c3cd080ed32f..3c50e4a08e706 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/HintValueContext.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/HintValueContext.java @@ -48,6 +48,8 @@ public final class HintValueContext { private boolean skipSQLRewrite; + private boolean skipMetadataValidate; + private boolean shadow; /** diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintPropertiesKey.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintPropertiesKey.java index 0e26b684f13bc..8748166889647 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintPropertiesKey.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintPropertiesKey.java @@ -43,6 +43,11 @@ public enum SQLHintPropertiesKey implements TypedPropertyKey { */ SKIP_SQL_REWRITE_KEY("SKIP_SQL_REWRITE", "skipSQLRewrite", String.valueOf(Boolean.FALSE), boolean.class), + /** + * Whether hint skip metadata validate or not. + */ + SKIP_METADATA_VALIDATE_KEY("SKIP_METADATA_VALIDATE", "skipMetadataValidate", String.valueOf(Boolean.FALSE), boolean.class), + /** * Hint disable audit names. */ diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java index ca6abe5a19b74..5286a68fffa1e 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java @@ -72,6 +72,9 @@ public static HintValueContext extractHint(final String sql) { if (containsHintKey(hintKeyValues, SQLHintPropertiesKey.SKIP_SQL_REWRITE_KEY)) { result.setSkipSQLRewrite(Boolean.parseBoolean(getHintValue(hintKeyValues, SQLHintPropertiesKey.SKIP_SQL_REWRITE_KEY))); } + if (containsHintKey(hintKeyValues, SQLHintPropertiesKey.SKIP_METADATA_VALIDATE_KEY)) { + result.setSkipMetadataValidate(Boolean.parseBoolean(getHintValue(hintKeyValues, SQLHintPropertiesKey.SKIP_METADATA_VALIDATE_KEY))); + } if (containsHintKey(hintKeyValues, SQLHintPropertiesKey.DISABLE_AUDIT_NAMES_KEY)) { String property = getHintValue(hintKeyValues, SQLHintPropertiesKey.DISABLE_AUDIT_NAMES_KEY); result.getDisableAuditNames().addAll(getSplitterSQLHintValue(property)); diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java index 0adba0a2c9029..a888a64e262ff 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java @@ -48,6 +48,12 @@ void assertSQLHintSkipSQLRewrite() { assertTrue(actual.isSkipSQLRewrite()); } + @Test + void assertSQLHintSkipMetadataValidate() { + HintValueContext actual = SQLHintUtils.extractHint("/* SHARDINGSPHERE_HINT: SKIP_METADATA_VALIDATE=true */"); + assertTrue(actual.isSkipMetadataValidate()); + } + @Test void assertSQLHintDisableAuditNames() { HintValueContext actual = SQLHintUtils.extractHint("/* SHARDINGSPHERE_HINT: DISABLE_AUDIT_NAMES=sharding_audit1 sharding_audit2 */"); diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java index 786b28ca5190e..99382c970e5b7 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java @@ -56,7 +56,7 @@ public RouteContext createRouteContext(final QueryContext queryContext, final Ru SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); SingleMetaDataValidatorFactory.newInstance(sqlStatementContext.getSqlStatement()).ifPresent(optional -> optional.validate(rule, sqlStatementContext, database)); Collection singleTables = getSingleTables(database, rule, result, sqlStatementContext); - SingleRouteEngineFactory.newInstance(singleTables, sqlStatementContext.getSqlStatement()).ifPresent(optional -> optional.route(result, rule)); + SingleRouteEngineFactory.newInstance(singleTables, sqlStatementContext.getSqlStatement(), queryContext.getHintValueContext()).ifPresent(optional -> optional.route(result, rule)); return result; } @@ -70,7 +70,7 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon final SingleRule rule, final ConfigurationProperties props) { SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); Collection singleTables = getSingleTables(database, rule, routeContext, sqlStatementContext); - SingleRouteEngineFactory.newInstance(singleTables, sqlStatementContext.getSqlStatement()).ifPresent(optional -> optional.route(routeContext, rule)); + SingleRouteEngineFactory.newInstance(singleTables, sqlStatementContext.getSqlStatement(), queryContext.getHintValueContext()).ifPresent(optional -> optional.route(routeContext, rule)); } private RouteContext createSingleDataSourceRouteContext(final SingleRule rule, final ShardingSphereDatabase database, final QueryContext queryContext) { diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactory.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactory.java index 5638bb5845e55..b230ac6010c09 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactory.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactory.java @@ -19,6 +19,7 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable; import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterSchemaStatement; @@ -39,11 +40,12 @@ public final class SingleRouteEngineFactory { * * @param singleTables single tables * @param sqlStatement SQL statement + * @param hintValueContext hint value context * @return created instance */ - public static Optional newInstance(final Collection singleTables, final SQLStatement sqlStatement) { + public static Optional newInstance(final Collection singleTables, final SQLStatement sqlStatement, final HintValueContext hintValueContext) { if (!singleTables.isEmpty()) { - return Optional.of(new SingleStandardRouteEngine(singleTables, sqlStatement)); + return Optional.of(new SingleStandardRouteEngine(singleTables, sqlStatement, hintValueContext)); } // TODO move this logic to common route logic if (isSchemaDDLStatement(sqlStatement)) { diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java index b43d53040c178..774718189c188 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException; import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; @@ -53,6 +54,8 @@ public final class SingleStandardRouteEngine implements SingleRouteEngine { private final SQLStatement sqlStatement; + private final HintValueContext hintValueContext; + @Override public void route(final RouteContext routeContext, final SingleRule singleRule) { if (routeContext.getRouteUnits().isEmpty() || sqlStatement instanceof SelectStatement) { @@ -90,7 +93,7 @@ private void routeDDLStatement(final RouteContext routeContext, final SingleRule if (dataNode.isPresent() && containsIfNotExists) { String dataSourceName = dataNode.map(DataNode::getDataSourceName).orElse(null); routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), Collections.singleton(new RouteMapper(table.getTableName(), table.getTableName())))); - } else if (dataNode.isPresent()) { + } else if (dataNode.isPresent() && !hintValueContext.isSkipMetadataValidate()) { throw new TableExistsException(table.getTableName()); } else { String dataSourceName = rule.assignNewDataSourceName(); diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactoryTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactoryTest.java index f530e6f368068..c04b8448c1afa 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactoryTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleRouteEngineFactoryTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.single.route.engine; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable; import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterSchemaStatement; @@ -34,26 +35,26 @@ class SingleRouteEngineFactoryTest { @Test void assertNewInstanceWithNotEmptySingleTables() { - assertTrue(SingleRouteEngineFactory.newInstance(Collections.singleton(new QualifiedTable("demo_ds", "t_order")), mock(SQLStatement.class)).isPresent()); + assertTrue(SingleRouteEngineFactory.newInstance(Collections.singleton(new QualifiedTable("demo_ds", "t_order")), mock(SQLStatement.class), mock(HintValueContext.class)).isPresent()); } @Test void assertNewInstanceWithEmptySingleTableNameAndCreateSchemaStatement() { - assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(CreateSchemaStatement.class)).isPresent()); + assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(CreateSchemaStatement.class), mock(HintValueContext.class)).isPresent()); } @Test void assertNewInstanceWithEmptySingleTableNameAndAlterSchemaStatement() { - assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(AlterSchemaStatement.class)).isPresent()); + assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(AlterSchemaStatement.class), mock(HintValueContext.class)).isPresent()); } @Test void assertNewInstanceWithEmptySingleTableNameAndDropSchemaStatement() { - assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(DropSchemaStatement.class)).isPresent()); + assertTrue(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(DropSchemaStatement.class), mock(HintValueContext.class)).isPresent()); } @Test void assertNewInstanceWithEmptySingleTableNameAndOtherStatement() { - assertFalse(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(SQLStatement.class)).isPresent()); + assertFalse(SingleRouteEngineFactory.newInstance(Collections.emptyList(), mock(SQLStatement.class), mock(HintValueContext.class)).isPresent()); } } diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngineTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngineTest.java index 819576ed17c83..c7d5669859cab 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngineTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngineTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.datanode.DataNode; +import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; @@ -63,7 +64,7 @@ class SingleStandardRouteEngineTest { @Test void assertRouteInSameDataSource() throws SQLException { - SingleStandardRouteEngine engine = new SingleStandardRouteEngine(mockQualifiedTables(), null); + SingleStandardRouteEngine engine = new SingleStandardRouteEngine(mockQualifiedTables(), null, mock(HintValueContext.class)); SingleRule singleRule = new SingleRule(new SingleRuleConfiguration(), DefaultDatabase.LOGIC_NAME, new MySQLDatabaseType(), createDataSourceMap(), Collections.emptyList()); singleRule.getAttributes().getAttribute(DataNodeRuleAttribute.class).getAllDataNodes().put("t_order", Collections.singleton(mockDataNode("t_order"))); singleRule.getAttributes().getAttribute(DataNodeRuleAttribute.class).getAllDataNodes().put("t_order_item", Collections.singleton(mockDataNode("t_order_item"))); @@ -94,7 +95,7 @@ private Collection mockQualifiedTables() { @Test void assertRouteWithoutSingleRule() throws SQLException { - SingleStandardRouteEngine engine = new SingleStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement(false)); + SingleStandardRouteEngine engine = new SingleStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement(false), mock(HintValueContext.class)); SingleRule singleRule = new SingleRule(new SingleRuleConfiguration(), DefaultDatabase.LOGIC_NAME, new MySQLDatabaseType(), createDataSourceMap(), Collections.emptyList()); RouteContext routeContext = new RouteContext(); engine.route(routeContext, singleRule); @@ -109,7 +110,7 @@ void assertRouteWithoutSingleRule() throws SQLException { @Test void assertRouteWithDefaultSingleRule() throws SQLException { - SingleStandardRouteEngine engine = new SingleStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement(false)); + SingleStandardRouteEngine engine = new SingleStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement(false), mock(HintValueContext.class)); SingleRule singleRule = new SingleRule(new SingleRuleConfiguration(Collections.emptyList(), "ds_0"), DefaultDatabase.LOGIC_NAME, new MySQLDatabaseType(), createDataSourceMap(), Collections.emptyList()); RouteContext routeContext = new RouteContext(); @@ -135,13 +136,15 @@ private Map createDataSourceMap() throws SQLException { @Test void assertRouteDuplicateSingleTable() { - SingleStandardRouteEngine engine = new SingleStandardRouteEngine(Collections.singleton(new QualifiedTable(DefaultDatabase.LOGIC_NAME, "t_order")), mockStatement(false)); + SingleStandardRouteEngine engine = + new SingleStandardRouteEngine(Collections.singleton(new QualifiedTable(DefaultDatabase.LOGIC_NAME, "t_order")), mockStatement(false), mock(HintValueContext.class)); assertThrows(TableExistsException.class, () -> engine.route(new RouteContext(), mockSingleRule())); } @Test void assertRouteIfNotExistsDuplicateSingleTable() { - SingleStandardRouteEngine engine = new SingleStandardRouteEngine(Collections.singleton(new QualifiedTable(DefaultDatabase.LOGIC_NAME, "t_order")), mockStatement(true)); + SingleStandardRouteEngine engine = + new SingleStandardRouteEngine(Collections.singleton(new QualifiedTable(DefaultDatabase.LOGIC_NAME, "t_order")), mockStatement(true), mock(HintValueContext.class)); assertDoesNotThrow(() -> engine.route(new RouteContext(), mockSingleRule())); } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/PreviewExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/PreviewExecutor.java index 3e28fd16b4e0c..e92f342b8d7f4 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/PreviewExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/PreviewExecutor.java @@ -90,6 +90,7 @@ public Collection getRows(final PreviewStatement sqlSta String toBePreviewedSQL = sqlStatement.getSql(); SQLStatement toBePreviewedStatement = metaData.getGlobalRuleMetaData().getSingleRule(SQLParserRule.class).getSQLParserEngine(database.getProtocolType()).parse(toBePreviewedSQL, false); HintValueContext hintValueContext = connectionContext.getQueryContext().getHintValueContext(); + hintValueContext.setSkipMetadataValidate(true); SQLStatementContext toBePreviewedStatementContext = new SQLBindEngine(metaData, database.getName(), hintValueContext).bind(toBePreviewedStatement, Collections.emptyList()); QueryContext queryContext = new QueryContext(toBePreviewedStatementContext, toBePreviewedSQL, Collections.emptyList(), hintValueContext, connectionContext.getQueryContext().getConnectionContext(), metaData); From 44f4b02f647d786e1da4af5f62f40612bd4ec69b Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 8 Oct 2024 18:40:23 +0800 Subject: [PATCH 2/3] Update release note --- RELEASE-NOTES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 9bb5224019bb3..0d570ccdebf6c 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -19,6 +19,8 @@ 1. Sharding: Fix alter view exception when config sharding rule and binding table rule - [#32696](https://github.com/apache/shardingsphere/issues/32696) 1. Shadow: Use hintValueContext to replace extract sql hint from sql statement for solving shadow sql hint bug - [#33063](https://github.com/apache/shardingsphere/pull/33063) 1. Shadow: Make Shadow feature available again in GraalVM Native Image - [#33080](https://github.com/apache/shardingsphere/pull/33080) +1. DistSQL & Kernel: Fix table exist exception when execute preview create table statement with exist table - [#33171](https://github.com/apache/shardingsphere/pull/33171) + ### Change Log From 3917d2ea0f2a6cce642b6400b1877d07f785385e Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Wed, 9 Oct 2024 09:33:36 +0800 Subject: [PATCH 3/3] refactor SingleStandardRouteEngine --- .../route/engine/SingleStandardRouteEngine.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java index 774718189c188..e8026039b8173 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java @@ -90,11 +90,8 @@ private void routeDDLStatement(final RouteContext routeContext, final SingleRule QualifiedTable table = singleTables.iterator().next(); Optional dataNode = rule.getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).findTableDataNode(table.getSchemaName(), table.getTableName()); boolean containsIfNotExists = ((CreateTableStatement) sqlStatement).isIfNotExists(); - if (dataNode.isPresent() && containsIfNotExists) { - String dataSourceName = dataNode.map(DataNode::getDataSourceName).orElse(null); - routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), Collections.singleton(new RouteMapper(table.getTableName(), table.getTableName())))); - } else if (dataNode.isPresent() && !hintValueContext.isSkipMetadataValidate()) { - throw new TableExistsException(table.getTableName()); + if (dataNode.isPresent()) { + routeDDLStatementWithExistTable(routeContext, containsIfNotExists, dataNode.get(), table); } else { String dataSourceName = rule.assignNewDataSourceName(); routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), Collections.singleton(new RouteMapper(table.getTableName(), table.getTableName())))); @@ -104,6 +101,16 @@ private void routeDDLStatement(final RouteContext routeContext, final SingleRule } } + private void routeDDLStatementWithExistTable(final RouteContext routeContext, final boolean containsIfNotExists, final DataNode dataNode, final QualifiedTable table) { + if (containsIfNotExists || hintValueContext.isSkipMetadataValidate()) { + String dataSourceName = dataNode.getDataSourceName(); + routeContext.getRouteUnits() + .add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), Collections.singleton(new RouteMapper(table.getTableName(), table.getTableName())))); + } else { + throw new TableExistsException(table.getTableName()); + } + } + private void fillRouteContext(final SingleRule singleRule, final RouteContext routeContext, final Collection logicTables) { for (QualifiedTable each : logicTables) { String tableName = each.getTableName();