From 258537dca34b81fcfafd0354aa26efab3bcead11 Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 17 Dec 2024 13:00:55 +0800 Subject: [PATCH 1/6] Add sql binder it and add select shorthand test case example --- test/it/binder/pom.xml | 152 ++++++++++++++ .../test/it/sql/binder/SQLBinderIT.java | 187 ++++++++++++++++++ .../it/sql/binder/SQLBinderITSettings.java | 37 ++++ .../src/main/resources/case/dml/select.xml | 90 +++++++++ .../resources/sql/supported/dml/select.xml | 21 ++ .../it/sql/binder/it/mysql/MySQLBinderIT.java | 25 +++ .../segment/projection/ProjectionAssert.java | 36 +++- .../asserts/segment/table/TableAssert.java | 20 +- .../impl/bound/ExpectedColumnBoundInfo.java | 48 +++++ .../impl/bound/ExpectedTableBoundInfo.java | 40 ++++ .../bound/column/ExpectedOriginalColumn.java | 26 +++ .../bound/column/ExpectedOriginalTable.java | 26 +++ .../bound/table/ExpectedOriginalDatabase.java | 26 +++ .../bound/table/ExpectedOriginalSchema.java | 26 +++ .../impl/column/ExpectedColumnProjection.java | 4 + .../ExpectedShorthandProjection.java | 4 + .../impl/table/ExpectedSimpleTable.java | 4 + test/it/pom.xml | 1 + 18 files changed, 760 insertions(+), 13 deletions(-) create mode 100644 test/it/binder/pom.xml create mode 100644 test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java create mode 100644 test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java create mode 100644 test/it/binder/src/main/resources/case/dml/select.xml create mode 100644 test/it/binder/src/main/resources/sql/supported/dml/select.xml create mode 100644 test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/it/mysql/MySQLBinderIT.java create mode 100644 test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/ExpectedColumnBoundInfo.java create mode 100644 test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/ExpectedTableBoundInfo.java create mode 100644 test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/column/ExpectedOriginalColumn.java create mode 100644 test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/column/ExpectedOriginalTable.java create mode 100644 test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/table/ExpectedOriginalDatabase.java create mode 100644 test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/table/ExpectedOriginalSchema.java diff --git a/test/it/binder/pom.xml b/test/it/binder/pom.xml new file mode 100644 index 0000000000000..ad118b4367d1e --- /dev/null +++ b/test/it/binder/pom.xml @@ -0,0 +1,152 @@ + + + + + 4.0.0 + + org.apache.shardingsphere + shardingsphere-test-it + 5.5.2-SNAPSHOT + + shardingsphere-test-it-binder + ${project.artifactId} + + + true + + + + + org.apache.shardingsphere + shardingsphere-test-it-parser + ${project.version} + compile + + + org.apache.shardingsphere + shardingsphere-infra-binder + ${project.version} + compile + + + + org.apache.shardingsphere + shardingsphere-parser-sql-sql92 + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-mysql + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-postgresql + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-oracle + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-sqlserver + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-opengauss + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-firebird + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-hive + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-presto + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-doris + ${project.version} + test + + + org.apache.shardingsphere + shardingsphere-parser-sql-clickhouse + ${project.version} + test + + + + org.apache.shardingsphere + shardingsphere-test-util + ${project.version} + compile + + + org.junit.jupiter + junit-jupiter-api + compile + + + org.junit.jupiter + junit-jupiter-engine + compile + + + org.junit.jupiter + junit-jupiter-params + compile + + + org.hamcrest + hamcrest + compile + + + org.mockito + mockito-core + compile + + + org.mockito + mockito-junit-jupiter + ${mockito.version} + compile + + + diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java new file mode 100644 index 0000000000000..f8b4555bfa28b --- /dev/null +++ b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.binder; + +import com.google.common.base.Preconditions; +import org.apache.shardingsphere.infra.binder.engine.SQLBindEngine; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; +import org.apache.shardingsphere.infra.hint.HintValueContext; +import org.apache.shardingsphere.infra.hint.SQLHintUtils; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; +import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.sql.parser.api.CacheOption; +import org.apache.shardingsphere.sql.parser.api.SQLParserEngine; +import org.apache.shardingsphere.sql.parser.api.SQLStatementVisitorEngine; +import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; +import org.apache.shardingsphere.test.it.sql.parser.internal.InternalSQLParserTestParameter; +import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext; +import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.SQLStatementAssert; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.SQLParserTestCases; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.SQLParserTestCase; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.registry.SQLParserTestCasesRegistry; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.SQLCases; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.registry.SQLCasesRegistry; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.type.SQLCaseType; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; + +import java.sql.Types; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.mockito.Mockito.mock; + +public abstract class SQLBinderIT { + + private static final SQLCases SQL_CASES = SQLCasesRegistry.getInstance().getCases(); + + private static final SQLParserTestCases SQL_PARSER_TEST_CASES = SQLParserTestCasesRegistry.getInstance().getCases(); + + @ParameterizedTest(name = "{0} ({1}) -> {2}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertBind(final String sqlCaseId, final SQLCaseType sqlCaseType, final String databaseType) { + String sql = SQL_CASES.getSQL(sqlCaseId, sqlCaseType, SQL_PARSER_TEST_CASES.get(sqlCaseId).getParameters()); + SQLParserTestCase expected = SQL_PARSER_TEST_CASES.get(sqlCaseId); + SQLStatement actual = bindSQLStatement("H2".equals(databaseType) ? "MySQL" : databaseType, sql, new ArrayList<>(expected.getParameters())); + SQLStatementAssert.assertIs(new SQLCaseAssertContext(sqlCaseId, sql, expected.getParameters(), sqlCaseType), actual, expected); + } + + private SQLStatement bindSQLStatement(final String databaseType, final String sql, final List parameters) { + HintValueContext hintValueContext = SQLHintUtils.extractHint(sql); + SQLStatement sqlStatement = new SQLStatementVisitorEngine(databaseType).visit(new SQLParserEngine(databaseType, new CacheOption(128, 1024L)).parse(sql, false)); + return new SQLBindEngine(mockMetaData(TypedSPILoader.getService(DatabaseType.class, databaseType)), "foo_db_1", hintValueContext).bind(sqlStatement, parameters).getSqlStatement(); + } + + private ShardingSphereMetaData mockMetaData(final DatabaseType databaseType) { + Collection databases = new LinkedList<>(); + databases.add(new ShardingSphereDatabase("foo_db_1", databaseType, mock(ResourceMetaData.class), mock(RuleMetaData.class), mockSchemas(databaseType, "foo_db_1"))); + databases.add(new ShardingSphereDatabase("foo_db_2", databaseType, mock(ResourceMetaData.class), mock(RuleMetaData.class), mockSchemas(databaseType, "foo_db_2"))); + return new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + } + + private Collection mockSchemas(final DatabaseType databaseType, final String databaseName) { + Collection result = new LinkedList<>(); + String defaultSchemaName = new DatabaseTypeRegistry(databaseType).getDefaultSchemaName(databaseName); + Collection tables = "foo_db_1".equalsIgnoreCase(databaseName) ? mockFooDB1Tables() : mockFooDB2Tables(); + result.add(new ShardingSphereSchema(defaultSchemaName, tables, Collections.emptyList())); + return result; + } + + private Collection mockFooDB1Tables() { + Collection result = new LinkedList<>(); + result.add(new ShardingSphereTable("t_order", Arrays.asList( + new ShardingSphereColumn("order_id", Types.BIGINT, true, false, false, true, false, false), + new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), + new ShardingSphereColumn("status", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("merchant_id", Types.INTEGER, false, false, false, true, false, true), + new ShardingSphereColumn("remark", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); + result.add(new ShardingSphereTable("t_order_item", Arrays.asList( + new ShardingSphereColumn("item_id", Types.BIGINT, true, false, false, true, false, false), + new ShardingSphereColumn("order_id", Types.BIGINT, false, false, false, true, false, false), + new ShardingSphereColumn("user_id", Types.INTEGER, false, false, false, true, false, false), + new ShardingSphereColumn("product_id", Types.INTEGER, false, false, false, true, false, false), + new ShardingSphereColumn("quantity", Types.INTEGER, false, false, false, true, false, false), + new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); + result.add(new ShardingSphereTable("t_user", Arrays.asList( + new ShardingSphereColumn("user_id", Types.INTEGER, true, false, false, true, false, false), + new ShardingSphereColumn("user_name", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("password", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("email", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("telephone", Types.CHAR, false, false, false, true, false, false), + new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); + result.add(new ShardingSphereTable("t_merchant", Arrays.asList( + new ShardingSphereColumn("merchant_id", Types.INTEGER, true, false, false, true, false, false), + new ShardingSphereColumn("country_id", Types.SMALLINT, false, false, false, true, false, false), + new ShardingSphereColumn("merchant_name", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("business_code", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("telephone", Types.CHAR, false, false, false, true, false, false), + new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); + return result; + } + + private Collection mockFooDB2Tables() { + Collection result = new LinkedList<>(); + result.add(new ShardingSphereTable("t_product", Arrays.asList( + new ShardingSphereColumn("product_id", Types.INTEGER, true, false, false, true, false, false), + new ShardingSphereColumn("product_name", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("category_id", Types.INTEGER, false, false, false, true, false, false), + new ShardingSphereColumn("price", Types.DECIMAL, false, false, false, true, false, true), + new ShardingSphereColumn("status", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); + result.add(new ShardingSphereTable("t_product_detail", Arrays.asList( + new ShardingSphereColumn("detail_id", Types.INTEGER, true, false, false, true, false, false), + new ShardingSphereColumn("product_id", Types.INTEGER, false, false, false, true, false, false), + new ShardingSphereColumn("description", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); + result.add(new ShardingSphereTable("t_product_category", Arrays.asList( + new ShardingSphereColumn("category_id", Types.INTEGER, true, false, false, true, false, false), + new ShardingSphereColumn("category_name", Types.VARCHAR, false, false, false, true, false, false), + new ShardingSphereColumn("parent_id", Types.INTEGER, false, false, false, true, false, false), + new ShardingSphereColumn("level", Types.TINYINT, false, false, false, true, false, false), + new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); + result.add(new ShardingSphereTable("t_country", Arrays.asList( + new ShardingSphereColumn("country_id", Types.SMALLINT, true, false, false, true, false, false), + new ShardingSphereColumn("country_name", Types.VARCHAR, false, false, false, true, false, true), + new ShardingSphereColumn("continent_name", Types.VARCHAR, false, false, false, true, false, true), + new ShardingSphereColumn("creation_date", Types.DATE, false, false, false, true, false, false)), Collections.emptyList(), Collections.emptyList())); + return result; + } + + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + SQLBinderITSettings settings = extensionContext.getRequiredTestClass().getAnnotation(SQLBinderITSettings.class); + Preconditions.checkNotNull(settings, "Annotation InternalSQLParserITSettings is required."); + return getTestParameters(settings.value()).stream(); + } + + private Collection getTestParameters(final String... databaseTypes) { + Collection result = new LinkedList<>(); + for (InternalSQLParserTestParameter each : SQL_CASES.generateTestParameters(Arrays.stream(databaseTypes).collect(Collectors.toSet()))) { + if (!isPlaceholderWithoutParameter(each)) { + result.add(Arguments.arguments(each.getSqlCaseId(), each.getSqlCaseType(), each.getDatabaseType())); + } + } + return result; + } + + private boolean isPlaceholderWithoutParameter(final InternalSQLParserTestParameter testParam) { + return SQLCaseType.PLACEHOLDER == testParam.getSqlCaseType() && SQL_PARSER_TEST_CASES.get(testParam.getSqlCaseId()).getParameters().isEmpty(); + } + } +} diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java new file mode 100644 index 0000000000000..c2bbd671fb3c1 --- /dev/null +++ b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.binder; + +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Internal SQL parser integrate test settings. + */ +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface SQLBinderITSettings { + + /** + * Get to be tested database types. + * + * @return to be tested database types + */ + String[] value(); +} diff --git a/test/it/binder/src/main/resources/case/dml/select.xml b/test/it/binder/src/main/resources/case/dml/select.xml new file mode 100644 index 0000000000000..7f03799f5f7c1 --- /dev/null +++ b/test/it/binder/src/main/resources/case/dml/select.xml @@ -0,0 +1,90 @@ + + + + + + diff --git a/test/it/binder/src/main/resources/sql/supported/dml/select.xml b/test/it/binder/src/main/resources/sql/supported/dml/select.xml new file mode 100644 index 0000000000000..aa09790e7ad17 --- /dev/null +++ b/test/it/binder/src/main/resources/sql/supported/dml/select.xml @@ -0,0 +1,21 @@ + + + + + + diff --git a/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/it/mysql/MySQLBinderIT.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/it/mysql/MySQLBinderIT.java new file mode 100644 index 0000000000000..54340c747caf0 --- /dev/null +++ b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/it/mysql/MySQLBinderIT.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.binder.it.mysql; + +import org.apache.shardingsphere.test.it.sql.binder.SQLBinderIT; +import org.apache.shardingsphere.test.it.sql.binder.SQLBinderITSettings; + +@SQLBinderITSettings({"MySQL", "H2"}) +class MySQLBinderIT extends SQLBinderIT { +} diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/projection/ProjectionAssert.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/projection/ProjectionAssert.java index ecaa805fd2c94..a4030d552351a 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/projection/ProjectionAssert.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/projection/ProjectionAssert.java @@ -48,6 +48,7 @@ import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.projection.impl.top.ExpectedTopProjection; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.type.SQLCaseType; +import java.util.ArrayList; import java.util.List; import static org.hamcrest.CoreMatchers.instanceOf; @@ -128,15 +129,26 @@ private static void assertShorthandProjection(final SQLCaseAssertContext assertC assertTrue(actual.getOwner().isPresent(), assertContext.getText("Actual owner should exist.")); OwnerAssert.assertIs(assertContext, actual.getOwner().get(), expected.getOwner()); } + assertActualProjections(assertContext, actual, expected); } - private static void assertColumnProjection(final SQLCaseAssertContext assertContext, final ColumnProjectionSegment actual, final ExpectedColumnProjection expected) { - assertThat(assertContext.getText("Column projection alias assertion error: "), actual.getAliasName().orElse(null), is(expected.getAlias())); - if (null != actual.getColumn().getNestedObjectAttributes()) { - assertThat(assertContext.getText("Nested Object attributes assertion error: "), actual.getColumn().getExpression(), is(expected.getName())); + private static void assertActualProjections(final SQLCaseAssertContext assertContext, final ShorthandProjectionSegment actual, final ExpectedShorthandProjection expected) { + if (null == expected.getActualProjections()) { + assertTrue(actual.getActualProjectionSegments().isEmpty(), assertContext.getText("Actual projections should not exist.")); } else { - IdentifierValueAssert.assertIs(assertContext, actual.getColumn().getIdentifier(), expected, "Column projection"); + assertFalse(actual.getActualProjectionSegments().isEmpty(), assertContext.getText("Actual projections should exist.")); + assertThat(assertContext.getText("Actual projections size assertion error:"), actual.getActualProjectionSegments().size(), is(expected.getActualProjections().getSize())); + List actualProjectionSegments = new ArrayList<>(actual.getActualProjectionSegments()); + int index = 0; + for (ExpectedProjection each : expected.getActualProjections().getExpectedProjections()) { + assertProjection(assertContext, actualProjectionSegments.get(index++), each); + } } + } + + private static void assertColumnProjection(final SQLCaseAssertContext assertContext, final ColumnProjectionSegment actual, final ExpectedColumnProjection expected) { + assertThat(assertContext.getText("Column projection alias assertion error: "), actual.getAliasName().orElse(null), is(expected.getAlias())); + assertColumnSegment(assertContext, actual, expected); assertLeftParentheses(assertContext, actual, expected); assertRightParentheses(assertContext, actual, expected); if (null == expected.getOwner()) { @@ -147,6 +159,20 @@ private static void assertColumnProjection(final SQLCaseAssertContext assertCont } } + private static void assertColumnSegment(final SQLCaseAssertContext assertContext, final ColumnProjectionSegment actual, final ExpectedColumnProjection expected) { + if (null != actual.getColumn().getNestedObjectAttributes()) { + assertThat(assertContext.getText("Nested Object attributes assertion error: "), actual.getColumn().getExpression(), is(expected.getName())); + } else { + IdentifierValueAssert.assertIs(assertContext, actual.getColumn().getIdentifier(), expected, "Column projection"); + if (null != expected.getColumnBound()) { + IdentifierValueAssert.assertIs(assertContext, actual.getColumn().getColumnBoundInfo().getOriginalDatabase(), expected.getColumnBound().getOriginalDatabase(), "Bound Database"); + IdentifierValueAssert.assertIs(assertContext, actual.getColumn().getColumnBoundInfo().getOriginalSchema(), expected.getColumnBound().getOriginalSchema(), "Bound Schema"); + IdentifierValueAssert.assertIs(assertContext, actual.getColumn().getColumnBoundInfo().getOriginalTable(), expected.getColumnBound().getOriginalTable(), "Bound Table"); + IdentifierValueAssert.assertIs(assertContext, actual.getColumn().getColumnBoundInfo().getOriginalColumn(), expected.getColumnBound().getOriginalColumn(), "Bound Column"); + } + } + } + private static void assertLeftParentheses(final SQLCaseAssertContext assertContext, final ColumnProjectionSegment actual, final ExpectedColumnProjection expected) { if (null == expected.getLeftParentheses()) { assertFalse(actual.getColumn().getLeftParentheses().isPresent(), assertContext.getText("Actual left parentheses should not exist.")); diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/table/TableAssert.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/table/TableAssert.java index 6285f307889ff..a13e50f21a0d6 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/table/TableAssert.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/table/TableAssert.java @@ -121,7 +121,7 @@ private static void assertIs(final SQLCaseAssertContext assertContext, final Fun * @param expected expected table */ public static void assertIs(final SQLCaseAssertContext assertContext, final SimpleTableSegment actual, final ExpectedSimpleTable expected) { - IdentifierValueAssert.assertIs(assertContext, actual.getTableName().getIdentifier(), expected, "Table"); + assertTableNameSegment(assertContext, actual, expected); assertThat(assertContext.getText("Table alias assertion error: "), actual.getAliasName().orElse(null), is(expected.getAlias())); if (null == expected.getOwner()) { assertFalse(actual.getOwner().isPresent(), assertContext.getText("Actual owner should not exist.")); @@ -237,13 +237,6 @@ private static void assertJoinType(final SQLCaseAssertContext assertContext, fin } } - /** - * Assert actual table function segment is correct with expected table function. - * - * @param assertContext assert context - * @param actual actual table function - * @param expected expected table function - */ private static void assertTableFunction(final SQLCaseAssertContext assertContext, final ExpressionSegment actual, final ExpectedTableFunction expected) { if (actual instanceof XmlTableFunctionSegment) { XmlTableFunctionSegment actualXmlTableFunction = (XmlTableFunctionSegment) actual; @@ -255,4 +248,15 @@ private static void assertTableFunction(final SQLCaseAssertContext assertContext assertThat(assertContext.getText("Function text assert error"), actual.getText(), is(expected.getText())); } } + + private static void assertTableNameSegment(final SQLCaseAssertContext assertContext, final SimpleTableSegment actual, final ExpectedSimpleTable expected) { + IdentifierValueAssert.assertIs(assertContext, actual.getTableName().getIdentifier(), expected, "Table"); + if (null == expected.getTableBound()) { + assertFalse(actual.getTableName().getTableBoundInfo().isPresent(), assertContext.getText("Actual table bound should not exist.")); + } else { + assertTrue(actual.getTableName().getTableBoundInfo().isPresent(), assertContext.getText("Actual table bound should exist.")); + IdentifierValueAssert.assertIs(assertContext, actual.getTableName().getTableBoundInfo().get().getOriginalDatabase(), expected.getTableBound().getOriginalDatabase(), "Bound Database"); + IdentifierValueAssert.assertIs(assertContext, actual.getTableName().getTableBoundInfo().get().getOriginalSchema(), expected.getTableBound().getOriginalSchema(), "Bound Schema"); + } + } } diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/ExpectedColumnBoundInfo.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/ExpectedColumnBoundInfo.java new file mode 100644 index 0000000000000..bbbcb1cafde9f --- /dev/null +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/ExpectedColumnBoundInfo.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound; + +import lombok.Getter; +import lombok.Setter; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedSQLSegment; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.column.ExpectedOriginalColumn; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.column.ExpectedOriginalTable; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.table.ExpectedOriginalDatabase; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.table.ExpectedOriginalSchema; + +import javax.xml.bind.annotation.XmlElement; + +/** + * Expected column bound info. + */ +@Getter +@Setter +public final class ExpectedColumnBoundInfo extends AbstractExpectedSQLSegment { + + @XmlElement(name = "original-database") + private ExpectedOriginalDatabase originalDatabase; + + @XmlElement(name = "original-schema") + private ExpectedOriginalSchema originalSchema; + + @XmlElement(name = "original-table") + private ExpectedOriginalTable originalTable; + + @XmlElement(name = "original-column") + private ExpectedOriginalColumn originalColumn; +} diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/ExpectedTableBoundInfo.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/ExpectedTableBoundInfo.java new file mode 100644 index 0000000000000..ea47c9c9e867b --- /dev/null +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/ExpectedTableBoundInfo.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound; + +import lombok.Getter; +import lombok.Setter; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedSQLSegment; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.table.ExpectedOriginalDatabase; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.table.ExpectedOriginalSchema; + +import javax.xml.bind.annotation.XmlElement; + +/** + * Expected table bound info. + */ +@Getter +@Setter +public final class ExpectedTableBoundInfo extends AbstractExpectedSQLSegment { + + @XmlElement(name = "original-database") + private ExpectedOriginalDatabase originalDatabase; + + @XmlElement(name = "original-schema") + private ExpectedOriginalSchema originalSchema; +} diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/column/ExpectedOriginalColumn.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/column/ExpectedOriginalColumn.java new file mode 100644 index 0000000000000..63249f7b7d171 --- /dev/null +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/column/ExpectedOriginalColumn.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.column; + +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedIdentifierSQLSegment; + +/** + * Expected original column. + */ +public final class ExpectedOriginalColumn extends AbstractExpectedIdentifierSQLSegment { +} diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/column/ExpectedOriginalTable.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/column/ExpectedOriginalTable.java new file mode 100644 index 0000000000000..a7e863a1835a6 --- /dev/null +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/column/ExpectedOriginalTable.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.column; + +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedIdentifierSQLSegment; + +/** + * Expected original table. + */ +public final class ExpectedOriginalTable extends AbstractExpectedIdentifierSQLSegment { +} diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/table/ExpectedOriginalDatabase.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/table/ExpectedOriginalDatabase.java new file mode 100644 index 0000000000000..fbf3a0ec66252 --- /dev/null +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/table/ExpectedOriginalDatabase.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.table; + +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedIdentifierSQLSegment; + +/** + * Expected original database. + */ +public final class ExpectedOriginalDatabase extends AbstractExpectedIdentifierSQLSegment { +} diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/table/ExpectedOriginalSchema.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/table/ExpectedOriginalSchema.java new file mode 100644 index 0000000000000..929acb240084d --- /dev/null +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/bound/table/ExpectedOriginalSchema.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.table; + +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedIdentifierSQLSegment; + +/** + * Expected original schema. + */ +public final class ExpectedOriginalSchema extends AbstractExpectedIdentifierSQLSegment { +} diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/projection/impl/column/ExpectedColumnProjection.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/projection/impl/column/ExpectedColumnProjection.java index 565e140fd47f3..7e57759c14db4 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/projection/impl/column/ExpectedColumnProjection.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/projection/impl/column/ExpectedColumnProjection.java @@ -20,6 +20,7 @@ import lombok.Getter; import lombok.Setter; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedIdentifierSQLSegment; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.ExpectedColumnBoundInfo; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.generic.ExpectedParentheses; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.projection.ExpectedProjection; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.table.ExpectedOwner; @@ -45,4 +46,7 @@ public final class ExpectedColumnProjection extends AbstractExpectedIdentifierSQ @XmlElement(name = "right-parentheses") private ExpectedParentheses rightParentheses; + + @XmlElement(name = "column-bound") + private ExpectedColumnBoundInfo columnBound; } diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/projection/impl/shorthand/ExpectedShorthandProjection.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/projection/impl/shorthand/ExpectedShorthandProjection.java index 019d71b2a5b7e..ba9cd77ad0e17 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/projection/impl/shorthand/ExpectedShorthandProjection.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/projection/impl/shorthand/ExpectedShorthandProjection.java @@ -21,6 +21,7 @@ import lombok.Setter; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedSQLSegment; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.projection.ExpectedProjection; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.projection.ExpectedProjections; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.table.ExpectedOwner; import javax.xml.bind.annotation.XmlElement; @@ -34,4 +35,7 @@ public final class ExpectedShorthandProjection extends AbstractExpectedSQLSegmen @XmlElement private ExpectedOwner owner; + + @XmlElement(name = "actual-projections") + private final ExpectedProjections actualProjections = new ExpectedProjections(); } diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/table/ExpectedSimpleTable.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/table/ExpectedSimpleTable.java index 7354ce47e0d78..96421c3d0e3f8 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/table/ExpectedSimpleTable.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/cases/parser/jaxb/segment/impl/table/ExpectedSimpleTable.java @@ -20,6 +20,7 @@ import lombok.Getter; import lombok.Setter; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.AbstractExpectedIdentifierSQLSegment; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.segment.impl.bound.ExpectedTableBoundInfo; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; @@ -39,6 +40,9 @@ public final class ExpectedSimpleTable extends AbstractExpectedIdentifierSQLSegm @XmlElement private ExpectedOwner owner; + @XmlElement(name = "table-bound") + private ExpectedTableBoundInfo tableBound; + @XmlElement(name = "index-hint") private final Collection indexHints = new LinkedList<>(); } diff --git a/test/it/pom.xml b/test/it/pom.xml index 501cad8031024..fa0a4b30ecf08 100644 --- a/test/it/pom.xml +++ b/test/it/pom.xml @@ -35,5 +35,6 @@ optimizer rewriter pipeline + binder From c0f7e44d480e45aafb7ec23483d8fa4f665c4c6b Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 17 Dec 2024 13:06:00 +0800 Subject: [PATCH 2/6] fix unit test --- .../internal/asserts/segment/projection/ProjectionAssert.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/projection/ProjectionAssert.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/projection/ProjectionAssert.java index a4030d552351a..1f091e7cec7f3 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/projection/ProjectionAssert.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/segment/projection/ProjectionAssert.java @@ -133,7 +133,7 @@ private static void assertShorthandProjection(final SQLCaseAssertContext assertC } private static void assertActualProjections(final SQLCaseAssertContext assertContext, final ShorthandProjectionSegment actual, final ExpectedShorthandProjection expected) { - if (null == expected.getActualProjections()) { + if (0 == expected.getActualProjections().getSize()) { assertTrue(actual.getActualProjectionSegments().isEmpty(), assertContext.getText("Actual projections should not exist.")); } else { assertFalse(actual.getActualProjectionSegments().isEmpty(), assertContext.getText("Actual projections should exist.")); From c1cc3e6c18dd352a78bff5ecd93daca9a39a321b Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 17 Dec 2024 13:58:04 +0800 Subject: [PATCH 3/6] add new SQLBinderCasesRegistry and SQLBinderTestCasesRegistry --- .../test/it/sql/binder/SQLBinderIT.java | 14 +++--- .../registry/SQLBinderTestCasesRegistry.java | 47 +++++++++++++++++++ .../sql/registry/SQLBinderCasesRegistry.java | 47 +++++++++++++++++++ .../resources/{case => cases}/dml/select.xml | 0 .../{sql/supported => sqls}/dml/select.xml | 0 5 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java create mode 100644 test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java rename test/it/binder/src/main/resources/{case => cases}/dml/select.xml (100%) rename test/it/binder/src/main/resources/{sql/supported => sqls}/dml/select.xml (100%) diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java index f8b4555bfa28b..5a79da50fc15f 100644 --- a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java +++ b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java @@ -36,14 +36,14 @@ import org.apache.shardingsphere.sql.parser.api.SQLParserEngine; import org.apache.shardingsphere.sql.parser.api.SQLStatementVisitorEngine; import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; +import org.apache.shardingsphere.test.it.sql.binder.cases.binder.registry.SQLBinderTestCasesRegistry; +import org.apache.shardingsphere.test.it.sql.binder.cases.sql.registry.SQLBinderCasesRegistry; import org.apache.shardingsphere.test.it.sql.parser.internal.InternalSQLParserTestParameter; import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext; import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.SQLStatementAssert; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.SQLParserTestCases; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.SQLParserTestCase; -import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.registry.SQLParserTestCasesRegistry; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.SQLCases; -import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.registry.SQLCasesRegistry; import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.type.SQLCaseType; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.ParameterizedTest; @@ -66,15 +66,15 @@ public abstract class SQLBinderIT { - private static final SQLCases SQL_CASES = SQLCasesRegistry.getInstance().getCases(); + private static final SQLCases SQL_CASES = SQLBinderCasesRegistry.getInstance().getCases(); - private static final SQLParserTestCases SQL_PARSER_TEST_CASES = SQLParserTestCasesRegistry.getInstance().getCases(); + private static final SQLParserTestCases SQL_BINDER_TEST_CASES = SQLBinderTestCasesRegistry.getInstance().getCases(); @ParameterizedTest(name = "{0} ({1}) -> {2}") @ArgumentsSource(TestCaseArgumentsProvider.class) void assertBind(final String sqlCaseId, final SQLCaseType sqlCaseType, final String databaseType) { - String sql = SQL_CASES.getSQL(sqlCaseId, sqlCaseType, SQL_PARSER_TEST_CASES.get(sqlCaseId).getParameters()); - SQLParserTestCase expected = SQL_PARSER_TEST_CASES.get(sqlCaseId); + String sql = SQL_CASES.getSQL(sqlCaseId, sqlCaseType, SQL_BINDER_TEST_CASES.get(sqlCaseId).getParameters()); + SQLParserTestCase expected = SQL_BINDER_TEST_CASES.get(sqlCaseId); SQLStatement actual = bindSQLStatement("H2".equals(databaseType) ? "MySQL" : databaseType, sql, new ArrayList<>(expected.getParameters())); SQLStatementAssert.assertIs(new SQLCaseAssertContext(sqlCaseId, sql, expected.getParameters(), sqlCaseType), actual, expected); } @@ -181,7 +181,7 @@ private Collection getTestParameters(final String... databaseTypes) { } private boolean isPlaceholderWithoutParameter(final InternalSQLParserTestParameter testParam) { - return SQLCaseType.PLACEHOLDER == testParam.getSqlCaseType() && SQL_PARSER_TEST_CASES.get(testParam.getSqlCaseId()).getParameters().isEmpty(); + return SQLCaseType.PLACEHOLDER == testParam.getSqlCaseType() && SQL_BINDER_TEST_CASES.get(testParam.getSqlCaseId()).getParameters().isEmpty(); } } } diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java new file mode 100644 index 0000000000000..8894e9e2c5d9c --- /dev/null +++ b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.binder.cases.binder.registry; + +import lombok.Getter; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.SQLParserTestCases; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.loader.SQLParserTestCaseLoaderCallback; +import org.apache.shardingsphere.test.it.sql.parser.internal.loader.CaseLoaderTemplate; + +/** + * SQL binder test cases registry. + */ +@Getter +public final class SQLBinderTestCasesRegistry { + + private static final SQLBinderTestCasesRegistry INSTANCE = new SQLBinderTestCasesRegistry(); + + private final SQLParserTestCases cases; + + private SQLBinderTestCasesRegistry() { + cases = new SQLParserTestCases(CaseLoaderTemplate.load("cases/", new SQLParserTestCaseLoaderCallback())); + } + + /** + * Get instance. + * + * @return got instance + */ + public static SQLBinderTestCasesRegistry getInstance() { + return INSTANCE; + } +} diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java new file mode 100644 index 0000000000000..c60dbe4eb7397 --- /dev/null +++ b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.it.sql.binder.cases.sql.registry; + +import lombok.Getter; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.SQLCases; +import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.loader.SQLCaseLoaderCallback; +import org.apache.shardingsphere.test.it.sql.parser.internal.loader.CaseLoaderTemplate; + +/** + * SQL binder cases registry. + */ +@Getter +public final class SQLBinderCasesRegistry { + + private static final SQLBinderCasesRegistry INSTANCE = new SQLBinderCasesRegistry(); + + private final SQLCases cases; + + private SQLBinderCasesRegistry() { + cases = new SQLCases(CaseLoaderTemplate.load("sqls/", new SQLCaseLoaderCallback())); + } + + /** + * Get instance. + * + * @return got instance + */ + public static SQLBinderCasesRegistry getInstance() { + return INSTANCE; + } +} diff --git a/test/it/binder/src/main/resources/case/dml/select.xml b/test/it/binder/src/main/resources/cases/dml/select.xml similarity index 100% rename from test/it/binder/src/main/resources/case/dml/select.xml rename to test/it/binder/src/main/resources/cases/dml/select.xml diff --git a/test/it/binder/src/main/resources/sql/supported/dml/select.xml b/test/it/binder/src/main/resources/sqls/dml/select.xml similarity index 100% rename from test/it/binder/src/main/resources/sql/supported/dml/select.xml rename to test/it/binder/src/main/resources/sqls/dml/select.xml From 368d98e6a554f67d98b0bbe3414275643e58b0c5 Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 17 Dec 2024 14:00:06 +0800 Subject: [PATCH 4/6] revise java doc --- .../shardingsphere/test/it/sql/binder/SQLBinderITSettings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java index c2bbd671fb3c1..17fc2ee46b516 100644 --- a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java +++ b/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java @@ -22,7 +22,7 @@ import java.lang.annotation.RetentionPolicy; /** - * Internal SQL parser integrate test settings. + * SQL binder integrate test settings. */ @Inherited @Retention(RetentionPolicy.RUNTIME) From f610446666899dc994b3b4a07cbfbdb7ea64615c Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 17 Dec 2024 14:11:45 +0800 Subject: [PATCH 5/6] move test logic to test package --- .../apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java | 2 +- .../shardingsphere/test/it/sql/binder/SQLBinderITSettings.java | 0 .../cases/binder/registry/SQLBinderTestCasesRegistry.java | 0 .../sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java | 0 .../test/it/sql/binder/{it => dialect}/mysql/MySQLBinderIT.java | 2 +- .../it/binder/src/{main => test}/resources/cases/dml/select.xml | 0 test/it/binder/src/{main => test}/resources/sqls/dml/select.xml | 0 7 files changed, 2 insertions(+), 2 deletions(-) rename test/it/binder/src/{main => test}/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java (99%) rename test/it/binder/src/{main => test}/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java (100%) rename test/it/binder/src/{main => test}/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java (100%) rename test/it/binder/src/{main => test}/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java (100%) rename test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/{it => dialect}/mysql/MySQLBinderIT.java (93%) rename test/it/binder/src/{main => test}/resources/cases/dml/select.xml (100%) rename test/it/binder/src/{main => test}/resources/sqls/dml/select.xml (100%) diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java similarity index 99% rename from test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java rename to test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java index 5a79da50fc15f..657bc809b5384 100644 --- a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java +++ b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderIT.java @@ -166,7 +166,7 @@ private static class TestCaseArgumentsProvider implements ArgumentsProvider { @Override public Stream provideArguments(final ExtensionContext extensionContext) { SQLBinderITSettings settings = extensionContext.getRequiredTestClass().getAnnotation(SQLBinderITSettings.class); - Preconditions.checkNotNull(settings, "Annotation InternalSQLParserITSettings is required."); + Preconditions.checkNotNull(settings, "Annotation SQLBinderITSettings is required."); return getTestParameters(settings.value()).stream(); } diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java similarity index 100% rename from test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java rename to test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/SQLBinderITSettings.java diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java similarity index 100% rename from test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java rename to test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java diff --git a/test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java similarity index 100% rename from test/it/binder/src/main/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java rename to test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java diff --git a/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/it/mysql/MySQLBinderIT.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/dialect/mysql/MySQLBinderIT.java similarity index 93% rename from test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/it/mysql/MySQLBinderIT.java rename to test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/dialect/mysql/MySQLBinderIT.java index 54340c747caf0..1b5cbdf70b0af 100644 --- a/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/it/mysql/MySQLBinderIT.java +++ b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/dialect/mysql/MySQLBinderIT.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.test.it.sql.binder.it.mysql; +package org.apache.shardingsphere.test.it.sql.binder.dialect.mysql; import org.apache.shardingsphere.test.it.sql.binder.SQLBinderIT; import org.apache.shardingsphere.test.it.sql.binder.SQLBinderITSettings; diff --git a/test/it/binder/src/main/resources/cases/dml/select.xml b/test/it/binder/src/test/resources/cases/dml/select.xml similarity index 100% rename from test/it/binder/src/main/resources/cases/dml/select.xml rename to test/it/binder/src/test/resources/cases/dml/select.xml diff --git a/test/it/binder/src/main/resources/sqls/dml/select.xml b/test/it/binder/src/test/resources/sqls/dml/select.xml similarity index 100% rename from test/it/binder/src/main/resources/sqls/dml/select.xml rename to test/it/binder/src/test/resources/sqls/dml/select.xml From f383870d7177c354724ce7ca30cdce5f40f59c11 Mon Sep 17 00:00:00 2001 From: duanzhengqiang Date: Tue, 17 Dec 2024 14:31:20 +0800 Subject: [PATCH 6/6] modify jar load case --- .../registry/SQLBinderTestCasesRegistry.java | 5 ++++- .../sql/registry/SQLBinderCasesRegistry.java | 5 ++++- .../internal/loader/CaseLoaderTemplate.java | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java index 8894e9e2c5d9c..792a14de095e7 100644 --- a/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java +++ b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/binder/registry/SQLBinderTestCasesRegistry.java @@ -22,6 +22,8 @@ import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.loader.SQLParserTestCaseLoaderCallback; import org.apache.shardingsphere.test.it.sql.parser.internal.loader.CaseLoaderTemplate; +import java.io.File; + /** * SQL binder test cases registry. */ @@ -33,7 +35,8 @@ public final class SQLBinderTestCasesRegistry { private final SQLParserTestCases cases; private SQLBinderTestCasesRegistry() { - cases = new SQLParserTestCases(CaseLoaderTemplate.load("cases/", new SQLParserTestCaseLoaderCallback())); + File file = new File(SQLBinderTestCasesRegistry.class.getProtectionDomain().getCodeSource().getLocation().getPath()); + cases = new SQLParserTestCases(CaseLoaderTemplate.load(file, "cases/", new SQLParserTestCaseLoaderCallback())); } /** diff --git a/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java index c60dbe4eb7397..6807d49ffafa8 100644 --- a/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java +++ b/test/it/binder/src/test/java/org/apache/shardingsphere/test/it/sql/binder/cases/sql/registry/SQLBinderCasesRegistry.java @@ -22,6 +22,8 @@ import org.apache.shardingsphere.test.it.sql.parser.internal.cases.sql.loader.SQLCaseLoaderCallback; import org.apache.shardingsphere.test.it.sql.parser.internal.loader.CaseLoaderTemplate; +import java.io.File; + /** * SQL binder cases registry. */ @@ -33,7 +35,8 @@ public final class SQLBinderCasesRegistry { private final SQLCases cases; private SQLBinderCasesRegistry() { - cases = new SQLCases(CaseLoaderTemplate.load("sqls/", new SQLCaseLoaderCallback())); + File file = new File(SQLBinderCasesRegistry.class.getProtectionDomain().getCodeSource().getLocation().getPath()); + cases = new SQLCases(CaseLoaderTemplate.load(file, "sqls/", new SQLCaseLoaderCallback())); } /** diff --git a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/loader/CaseLoaderTemplate.java b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/loader/CaseLoaderTemplate.java index ac0fecb2d35e2..c1f6ba1430ff4 100644 --- a/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/loader/CaseLoaderTemplate.java +++ b/test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/loader/CaseLoaderTemplate.java @@ -40,9 +40,22 @@ public final class CaseLoaderTemplate { * @param type of test case * @return loaded cases */ - @SneakyThrows({JAXBException.class, IOException.class}) public static Map load(final String rootDirectory, final CaseLoaderCallback callback) { File file = new File(CaseLoaderTemplate.class.getProtectionDomain().getCodeSource().getLocation().getPath()); + return load(file, rootDirectory, callback); + } + + /** + * Load test cases. + * + * @param file file + * @param rootDirectory root directory + * @param callback callback of cases loader + * @param type of test case + * @return loaded cases + */ + @SneakyThrows({JAXBException.class, IOException.class}) + public static Map load(final File file, final String rootDirectory, final CaseLoaderCallback callback) { return file.isFile() ? callback.loadFromJar(file, rootDirectory) : callback.loadFromDirectory(rootDirectory); } }