This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 5fcad9b6f96 Add more unit test for SelectStatementContext (#18713)
5fcad9b6f96 is described below
commit 5fcad9b6f96c5d1f1ea5c5396a1c61281d7cb4dd
Author: Ziyuan Han <[email protected]>
AuthorDate: Thu Jun 30 16:17:26 2022 +0800
Add more unit test for SelectStatementContext (#18713)
* Add unit test for SelectStatementContext#isContainsDollarParameterMarker
* Add unit test for SelectStatementContext#isContainsDollarParameterMarker
* Add unit test for
SelectStatementContext#isContainsPartialDistinctAggregation
* Code style format
* Code style format
* Code style format
* Code style format
* Code review fix
---
.../statement/impl/SelectStatementContextTest.java | 80 ++++++++++++++++++++++
1 file changed, 80 insertions(+)
diff --git
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/impl/SelectStatementContextTest.java
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/impl/SelectStatementContextTest.java
index 905e322b28f..609dd315ed4 100644
---
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/impl/SelectStatementContextTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/impl/SelectStatementContextTest.java
@@ -22,11 +22,14 @@ import
org.apache.shardingsphere.infra.database.DefaultDatabase;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.constant.ParameterMarkerType;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
@@ -40,6 +43,7 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.Or
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.JoinTableSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
@@ -484,7 +488,83 @@ public final class SelectStatementContextTest {
assertTrue(new SelectStatementContext(
Collections.singletonMap(DefaultDatabase.LOGIC_NAME,
mock(ShardingSphereDatabase.class)), Collections.emptyList(), selectStatement,
DefaultDatabase.LOGIC_NAME).isContainsSubquery());
}
+
+ @Test
+ public void assertContainsDollarParameterMarkerForMySQL() {
+ assertContainsDollarParameterMarker(new MySQLSelectStatement());
+ }
+
+ @Test
+ public void assertContainsDollarParameterMarkerForOracle() {
+ assertContainsDollarParameterMarker(new OracleSelectStatement());
+ }
+ @Test
+ public void assertContainsDollarParameterMarkerForPostgreSQL() {
+ assertContainsDollarParameterMarker(new PostgreSQLSelectStatement());
+ }
+
+ @Test
+ public void assertContainsDollarParameterMarkerForSQL92() {
+ assertContainsDollarParameterMarker(new SQL92SelectStatement());
+ }
+
+ @Test
+ public void assertContainsDollarParameterMarkerForSQLServer() {
+ assertContainsDollarParameterMarker(new SQLServerSelectStatement());
+ }
+
+ private void assertContainsDollarParameterMarker(final SelectStatement
selectStatement) {
+ ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
+ projectionsSegment.getProjections().add(new
ParameterMarkerExpressionSegment(0, 0, 0, ParameterMarkerType.DOLLAR));
+ selectStatement.setProjections(projectionsSegment);
+ SelectStatementContext selectStatementContext = new
SelectStatementContext(Collections.singletonMap(DefaultDatabase.LOGIC_NAME,
mock(ShardingSphereDatabase.class)), Collections.emptyList(),
+ selectStatement, DefaultDatabase.LOGIC_NAME);
+ assertTrue(selectStatementContext.isContainsDollarParameterMarker());
+ selectStatement.setProjections(new ProjectionsSegment(0, 0));
+ JoinTableSegment joinTableSegment = new JoinTableSegment();
+ joinTableSegment.setCondition(new ParameterMarkerExpressionSegment(0,
0, 0, ParameterMarkerType.DOLLAR));
+ selectStatement.setFrom(joinTableSegment);
+ selectStatementContext = new SelectStatementContext(
+ Collections.singletonMap(DefaultDatabase.LOGIC_NAME,
mock(ShardingSphereDatabase.class)), Collections.emptyList(), selectStatement,
DefaultDatabase.LOGIC_NAME);
+ assertTrue(selectStatementContext.isContainsDollarParameterMarker());
+ }
+
+ @Test
+ public void assertContainsPartialDistinctAggregationForMySQL() {
+ assertContainsPartialDistinctAggregation(new MySQLSelectStatement());
+ }
+
+ @Test
+ public void assertContainsPartialDistinctAggregationForOracle() {
+ assertContainsPartialDistinctAggregation(new OracleSelectStatement());
+ }
+
+ @Test
+ public void assertContainsPartialDistinctAggregationForPostgreSQL() {
+ assertContainsPartialDistinctAggregation(new
PostgreSQLSelectStatement());
+ }
+
+ @Test
+ public void assertContainsPartialDistinctAggregationForSQL92() {
+ assertContainsPartialDistinctAggregation(new SQL92SelectStatement());
+ }
+
+ @Test
+ public void assertContainsPartialDistinctAggregationForSQLServer() {
+ assertContainsPartialDistinctAggregation(new
SQLServerSelectStatement());
+ }
+
+ private void assertContainsPartialDistinctAggregation(final
SelectStatement selectStatement) {
+ ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
+ projectionsSegment.getProjections().add(new
AggregationProjectionSegment(0, 0, AggregationType.COUNT, "(*)"));
+ projectionsSegment.getProjections().add(new
AggregationDistinctProjectionSegment(0, 10, AggregationType.COUNT, "(1)",
"distinctExpression"));
+ selectStatement.setProjections(projectionsSegment);
+ SelectStatementContext selectStatementContext = new
SelectStatementContext(
+ Collections.singletonMap(DefaultDatabase.LOGIC_NAME,
mock(ShardingSphereDatabase.class)), Collections.emptyList(), selectStatement,
DefaultDatabase.LOGIC_NAME);
+
assertTrue(selectStatementContext.isContainsPartialDistinctAggregation());
+ }
+
private OrderByItemSegment createOrderByItemSegment(final String type) {
switch (type) {
case INDEX_ORDER_BY: