From 5098f5e3986181810315667f680a0f5540f451e9 Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 17 Dec 2024 18:18:57 +0800 Subject: [PATCH] fix unit test --- .../from/type/SimpleTableSegmentBinder.java | 43 +++++++++++++++---- .../test/resources/cases/ddl/create-table.xml | 38 ++++++++-------- .../test/resources/sqls/ddl/create-table.xml | 2 +- .../ddl/create/create-table.xml | 8 ++-- .../encrypt/config/query-with-cipher.yaml | 26 +++++++++++ 5 files changed, 84 insertions(+), 33 deletions(-) diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/from/type/SimpleTableSegmentBinder.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/from/type/SimpleTableSegmentBinder.java index a6b4c5c1cf6c6..7c3e9c00a6874 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/from/type/SimpleTableSegmentBinder.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/from/type/SimpleTableSegmentBinder.java @@ -32,10 +32,14 @@ import org.apache.shardingsphere.infra.database.postgresql.type.PostgreSQLDatabaseType; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.NoDatabaseSelectedException; +import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException; +import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.TableExistsException; +import org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException; import org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException; import org.apache.shardingsphere.infra.metadata.database.schema.manager.SystemSchemaManager; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.column.ColumnDefinitionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment; @@ -72,11 +76,10 @@ public static SimpleTableSegment bind(final SimpleTableSegment segment, final SQ final Multimap tableBinderContexts) { fillPivotColumnNamesInBinderContext(segment, binderContext); IdentifierValue databaseName = getDatabaseName(segment, binderContext); - ShardingSpherePreconditions.checkNotNull(databaseName.getValue(), NoDatabaseSelectedException::new); - IdentifierValue schemaName = getSchemaName(segment, binderContext); + IdentifierValue schemaName = getSchemaName(segment, binderContext, databaseName); IdentifierValue tableName = segment.getTableName().getIdentifier(); - checkTableExists(binderContext, databaseName.getValue(), schemaName.getValue(), tableName.getValue()); ShardingSphereSchema schema = binderContext.getMetaData().getDatabase(databaseName.getValue()).getSchema(schemaName.getValue()); + checkTableExists(binderContext, schema, schemaName.getValue(), tableName.getValue()); tableBinderContexts.put(new CaseInsensitiveString(segment.getAliasName().orElseGet(tableName::getValue)), createSimpleTableBinderContext(segment, schema, databaseName, schemaName, binderContext)); TableNameSegment tableNameSegment = new TableNameSegment(segment.getTableName().getStartIndex(), segment.getTableName().getStopIndex(), tableName); @@ -94,7 +97,17 @@ private static void fillPivotColumnNamesInBinderContext(final SimpleTableSegment private static IdentifierValue getDatabaseName(final SimpleTableSegment segment, final SQLStatementBinderContext binderContext) { DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(binderContext.getSqlStatement().getDatabaseType()).getDialectDatabaseMetaData(); Optional owner = dialectDatabaseMetaData.getDefaultSchema().isPresent() ? segment.getOwner().flatMap(OwnerSegment::getOwner) : segment.getOwner(); - return new IdentifierValue(owner.map(optional -> optional.getIdentifier().getValue()).orElse(binderContext.getCurrentDatabaseName())); + IdentifierValue result = new IdentifierValue(owner.map(optional -> optional.getIdentifier().getValue()).orElse(binderContext.getCurrentDatabaseName())); + ShardingSpherePreconditions.checkNotNull(result.getValue(), NoDatabaseSelectedException::new); + ShardingSpherePreconditions.checkState(binderContext.getMetaData().containsDatabase(result.getValue()), () -> new UnknownDatabaseException(result.getValue())); + return result; + } + + private static IdentifierValue getSchemaName(final SimpleTableSegment segment, final SQLStatementBinderContext binderContext, final IdentifierValue databaseName) { + IdentifierValue result = getSchemaName(segment, binderContext); + ShardingSpherePreconditions.checkState(binderContext.getMetaData().getDatabase(databaseName.getValue()).containsSchema(result.getValue()), + () -> new SchemaNotFoundException(result.getValue())); + return result; } private static IdentifierValue getSchemaName(final SimpleTableSegment segment, final SQLStatementBinderContext binderContext) { @@ -110,8 +123,10 @@ private static IdentifierValue getSchemaName(final SimpleTableSegment segment, f return new IdentifierValue(new DatabaseTypeRegistry(databaseType).getDefaultSchemaName(binderContext.getCurrentDatabaseName())); } - private static void checkTableExists(final SQLStatementBinderContext binderContext, final String databaseName, final String schemaName, final String tableName) { + private static void checkTableExists(final SQLStatementBinderContext binderContext, final ShardingSphereSchema schema, final String schemaName, final String tableName) { if (binderContext.getSqlStatement() instanceof CreateTableStatement) { + CreateTableStatement sqlStatement = (CreateTableStatement) binderContext.getSqlStatement(); + ShardingSpherePreconditions.checkState(sqlStatement.isIfNotExists() || !schema.containsTable(tableName), () -> new TableExistsException(tableName)); return; } if ("DUAL".equalsIgnoreCase(tableName)) { @@ -123,10 +138,7 @@ private static void checkTableExists(final SQLStatementBinderContext binderConte if (binderContext.getExternalTableBinderContexts().containsKey(new CaseInsensitiveString(tableName))) { return; } - ShardingSpherePreconditions.checkState(binderContext.getMetaData().containsDatabase(databaseName) - && binderContext.getMetaData().getDatabase(databaseName).containsSchema(schemaName) - && binderContext.getMetaData().getDatabase(databaseName).getSchema(schemaName).containsTable(tableName), - () -> new TableNotFoundException(tableName)); + ShardingSpherePreconditions.checkState(schema.containsTable(tableName), () -> new TableNotFoundException(tableName)); } private static SimpleTableSegmentBinderContext createSimpleTableBinderContext(final SimpleTableSegment segment, final ShardingSphereSchema schema, final IdentifierValue databaseName, @@ -135,9 +147,22 @@ private static SimpleTableSegmentBinderContext createSimpleTableBinderContext(fi if (binderContext.getMetaData().getDatabase(databaseName.getValue()).getSchema(schemaName.getValue()).containsTable(tableName.getValue())) { return createSimpleTableSegmentBinderContextWithMetaData(segment, schema, databaseName, schemaName, binderContext, tableName); } + if (binderContext.getSqlStatement() instanceof CreateTableStatement) { + return new SimpleTableSegmentBinderContext(createProjectionSegments((CreateTableStatement) binderContext.getSqlStatement(), databaseName, schemaName, tableName)); + } return new SimpleTableSegmentBinderContext(Collections.emptyList()); } + private static Collection createProjectionSegments(final CreateTableStatement sqlStatement, final IdentifierValue databaseName, + final IdentifierValue schemaName, final IdentifierValue tableName) { + Collection result = new LinkedList<>(); + for (ColumnDefinitionSegment each : sqlStatement.getColumnDefinitions()) { + each.getColumnName().setColumnBoundInfo(new ColumnSegmentBoundInfo(new TableSegmentBoundInfo(databaseName, schemaName), tableName, each.getColumnName().getIdentifier())); + result.add(new ColumnProjectionSegment(each.getColumnName())); + } + return result; + } + private static SimpleTableSegmentBinderContext createSimpleTableSegmentBinderContextWithMetaData(final SimpleTableSegment segment, final ShardingSphereSchema schema, final IdentifierValue databaseName, final IdentifierValue schemaName, final SQLStatementBinderContext binderContext, final IdentifierValue tableName) { diff --git a/test/it/binder/src/test/resources/cases/ddl/create-table.xml b/test/it/binder/src/test/resources/cases/ddl/create-table.xml index f543396e85e38..59815b36015b0 100644 --- a/test/it/binder/src/test/resources/cases/ddl/create-table.xml +++ b/test/it/binder/src/test/resources/cases/ddl/create-table.xml @@ -18,69 +18,69 @@ - +
- + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + diff --git a/test/it/binder/src/test/resources/sqls/ddl/create-table.xml b/test/it/binder/src/test/resources/sqls/ddl/create-table.xml index 617bfe246aec5..35e75c68f6121 100644 --- a/test/it/binder/src/test/resources/sqls/ddl/create-table.xml +++ b/test/it/binder/src/test/resources/sqls/ddl/create-table.xml @@ -17,5 +17,5 @@ --> - + diff --git a/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/create/create-table.xml b/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/create/create-table.xml index efad1b9e6d260..363a17f208d63 100644 --- a/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/create/create-table.xml +++ b/test/it/rewriter/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/create/create-table.xml @@ -18,12 +18,12 @@ - - + + - - + + diff --git a/test/it/rewriter/src/test/resources/scenario/encrypt/config/query-with-cipher.yaml b/test/it/rewriter/src/test/resources/scenario/encrypt/config/query-with-cipher.yaml index 7a0a5660d5512..c76e7e776bce9 100644 --- a/test/it/rewriter/src/test/resources/scenario/encrypt/config/query-with-cipher.yaml +++ b/test/it/rewriter/src/test/resources/scenario/encrypt/config/query-with-cipher.yaml @@ -76,6 +76,32 @@ rules: cipher: name: cipher_amount encryptorName: rewrite_normal_fixture + t_account_bak_for_create: + columns: + certificate_number: + cipher: + name: cipher_certificate_number + encryptorName: rewrite_normal_fixture + assistedQuery: + name: assisted_query_certificate_number + encryptorName: rewrite_assisted_query_fixture + likeQuery: + name: like_query_certificate_number + encryptorName: rewrite_like_query_fixture + password: + cipher: + name: cipher_password + encryptorName: rewrite_normal_fixture + assistedQuery: + name: assisted_query_password + encryptorName: rewrite_assisted_query_fixture + likeQuery: + name: like_query_password + encryptorName: rewrite_like_query_fixture + amount: + cipher: + name: cipher_amount + encryptorName: rewrite_normal_fixture t_account_detail: columns: certificate_number: