You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/06/02 09:06:00 UTC
[shardingsphere] branch master updated: fix select with group by
rewrite error (#10620)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 5e4b14e fix select with group by rewrite error (#10620)
5e4b14e is described below
commit 5e4b14e4feefa5ab01167cfa1408092c1d437055
Author: Zhengqiang Duan <st...@gmail.com>
AuthorDate: Wed Jun 2 17:05:09 2021 +0800
fix select with group by rewrite error (#10620)
* fix select order by rewrite error
* support oracle, sqlserver & sql92
* add sql parse test
* add rewrite test case
---
.../merge/ShardingResultMergerEngineTest.java | 2 +-
.../merge/dql/ShardingDQLResultMergerTest.java | 62 ++++++-------
.../dql/groupby/GroupByMemoryMergedResultTest.java | 2 +-
.../dql/groupby/GroupByRowComparatorTest.java | 16 ++--
.../dql/groupby/GroupByStreamMergedResultTest.java | 2 +-
.../iterator/IteratorStreamMergedResultTest.java | 2 +-
.../dql/orderby/OrderByStreamMergedResultTest.java | 2 +-
.../merge/dql/orderby/OrderByValueTest.java | 6 +-
.../pagination/LimitDecoratorMergedResultTest.java | 6 +-
.../RowNumberDecoratorMergedResultTest.java | 16 ++--
.../TopAndRowNumberDecoratorMergedResultTest.java | 10 +-
.../generator/impl/OrderByTokenGenerator.java | 22 +----
.../src/test/resources/sharding/select.xml | 16 +++-
.../segment/select/groupby/GroupByContext.java | 2 -
.../groupby/engine/GroupByContextEngine.java | 6 +-
.../statement/dml/SelectStatementContext.java | 22 +++++
.../groupby/engine/GroupByContextEngineTest.java | 2 -
.../orderby/engine/OrderByContextEngineTest.java | 10 +-
.../engine/ProjectionsContextEngineTest.java | 16 ++--
.../statement/impl/SelectStatementContextTest.java | 12 +--
.../src/main/antlr4/imports/mysql/BaseRule.g4 | 2 +-
.../statement/impl/MySQLStatementSQLVisitor.java | 101 +++++++++++++--------
.../impl/OracleDMLStatementSQLVisitor.java | 11 +++
.../impl/PostgreSQLStatementSQLVisitor.java | 21 +++++
.../impl/SQL92DMLStatementSQLVisitor.java | 15 ++-
.../impl/SQLServerDMLStatementSQLVisitor.java | 11 +++
.../segment/dml/predicate/HavingSegment.java | 19 ++--
.../sql/common/segment/generic/WindowSegment.java | 14 ++-
.../sql/common/statement/dml/SelectStatement.java | 12 +++
.../handler/dml/SelectStatementHandler.java | 17 ++++
.../statement/mysql/dml/MySQLSelectStatement.java | 12 +++
.../postgresql/dml/PostgreSQLSelectStatement.java | 14 ++-
.../handler/dml/SelectStatementHandlerTest.java | 31 +++++++
.../asserts/segment/having/HavingClauseAssert.java | 45 +++++++++
.../statement/dml/impl/SelectStatementAssert.java | 23 +++++
.../segment/impl/having/ExpectedHavingClause.java | 25 ++---
.../segment/impl/window/ExpectedWindowClause.java | 20 ++--
.../statement/dml/SelectStatementTestCase.java | 10 +-
.../main/resources/case/dml/select-group-by.xml | 53 +++++++++++
.../sql/supported/dml/select-group-by.xml | 2 +
40 files changed, 500 insertions(+), 192 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/ShardingResultMergerEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/ShardingResultMergerEngineTest.java
index bdc1c91..c6cfc5a 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/ShardingResultMergerEngineTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/ShardingResultMergerEngineTest.java
@@ -82,7 +82,7 @@ public final class ShardingResultMergerEngineTest {
private void assertNewInstanceWithSelectStatement(final SelectStatement selectStatement) {
ConfigurationProperties props = new ConfigurationProperties(new Properties());
SelectStatementContext sqlStatementContext = new SelectStatementContext(selectStatement,
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
assertThat(new ShardingResultMergerEngine().newInstance(DatabaseTypeRegistry.getActualDatabaseType("MySQL"), null, props, sqlStatementContext), instanceOf(ShardingDQLResultMerger.class));
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java
index 9ea516f3..a7bca3b 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java
@@ -18,9 +18,19 @@
package org.apache.shardingsphere.sharding.merge.dql;
import com.google.common.collect.ImmutableMap;
+import org.apache.shardingsphere.infra.binder.segment.select.groupby.GroupByContext;
+import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByContext;
+import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
+import org.apache.shardingsphere.infra.binder.segment.select.pagination.PaginationContext;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection;
+import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
+import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.sharding.merge.dql.groupby.GroupByMemoryMergedResult;
import org.apache.shardingsphere.sharding.merge.dql.groupby.GroupByStreamMergedResult;
import org.apache.shardingsphere.sharding.merge.dql.iterator.IteratorStreamMergedResult;
@@ -28,16 +38,6 @@ import org.apache.shardingsphere.sharding.merge.dql.orderby.OrderByStreamMergedR
import org.apache.shardingsphere.sharding.merge.dql.pagination.LimitDecoratorMergedResult;
import org.apache.shardingsphere.sharding.merge.dql.pagination.RowNumberDecoratorMergedResult;
import org.apache.shardingsphere.sharding.merge.dql.pagination.TopAndRowNumberDecoratorMergedResult;
-import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
-import org.apache.shardingsphere.infra.binder.segment.select.groupby.GroupByContext;
-import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByContext;
-import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
-import org.apache.shardingsphere.infra.binder.segment.select.pagination.PaginationContext;
-import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext;
-import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection;
-import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
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.segment.dml.item.ProjectionsSegment;
@@ -70,7 +70,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildIteratorStreamMergedResult() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new MySQLSelectStatement()),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
assertThat(resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema()), instanceOf(IteratorStreamMergedResult.class));
}
@@ -79,7 +79,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildIteratorStreamMergedResultWithLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new MySQLSelectStatement()),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList()));
assertThat(resultMerger.merge(Collections.singletonList(createQueryResult()), selectStatementContext, buildSchema()), instanceOf(IteratorStreamMergedResult.class));
@@ -89,7 +89,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildIteratorStreamMergedResultWithMySQLLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new MySQLSelectStatement()),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema());
@@ -101,7 +101,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildIteratorStreamMergedResultWithOracleLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("Oracle"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new OracleSelectStatement()),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema());
@@ -113,7 +113,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildIteratorStreamMergedResultWithSQLServerLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("SQLServer"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new SQLServerSelectStatement()),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema());
@@ -125,7 +125,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildOrderByStreamMergedResult() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- buildSelectStatement(new MySQLSelectStatement()), new GroupByContext(Collections.emptyList(), 0),
+ buildSelectStatement(new MySQLSelectStatement()), new GroupByContext(Collections.emptyList()),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
assertThat(resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema()), instanceOf(OrderByStreamMergedResult.class));
@@ -135,7 +135,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildOrderByStreamMergedResultWithMySQLLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- buildSelectStatement(new MySQLSelectStatement()), new GroupByContext(Collections.emptyList(), 0),
+ buildSelectStatement(new MySQLSelectStatement()), new GroupByContext(Collections.emptyList()),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList()));
@@ -148,7 +148,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildOrderByStreamMergedResultWithOracleLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("Oracle"));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- buildSelectStatement(new OracleSelectStatement()), new GroupByContext(Collections.emptyList(), 0),
+ buildSelectStatement(new OracleSelectStatement()), new GroupByContext(Collections.emptyList()),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
@@ -161,7 +161,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildOrderByStreamMergedResultWithSQLServerLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("SQLServer"));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- buildSelectStatement(new SQLServerSelectStatement()), new GroupByContext(Collections.emptyList(), 0),
+ buildSelectStatement(new SQLServerSelectStatement()), new GroupByContext(Collections.emptyList()),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
@@ -174,7 +174,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildGroupByStreamMergedResult() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new MySQLSelectStatement()),
- new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)))),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
assertThat(resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema()), instanceOf(GroupByStreamMergedResult.class));
@@ -184,7 +184,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildGroupByStreamMergedResultWithMySQLLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new MySQLSelectStatement()),
- new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)))),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList()));
@@ -197,7 +197,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildGroupByStreamMergedResultWithOracleLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("Oracle"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new OracleSelectStatement()),
- new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)))),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
@@ -210,7 +210,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildGroupByStreamMergedResultWithSQLServerLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("SQLServer"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new SQLServerSelectStatement()),
- new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)))),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
@@ -223,7 +223,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildGroupByMemoryMergedResult() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new MySQLSelectStatement()),
- new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)))),
new OrderByContext(Collections.emptyList(), false), new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(null, null, Collections.emptyList()));
assertThat(resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema()), instanceOf(GroupByMemoryMergedResult.class));
@@ -233,7 +233,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildGroupByMemoryMergedResultWithMySQLLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new MySQLSelectStatement()),
- new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)))),
new OrderByContext(Collections.emptyList(), false), new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema());
@@ -245,7 +245,7 @@ public final class ShardingDQLResultMergerTest {
public void assertBuildGroupByMemoryMergedResultWithOracleLimit() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("Oracle"));
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new OracleSelectStatement()),
- new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)))),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
@@ -260,7 +260,7 @@ public final class ShardingDQLResultMergerTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(buildSelectStatement(new SQLServerSelectStatement()),
new GroupByContext(Arrays.asList(
new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)),
- new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC))), 0), new OrderByContext(Collections.emptyList(), false),
+ new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)))), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema());
@@ -274,7 +274,7 @@ public final class ShardingDQLResultMergerTest {
ProjectionsContext projectionsContext = new ProjectionsContext(
0, 0, false, Collections.singletonList(new AggregationProjection(AggregationType.COUNT, "(*)", null)));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- buildSelectStatement(new MySQLSelectStatement()), new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ buildSelectStatement(new MySQLSelectStatement()), new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
projectionsContext, new PaginationContext(null, null, Collections.emptyList()));
assertThat(resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema()), instanceOf(GroupByMemoryMergedResult.class));
}
@@ -285,7 +285,7 @@ public final class ShardingDQLResultMergerTest {
ProjectionsContext projectionsContext = new ProjectionsContext(
0, 0, false, Collections.singletonList(new AggregationProjection(AggregationType.COUNT, "(*)", null)));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- buildSelectStatement(new MySQLSelectStatement()), new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ buildSelectStatement(new MySQLSelectStatement()), new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
projectionsContext, new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema());
assertThat(actual, instanceOf(LimitDecoratorMergedResult.class));
@@ -298,7 +298,7 @@ public final class ShardingDQLResultMergerTest {
ProjectionsContext projectionsContext = new ProjectionsContext(
0, 0, false, Collections.singletonList(new AggregationProjection(AggregationType.COUNT, "(*)", null)));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- buildSelectStatement(new OracleSelectStatement()), new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ buildSelectStatement(new OracleSelectStatement()), new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
projectionsContext, new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema());
assertThat(actual, instanceOf(RowNumberDecoratorMergedResult.class));
@@ -311,7 +311,7 @@ public final class ShardingDQLResultMergerTest {
ProjectionsContext projectionsContext = new ProjectionsContext(
0, 0, false, Collections.singletonList(new AggregationProjection(AggregationType.COUNT, "(*)", null)));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- buildSelectStatement(new SQLServerSelectStatement()), new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ buildSelectStatement(new SQLServerSelectStatement()), new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
projectionsContext, new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(createQueryResults(), selectStatementContext, buildSchema());
assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class));
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java
index 9f70c94..ebcb8fd 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java
@@ -112,7 +112,7 @@ public final class GroupByMemoryMergedResultTest {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
selectStatement.setProjections(projectionsSegment);
return new SelectStatementContext(selectStatement,
- new GroupByContext(Collections.singletonList(createOrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(createOrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC)))),
new OrderByContext(Collections.singletonList(createOrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.DESC, OrderDirection.ASC))), false),
projectionsContext, new PaginationContext(null, null, Collections.emptyList()));
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByRowComparatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByRowComparatorTest.java
index 41331e3..24e2db1 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByRowComparatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByRowComparatorTest.java
@@ -18,14 +18,14 @@
package org.apache.shardingsphere.sharding.merge.dql.groupby;
import com.google.common.collect.Lists;
-import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
-import org.apache.shardingsphere.infra.merge.result.impl.memory.MemoryQueryResultRow;
import org.apache.shardingsphere.infra.binder.segment.select.groupby.GroupByContext;
import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByContext;
import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
import org.apache.shardingsphere.infra.binder.segment.select.pagination.PaginationContext;
import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
+import org.apache.shardingsphere.infra.merge.result.impl.memory.MemoryQueryResultRow;
import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
@@ -83,7 +83,7 @@ public final class GroupByRowComparatorTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement,
new GroupByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)),
- createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), 0),
+ createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC)))),
new OrderByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), false),
@@ -123,7 +123,7 @@ public final class GroupByRowComparatorTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement,
new GroupByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
- createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), 0),
+ createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC)))),
new OrderByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), false),
@@ -163,7 +163,7 @@ public final class GroupByRowComparatorTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement,
new GroupByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)),
- createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), 0),
+ createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC)))),
new OrderByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), false),
@@ -203,7 +203,7 @@ public final class GroupByRowComparatorTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement,
new GroupByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
- createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), 0), new OrderByContext(Collections.emptyList(), false),
+ createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC)))), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatementContext, caseSensitives);
MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
@@ -240,7 +240,7 @@ public final class GroupByRowComparatorTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement,
new GroupByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)),
- createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), 0), new OrderByContext(Collections.emptyList(), false),
+ createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC)))), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatementContext, caseSensitives);
MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
@@ -277,7 +277,7 @@ public final class GroupByRowComparatorTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(selectStatement,
new GroupByContext(Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
- createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), 0), new OrderByContext(Collections.emptyList(), false),
+ createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC)))), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatementContext, caseSensitives);
MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java
index 0a0ece0..814d24c 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java
@@ -173,7 +173,7 @@ public final class GroupByStreamMergedResultTest {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
selectStatement.setProjections(projectionsSegment);
return new SelectStatementContext(selectStatement,
- new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), 0),
+ new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC)))),
new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), false),
projectionsContext, new PaginationContext(null, null, Collections.emptyList()));
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/iterator/IteratorStreamMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/iterator/IteratorStreamMergedResultTest.java
index 6942da3..96444d3 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/iterator/IteratorStreamMergedResultTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/iterator/IteratorStreamMergedResultTest.java
@@ -48,7 +48,7 @@ public final class IteratorStreamMergedResultTest {
@Before
public void setUp() {
selectStatementContext = new SelectStatementContext(new MySQLSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java
index 48d58a9..05b92d9 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java
@@ -68,7 +68,7 @@ public final class OrderByStreamMergedResultTest {
OrderByContext orderByContext = new OrderByContext(Arrays.asList(
new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
new OrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), false);
- selectStatementContext = new SelectStatementContext(selectStatement, new GroupByContext(Collections.emptyList(), 0), orderByContext,
+ selectStatementContext = new SelectStatementContext(selectStatement, new GroupByContext(Collections.emptyList()), orderByContext,
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByValueTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByValueTest.java
index 2ae7963..62e3c80 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByValueTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByValueTest.java
@@ -85,7 +85,7 @@ public final class OrderByValueTest {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
selectStatement.setProjections(projectionsSegment);
SelectStatementContext selectStatementContext = new SelectStatementContext(
- selectStatement, new GroupByContext(Collections.emptyList(), 0), createOrderBy(), createProjectionsContext(), null);
+ selectStatement, new GroupByContext(Collections.emptyList()), createOrderBy(), createProjectionsContext(), null);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
QueryResult queryResult1 = createQueryResult("1", "2");
OrderByValue orderByValue1 = new OrderByValue(queryResult1, Arrays.asList(
@@ -135,7 +135,7 @@ public final class OrderByValueTest {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
selectStatement.setProjections(projectionsSegment);
SelectStatementContext selectStatementContext = new SelectStatementContext(
- selectStatement, new GroupByContext(Collections.emptyList(), 0), createOrderBy(), createProjectionsContext(), null);
+ selectStatement, new GroupByContext(Collections.emptyList()), createOrderBy(), createProjectionsContext(), null);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
QueryResult queryResult1 = createQueryResult("1", "2");
OrderByValue orderByValue1 = new OrderByValue(queryResult1, Arrays.asList(
@@ -185,7 +185,7 @@ public final class OrderByValueTest {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
selectStatement.setProjections(projectionsSegment);
SelectStatementContext selectStatementContext = new SelectStatementContext(
- selectStatement, new GroupByContext(Collections.emptyList(), 0), createOrderBy(), createProjectionsContext(), null);
+ selectStatement, new GroupByContext(Collections.emptyList()), createOrderBy(), createProjectionsContext(), null);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
QueryResult queryResult1 = createQueryResult("1", "2");
OrderByValue orderByValue1 = new OrderByValue(queryResult1, Arrays.asList(
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/LimitDecoratorMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/LimitDecoratorMergedResultTest.java
index 1474fd1..eb75260 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/LimitDecoratorMergedResultTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/LimitDecoratorMergedResultTest.java
@@ -46,7 +46,7 @@ public final class LimitDecoratorMergedResultTest {
public void assertNextForSkipAll() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new MySQLSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
@@ -57,7 +57,7 @@ public final class LimitDecoratorMergedResultTest {
public void assertNextWithoutRowCount() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new MySQLSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 2), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
@@ -71,7 +71,7 @@ public final class LimitDecoratorMergedResultTest {
public void assertNextWithRowCount() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("MySQL"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new MySQLSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 2), new NumberLiteralLimitValueSegment(0, 0, 2), Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java
index af970e4..b4a1965 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java
@@ -17,15 +17,15 @@
package org.apache.shardingsphere.sharding.merge.dql.pagination;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
-import org.apache.shardingsphere.infra.merge.result.MergedResult;
-import org.apache.shardingsphere.sharding.merge.dql.ShardingDQLResultMerger;
import org.apache.shardingsphere.infra.binder.segment.select.groupby.GroupByContext;
import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByContext;
import org.apache.shardingsphere.infra.binder.segment.select.pagination.PaginationContext;
import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
+import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import org.apache.shardingsphere.sharding.merge.dql.ShardingDQLResultMerger;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.dml.OracleSelectStatement;
import org.junit.Test;
@@ -46,7 +46,7 @@ public final class RowNumberDecoratorMergedResultTest {
public void assertNextForSkipAll() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("Oracle"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new OracleSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
@@ -57,7 +57,7 @@ public final class RowNumberDecoratorMergedResultTest {
public void assertNextWithoutOffsetWithoutRowCount() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("Oracle"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new OracleSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
for (int i = 0; i < 8; i++) {
@@ -70,7 +70,7 @@ public final class RowNumberDecoratorMergedResultTest {
public void assertNextForRowCountBoundOpenedFalse() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("Oracle"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new OracleSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, false), Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
@@ -83,7 +83,7 @@ public final class RowNumberDecoratorMergedResultTest {
public void assertNextForRowCountBoundOpenedTrue() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("Oracle"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new OracleSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, true), Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java
index e8ae837..60f3047 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java
@@ -46,7 +46,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
@Test
public void assertNextForSkipAll() throws SQLException {
SelectStatementContext selectStatementContext = new SelectStatementContext(new SQLServerSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null, Collections.emptyList()));
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("SQLServer"));
@@ -58,7 +58,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
public void assertNextWithoutOffsetWithRowCount() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("SQLServer"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new SQLServerSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(null, new NumberLiteralLimitValueSegment(0, 0, 5), Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
@@ -72,7 +72,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
public void assertNextWithOffsetWithoutRowCount() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("SQLServer"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new SQLServerSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), null, Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
@@ -86,7 +86,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
public void assertNextWithOffsetBoundOpenedFalse() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("SQLServer"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new SQLServerSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, false), new NumberLiteralLimitValueSegment(0, 0, 4), Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
@@ -99,7 +99,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest {
public void assertNextWithOffsetBoundOpenedTrue() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypeRegistry.getActualDatabaseType("SQLServer"));
SelectStatementContext selectStatementContext = new SelectStatementContext(new SQLServerSelectStatement(),
- new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false),
+ new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false),
new ProjectionsContext(0, 0, false, Collections.emptyList()),
new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4), Collections.emptyList()));
MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, null);
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/OrderByTokenGenerator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/OrderByTokenGenerator.java
index b9444ac..04a0fd5 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/OrderByTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/OrderByTokenGenerator.java
@@ -17,16 +17,14 @@
package org.apache.shardingsphere.sharding.rewrite.token.generator.impl;
-import org.apache.shardingsphere.infra.rewrite.sql.token.generator.OptionalSQLTokenGenerator;
-import org.apache.shardingsphere.sharding.rewrite.token.generator.IgnoreForSingleRoute;
-import org.apache.shardingsphere.sharding.rewrite.token.pojo.OrderByToken;
import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.rewrite.sql.token.generator.OptionalSQLTokenGenerator;
+import org.apache.shardingsphere.sharding.rewrite.token.generator.IgnoreForSingleRoute;
+import org.apache.shardingsphere.sharding.rewrite.token.pojo.OrderByToken;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ExpressionOrderByItemSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
/**
* Order by token generator.
@@ -40,7 +38,7 @@ public final class OrderByTokenGenerator implements OptionalSQLTokenGenerator<Se
@Override
public OrderByToken generateSQLToken(final SelectStatementContext selectStatementContext) {
- OrderByToken result = new OrderByToken(generateOrderByIndex(selectStatementContext));
+ OrderByToken result = new OrderByToken(selectStatementContext.getGenerateOrderByStartIndex());
String columnLabel;
for (OrderByItem each : selectStatementContext.getOrderByContext().getItems()) {
if (each.getSegment() instanceof ColumnOrderByItemSegment) {
@@ -56,16 +54,4 @@ public final class OrderByTokenGenerator implements OptionalSQLTokenGenerator<Se
}
return result;
}
-
- private int generateOrderByIndex(final SelectStatementContext selectStatementContext) {
- if (selectStatementContext.getGroupByContext().getLastIndex() > 0) {
- return selectStatementContext.getGroupByContext().getLastIndex() + 1;
- }
- SelectStatement selectStatement = selectStatementContext.getSqlStatement();
- if (selectStatement.getWhere().isPresent()) {
- return selectStatement.getWhere().get().getStopIndex() + 1;
- } else {
- return selectStatementContext.getAllSimpleTableSegments().stream().mapToInt(SimpleTableSegment::getStopIndex).max().getAsInt() + 1;
- }
- }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/select.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/select.xml
index db03f52..e8ee3a3 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/select.xml
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/select.xml
@@ -45,8 +45,20 @@
<rewrite-assertion id="select_with_having">
<input sql="SELECT COUNT(account_id) as count_alias FROM t_account GROUP BY amount HAVING count_alias > ?" parameters="1"/>
- <output sql="SELECT COUNT(account_id) as count_alias , amount AS GROUP_BY_DERIVED_0 FROM t_account_0 GROUP BY amount ORDER BY amount ASC HAVING count_alias > ?" parameters="1"/>
- <output sql="SELECT COUNT(account_id) as count_alias , amount AS GROUP_BY_DERIVED_0 FROM t_account_1 GROUP BY amount ORDER BY amount ASC HAVING count_alias > ?" parameters="1"/>
+ <output sql="SELECT COUNT(account_id) as count_alias , amount AS GROUP_BY_DERIVED_0 FROM t_account_0 GROUP BY amount HAVING count_alias > ? ORDER BY amount ASC " parameters="1"/>
+ <output sql="SELECT COUNT(account_id) as count_alias , amount AS GROUP_BY_DERIVED_0 FROM t_account_1 GROUP BY amount HAVING count_alias > ? ORDER BY amount ASC " parameters="1"/>
+ </rewrite-assertion>
+
+ <rewrite-assertion id="select_with_having_and_window_for_mysql" db-type="MySQL">
+ <input sql="SELECT COUNT(account_id) as count_alias FROM t_account GROUP BY amount HAVING count_alias > ? WINDOW w AS (PARTITION BY account_id)" parameters="1"/>
+ <output sql="SELECT COUNT(account_id) as count_alias , amount AS GROUP_BY_DERIVED_0 FROM t_account_0 GROUP BY amount HAVING count_alias > ? WINDOW w AS (PARTITION BY account_id) ORDER BY amount ASC " parameters="1"/>
+ <output sql="SELECT COUNT(account_id) as count_alias , amount AS GROUP_BY_DERIVED_0 FROM t_account_1 GROUP BY amount HAVING count_alias > ? WINDOW w AS (PARTITION BY account_id) ORDER BY amount ASC " parameters="1"/>
+ </rewrite-assertion>
+
+ <rewrite-assertion id="select_with_having_and_window_for_postgresql" db-type="PostgreSQL">
+ <input sql="SELECT COUNT(account_id) as count_alias FROM t_account GROUP BY amount HAVING count_alias > ? WINDOW w AS (PARTITION BY account_id)" parameters="1"/>
+ <output sql="SELECT COUNT(account_id) as count_alias , amount AS GROUP_BY_DERIVED_0 FROM t_account_0 GROUP BY amount HAVING count_alias > ? WINDOW w AS (PARTITION BY account_id) ORDER BY amount ASC " parameters="1"/>
+ <output sql="SELECT COUNT(account_id) as count_alias , amount AS GROUP_BY_DERIVED_0 FROM t_account_1 GROUP BY amount HAVING count_alias > ? WINDOW w AS (PARTITION BY account_id) ORDER BY amount ASC " parameters="1"/>
</rewrite-assertion>
<rewrite-assertion id="select_with_schema">
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
index 97a57e3..331bc49 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
@@ -31,6 +31,4 @@ import java.util.Collection;
public final class GroupByContext {
private final Collection<OrderByItem> items;
-
- private final int lastIndex;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/engine/GroupByContextEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/engine/GroupByContextEngine.java
index 9c9df1b..7b94dcd 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/engine/GroupByContextEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/engine/GroupByContextEngine.java
@@ -17,8 +17,8 @@
package org.apache.shardingsphere.infra.binder.segment.select.groupby.engine;
-import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
import org.apache.shardingsphere.infra.binder.segment.select.groupby.GroupByContext;
+import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
@@ -39,7 +39,7 @@ public final class GroupByContextEngine {
*/
public GroupByContext createGroupByContext(final SelectStatement selectStatement) {
if (!selectStatement.getGroupBy().isPresent()) {
- return new GroupByContext(new LinkedList<>(), 0);
+ return new GroupByContext(new LinkedList<>());
}
Collection<OrderByItem> groupByItems = new LinkedList<>();
for (OrderByItemSegment each : selectStatement.getGroupBy().get().getGroupByItems()) {
@@ -49,6 +49,6 @@ public final class GroupByContextEngine {
}
groupByItems.add(orderByItem);
}
- return new GroupByContext(groupByItems, selectStatement.getGroupBy().get().getStopIndex());
+ return new GroupByContext(groupByItems);
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java
index 3d1cad5..2f5c691 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java
@@ -49,6 +49,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectState
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
import org.apache.shardingsphere.sql.parser.sql.common.util.WhereSegmentExtractUtils;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.SelectStatementHandler;
import java.util.Collection;
import java.util.LinkedList;
@@ -74,6 +75,8 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
private final PaginationContext paginationContext;
private final boolean containsSubquery;
+
+ private final int generateOrderByStartIndex;
// TODO to be remove, for test case only
public SelectStatementContext(final SelectStatement sqlStatement, final GroupByContext groupByContext,
@@ -85,6 +88,7 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
this.projectionsContext = projectionsContext;
this.paginationContext = paginationContext;
containsSubquery = containsSubquery();
+ generateOrderByStartIndex = generateOrderByStartIndex();
}
public SelectStatementContext(final ShardingSphereSchema schema, final List<Object> parameters, final SelectStatement sqlStatement) {
@@ -95,6 +99,7 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
projectionsContext = new ProjectionsContextEngine(schema).createProjectionsContext(getFromSimpleTableSegments(), getSqlStatement().getProjections(), groupByContext, orderByContext);
paginationContext = new PaginationContextEngine().createPaginationContext(sqlStatement, projectionsContext, parameters);
containsSubquery = containsSubquery();
+ generateOrderByStartIndex = generateOrderByStartIndex();
}
private boolean containsSubquery() {
@@ -107,6 +112,23 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
return false;
}
+ private int generateOrderByStartIndex() {
+ SelectStatement sqlStatement = getSqlStatement();
+ int stopIndex;
+ if (SelectStatementHandler.getWindowSegment(sqlStatement).isPresent()) {
+ stopIndex = SelectStatementHandler.getWindowSegment(sqlStatement).get().getStopIndex();
+ } else if (sqlStatement.getHaving().isPresent()) {
+ stopIndex = sqlStatement.getHaving().get().getStopIndex();
+ } else if (sqlStatement.getGroupBy().isPresent()) {
+ stopIndex = sqlStatement.getGroupBy().get().getStopIndex();
+ } else if (sqlStatement.getWhere().isPresent()) {
+ stopIndex = sqlStatement.getWhere().get().getStopIndex();
+ } else {
+ stopIndex = getAllSimpleTableSegments().stream().mapToInt(SimpleTableSegment::getStopIndex).max().orElse(0);
+ }
+ return stopIndex + 1;
+ }
+
/**
* Whether it contain join query.
*
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/engine/GroupByContextEngineTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/engine/GroupByContextEngineTest.java
index 26d4d24..7c93bad 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/engine/GroupByContextEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/engine/GroupByContextEngineTest.java
@@ -70,7 +70,6 @@ public final class GroupByContextEngineTest {
private void assertCreateGroupByContextWithoutGroupBy(final SelectStatement selectStatement) {
GroupByContext actualGroupByContext = new GroupByContextEngine().createGroupByContext(selectStatement);
assertTrue(actualGroupByContext.getItems().isEmpty());
- assertThat(actualGroupByContext.getLastIndex(), is(0));
}
@Test
@@ -111,6 +110,5 @@ public final class GroupByContextEngineTest {
OrderByItem expectedOrderByItem3 = new OrderByItem(indexOrderByItemSegment2);
expectedOrderByItem3.setIndex(3);
assertThat(actualGroupByContext.getItems(), is(Arrays.asList(expectedOrderByItem1, expectedOrderByItem2, expectedOrderByItem3)));
- assertThat(actualGroupByContext.getLastIndex(), is(10));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/orderby/engine/OrderByContextEngineTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/orderby/engine/OrderByContextEngineTest.java
index 541e53b..79fdebc 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/orderby/engine/OrderByContextEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/orderby/engine/OrderByContextEngineTest.java
@@ -95,7 +95,7 @@ public final class OrderByContextEngineTest {
OrderByItem orderByItem1 = new OrderByItem(new IndexOrderByItemSegment(0, 1, 1, OrderDirection.ASC, OrderDirection.DESC));
OrderByItem orderByItem2 = new OrderByItem(new IndexOrderByItemSegment(1, 2, 2, OrderDirection.ASC, OrderDirection.DESC));
Collection<OrderByItem> orderByItems = Arrays.asList(orderByItem1, orderByItem2);
- GroupByContext groupByContext = new GroupByContext(orderByItems, 0);
+ GroupByContext groupByContext = new GroupByContext(orderByItems);
OrderByContext actualOrderByContext = new OrderByContextEngine().createOrderBy(new ShardingSphereSchema(), selectStatement, groupByContext);
assertThat(actualOrderByContext.getItems(), is(orderByItems));
assertTrue(actualOrderByContext.isGenerated());
@@ -132,7 +132,7 @@ public final class OrderByContextEngineTest {
OrderByItemSegment indexOrderByItemSegment2 = new IndexOrderByItemSegment(2, 3, 3, OrderDirection.ASC, OrderDirection.DESC);
OrderBySegment orderBySegment = new OrderBySegment(0, 1, Arrays.asList(columnOrderByItemSegment, indexOrderByItemSegment1, indexOrderByItemSegment2));
selectStatement.setOrderBy(orderBySegment);
- GroupByContext emptyGroupByContext = new GroupByContext(Collections.emptyList(), 0);
+ GroupByContext emptyGroupByContext = new GroupByContext(Collections.emptyList());
OrderByContext actualOrderByContext = new OrderByContextEngine().createOrderBy(new ShardingSphereSchema(), selectStatement, emptyGroupByContext);
OrderByItem expectedOrderByItem1 = new OrderByItem(columnOrderByItemSegment);
OrderByItem expectedOrderByItem2 = new OrderByItem(indexOrderByItemSegment1);
@@ -176,7 +176,7 @@ public final class OrderByContextEngineTest {
projectionsSegment.setDistinctRow(true);
projectionsSegment.getProjections().addAll(list);
selectStatement.setProjections(projectionsSegment);
- GroupByContext groupByContext = new GroupByContext(Collections.emptyList(), 0);
+ GroupByContext groupByContext = new GroupByContext(Collections.emptyList());
OrderByContext actualOrderByContext = new OrderByContextEngine().createOrderBy(new ShardingSphereSchema(), selectStatement, groupByContext);
assertThat(actualOrderByContext.getItems().size(), is(list.size()));
List<OrderByItem> items = (List<OrderByItem>) actualOrderByContext.getItems();
@@ -189,7 +189,7 @@ public final class OrderByContextEngineTest {
public void assertCreateOrderByContextForMySQLSelectWithoutOrderByOnPlainQuery() {
SelectStatement selectStatement = mock(MySQLSelectStatement.class, RETURNS_DEEP_STUBS);
when(selectStatement.getFrom()).thenReturn(new SimpleTableSegment(0, 1, new IdentifierValue("t_order")));
- GroupByContext groupByContext = new GroupByContext(Collections.emptyList(), 0);
+ GroupByContext groupByContext = new GroupByContext(Collections.emptyList());
OrderByContext actualOrderByContext = new OrderByContextEngine().createOrderBy(getShardingSphereSchemaForMySQLSelectWithoutOrderBy(), selectStatement, groupByContext);
assertTrue(actualOrderByContext.isGenerated());
assertThat(actualOrderByContext.getItems().size(), is(1));
@@ -203,7 +203,7 @@ public final class OrderByContextEngineTest {
when(selectStatement.getFrom()).thenReturn(new SimpleTableSegment(0, 1, new IdentifierValue("t_order")));
when(selectStatement.getOrderBy()).thenReturn(Optional.of(new OrderBySegment(0, 1, Collections.singleton(new ColumnOrderByItemSegment(
new ColumnSegment(0, 1, new IdentifierValue("order_id")), OrderDirection.ASC)))));
- GroupByContext groupByContext = new GroupByContext(Collections.emptyList(), 0);
+ GroupByContext groupByContext = new GroupByContext(Collections.emptyList());
OrderByContext actualOrderByContext = new OrderByContextEngine().createOrderBy(getShardingSphereSchemaForMySQLSelectWithoutOrderBy(), selectStatement, groupByContext);
assertFalse(actualOrderByContext.isGenerated());
assertThat(actualOrderByContext.getItems().size(), is(1));
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
index 23e5e48..baddbe2 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
@@ -88,7 +88,7 @@ public final class ProjectionsContextEngineTest {
Collection<SimpleTableSegment> tables = selectStatementContext.getAllSimpleTableSegments();
ProjectionsSegment projectionsSegment = selectStatement.getProjections();
ProjectionsContext actual = projectionsContextEngine
- .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false));
+ .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false));
assertNotNull(actual);
}
@@ -127,7 +127,7 @@ public final class ProjectionsContextEngineTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(schema, new LinkedList<>(), selectStatement);
Collection<SimpleTableSegment> tables = selectStatementContext.getAllSimpleTableSegments();
ProjectionsContext actual = new ProjectionsContextEngine(schema)
- .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false));
+ .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false));
assertNotNull(actual);
}
@@ -168,7 +168,7 @@ public final class ProjectionsContextEngineTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(schema, new LinkedList<>(), selectStatement);
Collection<SimpleTableSegment> tables = selectStatementContext.getAllSimpleTableSegments();
ProjectionsContext actual = new ProjectionsContextEngine(schema)
- .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList(), 0), orderByContext);
+ .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
assertNotNull(actual);
}
@@ -209,7 +209,7 @@ public final class ProjectionsContextEngineTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(schema, new LinkedList<>(), selectStatement);
Collection<SimpleTableSegment> tables = selectStatementContext.getAllSimpleTableSegments();
ProjectionsContext actual = new ProjectionsContextEngine(schema)
- .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList(), 0), orderByContext);
+ .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
assertNotNull(actual);
}
@@ -251,7 +251,7 @@ public final class ProjectionsContextEngineTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(schema, new LinkedList<>(), selectStatement);
Collection<SimpleTableSegment> tables = selectStatementContext.getAllSimpleTableSegments();
ProjectionsContext actual = new ProjectionsContextEngine(schema)
- .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList(), 0), orderByContext);
+ .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
assertNotNull(actual);
}
@@ -293,7 +293,7 @@ public final class ProjectionsContextEngineTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(schema, new LinkedList<>(), selectStatement);
Collection<SimpleTableSegment> tables = selectStatementContext.getAllSimpleTableSegments();
ProjectionsContext actual = new ProjectionsContextEngine(schema)
- .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList(), 0), orderByContext);
+ .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
assertNotNull(actual);
}
@@ -343,7 +343,7 @@ public final class ProjectionsContextEngineTest {
SelectStatementContext selectStatementContext = new SelectStatementContext(schema, new LinkedList<>(), selectStatement);
Collection<SimpleTableSegment> tables = selectStatementContext.getAllSimpleTableSegments();
ProjectionsContext actual = new ProjectionsContextEngine(schema)
- .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList(), 0), orderByContext);
+ .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
assertNotNull(actual);
}
@@ -385,7 +385,7 @@ public final class ProjectionsContextEngineTest {
ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("name"));
columnSegment.setOwner(new OwnerSegment(0, 0, new IdentifierValue("d")));
OrderByItem groupByItem = new OrderByItem(new ColumnOrderByItemSegment(columnSegment, OrderDirection.ASC));
- GroupByContext groupByContext = new GroupByContext(Collections.singleton(groupByItem), 0);
+ GroupByContext groupByContext = new GroupByContext(Collections.singleton(groupByItem));
SelectStatementContext selectStatementContext = new SelectStatementContext(schema, new LinkedList<>(), selectStatement);
ProjectionsContext actual = new ProjectionsContextEngine(schema)
.createProjectionsContext(selectStatementContext.getFromSimpleTableSegments(), projectionsSegment, groupByContext, new OrderByContext(Collections.emptyList(), false));
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 c2396dc..47d328b 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
@@ -23,9 +23,9 @@ import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByCont
import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
import org.apache.shardingsphere.infra.binder.segment.select.projection.Projection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext;
-import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection;
import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
+import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
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.segment.dml.column.ColumnSegment;
@@ -102,7 +102,7 @@ public final class SelectStatementContextTest {
public void assertSetIndexForItemsByIndexOrderBy(final SelectStatement selectStatement) {
SelectStatementContext selectStatementContext = new SelectStatementContext(
- selectStatement, new GroupByContext(Collections.emptyList(), 0), createOrderBy(INDEX_ORDER_BY), createProjectionsContext(), null);
+ selectStatement, new GroupByContext(Collections.emptyList()), createOrderBy(INDEX_ORDER_BY), createProjectionsContext(), null);
selectStatementContext.setIndexes(Collections.emptyMap());
assertThat(selectStatementContext.getOrderByContext().getItems().iterator().next().getIndex(), is(4));
}
@@ -134,7 +134,7 @@ public final class SelectStatementContextTest {
private void assertSetIndexForItemsByColumnOrderByWithOwner(final SelectStatement selectStatement) {
SelectStatementContext selectStatementContext = new SelectStatementContext(
- selectStatement, new GroupByContext(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITH_OWNER), createProjectionsContext(), null);
+ selectStatement, new GroupByContext(Collections.emptyList()), createOrderBy(COLUMN_ORDER_BY_WITH_OWNER), createProjectionsContext(), null);
selectStatementContext.setIndexes(Collections.emptyMap());
assertThat(selectStatementContext.getOrderByContext().getItems().iterator().next().getIndex(), is(1));
}
@@ -166,7 +166,7 @@ public final class SelectStatementContextTest {
private void assertSetIndexForItemsByColumnOrderByWithAlias(final SelectStatement selectStatement) {
SelectStatementContext selectStatementContext = new SelectStatementContext(
- selectStatement, new GroupByContext(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITH_ALIAS), createProjectionsContext(), null);
+ selectStatement, new GroupByContext(Collections.emptyList()), createOrderBy(COLUMN_ORDER_BY_WITH_ALIAS), createProjectionsContext(), null);
Map<String, Integer> columnLabelIndexMap = new HashMap<>();
columnLabelIndexMap.put("n", 2);
selectStatementContext.setIndexes(columnLabelIndexMap);
@@ -200,7 +200,7 @@ public final class SelectStatementContextTest {
private void assertSetIndexForItemsByColumnOrderByWithoutAlias(final SelectStatement selectStatement) {
SelectStatementContext selectStatementContext = new SelectStatementContext(
- selectStatement, new GroupByContext(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITHOUT_OWNER_ALIAS), createProjectionsContext(), null);
+ selectStatement, new GroupByContext(Collections.emptyList()), createOrderBy(COLUMN_ORDER_BY_WITHOUT_OWNER_ALIAS), createProjectionsContext(), null);
Map<String, Integer> columnLabelIndexMap = new HashMap<>();
columnLabelIndexMap.put("id", 3);
selectStatementContext.setIndexes(columnLabelIndexMap);
@@ -334,7 +334,7 @@ public final class SelectStatementContextTest {
aggregationProjection.getDerivedAggregationProjections().addAll(Collections.singletonList(aggregationProjection));
ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, false, Collections.singletonList(aggregationProjection));
SelectStatementContext selectStatementContext = new SelectStatementContext(
- selectStatement, new GroupByContext(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITHOUT_OWNER_ALIAS), projectionsContext, null);
+ selectStatement, new GroupByContext(Collections.emptyList()), createOrderBy(COLUMN_ORDER_BY_WITHOUT_OWNER_ALIAS), projectionsContext, null);
Map<String, Integer> columnLabelIndexMap = new HashMap<>();
columnLabelIndexMap.put("id", 3);
selectStatementContext.setIndexes(columnLabelIndexMap);
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index 8518259..26bc654 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -889,7 +889,7 @@ overClause
;
windowSpecification
- : LP_ identifier? (PARTITION BY expr (COMMA_ expr)*)? orderByClause? frameClause? RP_
+ : identifier? (PARTITION BY expr (COMMA_ expr)*)? orderByClause? frameClause?
;
frameClause
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
index 2559bef..19841ec 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/impl/MySQLStatementSQLVisitor.java
@@ -24,33 +24,64 @@ import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementBaseVisitor;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AggregationFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AliasContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AssignmentContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AssignmentValueContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AssignmentValuesContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BitExprContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BitValueLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BlobValueContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BooleanLiteralsContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BooleanPrimaryContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CastFunctionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CharFunctionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnNameContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnNamesContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnRefContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ConvertFunctionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DataTypeContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ConstraintNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DeleteContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DuplicateSpecificationContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.EscapedTableReferenceContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExprContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExtractFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FieldLengthContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FieldsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FromClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FunctionCallContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.GroupByClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.GroupConcatFunctionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.HavingClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.HexadecimalLiteralsContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IdentifierContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IndexNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.InsertContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.InsertIdentifierContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.InsertSelectClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.InsertValuesClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IntervalExpressionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.JoinSpecificationContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.JoinedTableContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitOffsetContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LimitRowCountContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LockClauseListContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.MatchExpressionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.MultipleTablesClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NullValueLiteralsContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NumberLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OnDuplicateKeyClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OrderByClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OrderByItemContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OwnerContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ParameterMarkerContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.PositionFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.PrecisionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.PredicateContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ProjectionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ProjectionsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.QualifiedShorthandContext;
@@ -59,64 +90,35 @@ import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.QueryEx
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.QueryExpressionParensContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.QueryPrimaryContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.QuerySpecificationContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.RegularFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ReplaceContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ReplaceSelectClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ReplaceValuesClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SchemaNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SelectContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SelectSpecificationContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SelectWithIntoContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SetAssignmentsClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SimpleExprContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SingleTableClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SpecialFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.StringLiteralsContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NumberLiteralsContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SubqueryContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SubstringFunctionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableAliasRefListContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableFactorContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableListContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableReferenceContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableReferencesContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TemporalLiteralsContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.HexadecimalLiteralsContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BitValueLiteralsContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BooleanLiteralsContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableNameContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NullValueLiteralsContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ParameterMarkerContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LiteralsContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IdentifierContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SchemaNameContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OwnerContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.UpdateContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ViewNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ViewNamesContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnNameContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnNamesContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IndexNameContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExprContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BooleanPrimaryContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.PredicateContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SimpleExprContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SelectWithIntoContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableAliasRefListContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BitExprContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SubqueryContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IntervalExpressionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AggregationFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FunctionCallContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SpecialFunctionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WeightStringFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WhereClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WindowFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.GroupConcatFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CastFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ConvertFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SubstringFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.PositionFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExtractFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WeightStringFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CharFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.RegularFunctionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.MatchExpressionContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DataTypeContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OrderByClauseContext;
-import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OrderByItemContext;
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.segment.ddl.constraint.ConstraintSegment;
@@ -157,12 +159,14 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.Pa
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
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.DataTypeLengthSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.DeleteMultiTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.JoinTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
@@ -607,10 +611,27 @@ public abstract class MySQLStatementSQLVisitor extends MySQLStatementBaseVisitor
if (null != ctx.groupByClause()) {
result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
}
+ if (null != ctx.havingClause()) {
+ result.setHaving((HavingSegment) visit(ctx.havingClause()));
+ }
+ if (null != ctx.windowClause()) {
+ result.setWindow((WindowSegment) visit(ctx.windowClause()));
+ }
return result;
}
@Override
+ public ASTNode visitWindowClause(final WindowClauseContext ctx) {
+ return new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
+ }
+
+ @Override
+ public ASTNode visitHavingClause(final HavingClauseContext ctx) {
+ ExpressionSegment expr = (ExpressionSegment) visit(ctx.expr());
+ return new HavingSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), expr);
+ }
+
+ @Override
public final ASTNode visitIntervalExpression(final IntervalExpressionContext ctx) {
calculateParameterCount(Collections.singleton(ctx.intervalValue().expr()));
return new ExpressionProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText());
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDMLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDMLStatementSQLVisitor.java
index 927ce18..1228b1b 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDMLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDMLStatementSQLVisitor.java
@@ -36,6 +36,7 @@ import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.ForUpd
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.ForUpdateClauseListContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.FromClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.GroupByClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.HavingClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.InsertContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.InsertValuesClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.IntoClauseContext;
@@ -89,6 +90,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.Subquery
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.GroupBySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.OrderBySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
@@ -296,10 +298,19 @@ public final class OracleDMLStatementSQLVisitor extends OracleStatementSQLVisito
if (null != ctx.groupByClause()) {
result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
}
+ if (null != ctx.havingClause()) {
+ result.setHaving((HavingSegment) visit(ctx.havingClause()));
+ }
return result;
}
@Override
+ public ASTNode visitHavingClause(final HavingClauseContext ctx) {
+ ExpressionSegment expr = (ExpressionSegment) visit(ctx.expr());
+ return new HavingSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), expr);
+ }
+
+ @Override
public ASTNode visitSelectUnionClause(final SelectUnionClauseContext ctx) {
OracleSelectStatement result;
if (null != ctx.queryBlock()) {
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
index e75e355..f10447f 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLStatementSQLVisitor.java
@@ -52,6 +52,7 @@ import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.Fu
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.FunctionExprCommonSubexprContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.GroupByItemContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.GroupClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.HavingClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.IdentifierContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.InExprContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.IndexNameContext;
@@ -94,6 +95,7 @@ import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.Up
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ValuesClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WhereClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WhereOrCurrentClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.WindowClauseContext;
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.segment.ddl.constraint.ConstraintSegment;
@@ -131,12 +133,14 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.li
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
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.DataTypeLengthSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
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.SubqueryTableSegment;
@@ -780,10 +784,27 @@ public abstract class PostgreSQLStatementSQLVisitor extends PostgreSQLStatementB
if (null != ctx.groupClause()) {
result.setGroupBy((GroupBySegment) visit(ctx.groupClause()));
}
+ if (null != ctx.havingClause()) {
+ result.setHaving((HavingSegment) visit(ctx.havingClause()));
+ }
+ if (null != ctx.windowClause()) {
+ result.setWindow((WindowSegment) visit(ctx.windowClause()));
+ }
return result;
}
@Override
+ public ASTNode visitHavingClause(final HavingClauseContext ctx) {
+ ExpressionSegment expr = (ExpressionSegment) visit(ctx.aExpr());
+ return new HavingSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), expr);
+ }
+
+ @Override
+ public ASTNode visitWindowClause(final WindowClauseContext ctx) {
+ return new WindowSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
+ }
+
+ @Override
public ASTNode visitGroupClause(final GroupClauseContext ctx) {
Collection<OrderByItemSegment> items = new LinkedList<>();
for (GroupByItemContext each : ctx.groupByList().groupByItem()) {
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92DMLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92DMLStatementSQLVisitor.java
index 0e13a79..ce9b0d2 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92DMLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sql92/src/main/java/org/apache/shardingsphere/sql/parser/sql92/visitor/statement/impl/SQL92DMLStatementSQLVisitor.java
@@ -18,8 +18,8 @@
package org.apache.shardingsphere.sql.parser.sql92.visitor.statement.impl;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.sql.parser.api.visitor.operation.SQLStatementVisitor;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
+import org.apache.shardingsphere.sql.parser.api.visitor.operation.SQLStatementVisitor;
import org.apache.shardingsphere.sql.parser.api.visitor.type.DMLSQLVisitor;
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.AliasContext;
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.AssignmentContext;
@@ -33,6 +33,7 @@ import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.Escaped
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.ExprContext;
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.FromClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.GroupByClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.HavingClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.InsertContext;
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.InsertValuesClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.JoinSpecificationContext;
@@ -74,6 +75,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.Subquery
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.GroupBySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.OrderBySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
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;
@@ -236,9 +238,18 @@ public final class SQL92DMLStatementSQLVisitor extends SQL92StatementSQLVisitor
if (null != ctx.orderByClause()) {
result.setOrderBy((OrderBySegment) visit(ctx.orderByClause()));
}
+ if (null != ctx.havingClause()) {
+ result.setHaving((HavingSegment) visit(ctx.havingClause()));
+ }
return result;
}
-
+
+ @Override
+ public ASTNode visitHavingClause(final HavingClauseContext ctx) {
+ ExpressionSegment expr = (ExpressionSegment) visit(ctx.expr());
+ return new HavingSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), expr);
+ }
+
private boolean isDistinct(final SelectSpecificationContext ctx) {
return ((BooleanLiteralValue) visit(ctx.duplicateSpecification())).getValue();
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerDMLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerDMLStatementSQLVisitor.java
index 7d05df0..1b6ae15 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerDMLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-sqlserver/src/main/java/org/apache/shardingsphere/sql/parser/sqlserver/visitor/statement/impl/SQLServerDMLStatementSQLVisitor.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.Dup
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.ExprContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.FromClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.GroupByClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.HavingClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertDefaultValueContext;
import org.apache.shardingsphere.sql.parser.autogen.SQLServerStatementParser.InsertSelectClauseContext;
@@ -95,6 +96,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.ro
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.rownum.ParameterMarkerRowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.rownum.RowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.top.TopProjectionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
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.OutputSegment;
@@ -381,12 +383,21 @@ public final class SQLServerDMLStatementSQLVisitor extends SQLServerStatementSQL
if (null != ctx.groupByClause()) {
result.setGroupBy((GroupBySegment) visit(ctx.groupByClause()));
}
+ if (null != ctx.havingClause()) {
+ result.setHaving((HavingSegment) visit(ctx.havingClause()));
+ }
if (null != ctx.orderByClause()) {
result = visitOrderBy(result, ctx.orderByClause());
}
return result;
}
+ @Override
+ public ASTNode visitHavingClause(final HavingClauseContext ctx) {
+ ExpressionSegment expr = (ExpressionSegment) visit(ctx.expr());
+ return new HavingSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), expr);
+ }
+
private SQLServerSelectStatement visitOrderBy(final SQLServerSelectStatement selectStatement, final OrderByClauseContext ctx) {
Collection<OrderByItemSegment> items = new LinkedList<>();
int orderByStartIndex = ctx.start.getStartIndex();
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/predicate/HavingSegment.java
similarity index 66%
copy from shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
copy to shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/predicate/HavingSegment.java
index 97a57e3..0f8cb4b 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/dml/predicate/HavingSegment.java
@@ -15,22 +15,25 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.binder.segment.select.groupby;
+package org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
-
-import java.util.Collection;
+import lombok.Setter;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
/**
- * Group by context.
+ * Having segment.
*/
@RequiredArgsConstructor
@Getter
-public final class GroupByContext {
+@Setter
+public final class HavingSegment implements SQLSegment {
+
+ private final int startIndex;
- private final Collection<OrderByItem> items;
+ private final int stopIndex;
- private final int lastIndex;
+ private final ExpressionSegment expr;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/generic/WindowSegment.java
similarity index 74%
copy from shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
copy to shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/generic/WindowSegment.java
index 97a57e3..2d375d7 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/generic/WindowSegment.java
@@ -15,22 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.binder.segment.select.groupby;
+package org.apache.shardingsphere.sql.parser.sql.common.segment.generic;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
-
-import java.util.Collection;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
/**
- * Group by context.
+ * Window segment.
*/
@RequiredArgsConstructor
@Getter
-public final class GroupByContext {
+public final class WindowSegment implements SQLSegment {
- private final Collection<OrderByItem> items;
+ private final int startIndex;
- private final int lastIndex;
+ private final int stopIndex;
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/SelectStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/SelectStatement.java
index cdef8b5..f0eb050 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/SelectStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/dml/SelectStatement.java
@@ -23,6 +23,7 @@ import lombok.ToString;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.GroupBySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.OrderBySegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
@@ -45,6 +46,8 @@ public abstract class SelectStatement extends AbstractSQLStatement implements DM
private GroupBySegment groupBy;
+ private HavingSegment having;
+
private OrderBySegment orderBy;
/**
@@ -66,6 +69,15 @@ public abstract class SelectStatement extends AbstractSQLStatement implements DM
}
/**
+ * Get having segment.
+ *
+ * @return having segment
+ */
+ public Optional<HavingSegment> getHaving() {
+ return Optional.ofNullable(having);
+ }
+
+ /**
* Get order by segment.
*
* @return order by segment
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandler.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandler.java
index 3afb3e6..b828476 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandler.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandler.java
@@ -21,6 +21,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.SQLStatementHandler;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
@@ -82,4 +83,20 @@ public final class SelectStatementHandler implements SQLStatementHandler {
}
return Optional.empty();
}
+
+ /**
+ * Get window segment.
+ *
+ * @param selectStatement select statement
+ * @return window segment
+ */
+ public static Optional<WindowSegment> getWindowSegment(final SelectStatement selectStatement) {
+ if (selectStatement instanceof MySQLStatement) {
+ return ((MySQLSelectStatement) selectStatement).getWindow();
+ }
+ if (selectStatement instanceof PostgreSQLStatement) {
+ return ((PostgreSQLSelectStatement) selectStatement).getWindow();
+ }
+ return Optional.empty();
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLSelectStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLSelectStatement.java
index 8f5ed95..c08a011 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLSelectStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLSelectStatement.java
@@ -21,6 +21,7 @@ import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
@@ -37,6 +38,8 @@ public final class MySQLSelectStatement extends SelectStatement implements MySQL
private LockSegment lock;
+ private WindowSegment window;
+
/**
* Get order by segment.
*
@@ -54,4 +57,13 @@ public final class MySQLSelectStatement extends SelectStatement implements MySQL
public Optional<LockSegment> getLock() {
return Optional.ofNullable(lock);
}
+
+ /**
+ * Get window segment.
+ *
+ * @return window segment
+ */
+ public Optional<WindowSegment> getWindow() {
+ return Optional.ofNullable(window);
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLSelectStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLSelectStatement.java
index 6255ad6..d638b78 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLSelectStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/dml/PostgreSQLSelectStatement.java
@@ -21,6 +21,7 @@ import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;
@@ -36,7 +37,9 @@ public final class PostgreSQLSelectStatement extends SelectStatement implements
private LimitSegment limit;
private LockSegment lock;
-
+
+ private WindowSegment window;
+
/**
* Get order by segment.
*
@@ -54,4 +57,13 @@ public final class PostgreSQLSelectStatement extends SelectStatement implements
public Optional<LockSegment> getLock() {
return Optional.ofNullable(lock);
}
+
+ /**
+ * Get window segment.
+ *
+ * @return window segment
+ */
+ public Optional<WindowSegment> getWindow() {
+ return Optional.ofNullable(window);
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandlerTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandlerTest.java
index de1e398..cc14694 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandlerTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/dml/SelectStatementHandlerTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.dml.OracleSelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLSelectStatement;
@@ -137,4 +138,34 @@ public final class SelectStatementHandlerTest {
Optional<LockSegment> lockSegment = SelectStatementHandler.getLockSegment(selectStatement);
assertFalse(lockSegment.isPresent());
}
+
+ @Test
+ public void assertGetWindowSegmentWithWindowSegmentForMySQL() {
+ MySQLSelectStatement selectStatement = new MySQLSelectStatement();
+ selectStatement.setWindow(new WindowSegment(0, 0));
+ Optional<WindowSegment> windowSegment = SelectStatementHandler.getWindowSegment(selectStatement);
+ assertTrue(windowSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetWindowSegmentWithoutWindowSegmentForMySQL() {
+ MySQLSelectStatement selectStatement = new MySQLSelectStatement();
+ Optional<WindowSegment> windowSegment = SelectStatementHandler.getWindowSegment(selectStatement);
+ assertFalse(windowSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetWindowSegmentWithWindowSegmentForPostgreSQL() {
+ PostgreSQLSelectStatement selectStatement = new PostgreSQLSelectStatement();
+ selectStatement.setWindow(new WindowSegment(0, 0));
+ Optional<WindowSegment> windowSegment = SelectStatementHandler.getWindowSegment(selectStatement);
+ assertTrue(windowSegment.isPresent());
+ }
+
+ @Test
+ public void assertGetWindowSegmentWithoutWindowSegmentForPostgreSQL() {
+ PostgreSQLSelectStatement selectStatement = new PostgreSQLSelectStatement();
+ Optional<WindowSegment> windowSegment = SelectStatementHandler.getWindowSegment(selectStatement);
+ assertFalse(windowSegment.isPresent());
+ }
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/having/HavingClauseAssert.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/having/HavingClauseAssert.java
new file mode 100644
index 0000000..18fd09f
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/segment/having/HavingClauseAssert.java
@@ -0,0 +1,45 @@
+/*
+ * 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.sql.parser.parameterized.asserts.segment.having;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.HavingSegment;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.SQLSegmentAssert;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.expression.ExpressionAssert;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.having.ExpectedHavingClause;
+
+/**
+ * Having clause assert.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class HavingClauseAssert {
+
+ /**
+ * Assert actual having segment is correct with expected having clause.
+ *
+ * @param assertContext assert context
+ * @param actual actual having segment
+ * @param expected expected having clause
+ */
+ public static void assertIs(final SQLCaseAssertContext assertContext, final HavingSegment actual, final ExpectedHavingClause expected) {
+ ExpressionAssert.assertExpression(assertContext, actual.getExpr(), expected.getExpr());
+ SQLSegmentAssert.assertIs(assertContext, actual, expected);
+ }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/SelectStatementAssert.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/SelectStatementAssert.java
index e00f0cd..f964e3f 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/SelectStatementAssert.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/dml/impl/SelectStatementAssert.java
@@ -21,11 +21,13 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.LockSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.SelectStatementHandler;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.SQLSegmentAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.groupby.GroupByClauseAssert;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.having.HavingClauseAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.limit.LimitClauseAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.lock.LockClauseAssert;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.orderby.OrderByClauseAssert;
@@ -57,12 +59,33 @@ public final class SelectStatementAssert {
assertProjection(assertContext, actual, expected);
assertWhereClause(assertContext, actual, expected);
assertGroupByClause(assertContext, actual, expected);
+ assertHavingClause(assertContext, actual, expected);
+ assertWindowClause(assertContext, actual, expected);
assertOrderByClause(assertContext, actual, expected);
assertLimitClause(assertContext, actual, expected);
assertTable(assertContext, actual, expected);
assertLockClause(assertContext, actual, expected);
}
+ private static void assertWindowClause(final SQLCaseAssertContext assertContext, final SelectStatement actual, final SelectStatementTestCase expected) {
+ Optional<WindowSegment> windowSegment = SelectStatementHandler.getWindowSegment(actual);
+ if (null != expected.getWindowClause()) {
+ assertTrue(assertContext.getText("Actual window segment should exist."), windowSegment.isPresent());
+ SQLSegmentAssert.assertIs(assertContext, windowSegment.get(), expected.getWindowClause());
+ } else {
+ assertFalse(assertContext.getText("Actual window segment should not exist."), windowSegment.isPresent());
+ }
+ }
+
+ private static void assertHavingClause(final SQLCaseAssertContext assertContext, final SelectStatement actual, final SelectStatementTestCase expected) {
+ if (null != expected.getHavingClause()) {
+ assertTrue(assertContext.getText("Actual having segment should exist."), actual.getHaving().isPresent());
+ HavingClauseAssert.assertIs(assertContext, actual.getHaving().get(), expected.getHavingClause());
+ } else {
+ assertFalse(assertContext.getText("Actual having segment should not exist."), actual.getHaving().isPresent());
+ }
+ }
+
private static void assertProjection(final SQLCaseAssertContext assertContext, final SelectStatement actual, final SelectStatementTestCase expected) {
if (null == actual.getProjections() && 0 == expected.getProjections().getSize()) {
return;
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/having/ExpectedHavingClause.java
similarity index 53%
copy from shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
copy to shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/having/ExpectedHavingClause.java
index 97a57e3..a0b0263 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/having/ExpectedHavingClause.java
@@ -15,22 +15,25 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.binder.segment.select.groupby;
+package org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.having;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
+import lombok.Setter;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.AbstractExpectedSQLSegment;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.expr.ExpectedExpression;
-import java.util.Collection;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
/**
- * Group by context.
+ * Expected having clause.
*/
-@RequiredArgsConstructor
@Getter
-public final class GroupByContext {
-
- private final Collection<OrderByItem> items;
-
- private final int lastIndex;
+@Setter
+@XmlAccessorType(XmlAccessType.FIELD)
+public final class ExpectedHavingClause extends AbstractExpectedSQLSegment {
+
+ @XmlElement(name = "expr")
+ private ExpectedExpression expr;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/window/ExpectedWindowClause.java
similarity index 63%
copy from shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
copy to shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/window/ExpectedWindowClause.java
index 97a57e3..a62629c 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/groupby/GroupByContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/segment/impl/window/ExpectedWindowClause.java
@@ -15,22 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.binder.segment.select.groupby;
+package org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.window;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
+import lombok.Setter;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.AbstractExpectedSQLSegment;
-import java.util.Collection;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
/**
- * Group by context.
+ * Expected window clause.
*/
-@RequiredArgsConstructor
@Getter
-public final class GroupByContext {
-
- private final Collection<OrderByItem> items;
-
- private final int lastIndex;
+@Setter
+@XmlAccessorType(XmlAccessType.FIELD)
+public final class ExpectedWindowClause extends AbstractExpectedSQLSegment {
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dml/SelectStatementTestCase.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dml/SelectStatementTestCase.java
index 43104c5..0826892 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dml/SelectStatementTestCase.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/dml/SelectStatementTestCase.java
@@ -19,12 +19,14 @@ package org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domai
import lombok.Getter;
import lombok.Setter;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.having.ExpectedHavingClause;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.limit.ExpectedLimitClause;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.lock.ExpectedLockClause;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.orderby.ExpectedOrderByClause;
-import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.where.ExpectedWhereClause;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.projection.ExpectedProjections;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.table.ExpectedTable;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.where.ExpectedWhereClause;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.window.ExpectedWindowClause;
import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.SQLParserTestCase;
import javax.xml.bind.annotation.XmlElement;
@@ -51,6 +53,12 @@ public final class SelectStatementTestCase extends SQLParserTestCase {
@XmlElement(name = "order-by")
private ExpectedOrderByClause orderByClause;
+ @XmlElement(name = "having")
+ private ExpectedHavingClause havingClause;
+
+ @XmlElement(name = "window")
+ private ExpectedWindowClause windowClause;
+
@XmlElement(name = "limit")
private ExpectedLimitClause limitClause;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dml/select-group-by.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dml/select-group-by.xml
index da67af9..9c0604d 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dml/select-group-by.xml
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/case/dml/select-group-by.xml
@@ -337,4 +337,57 @@
<index-item index="2" start-index="81" stop-index="81" />
</order-by>
</select>
+
+ <select sql-case-id="select_group_by_with_having">
+ <from>
+ <simple-table name="t_order" start-index="53" stop-index="59" />
+ </from>
+ <projections start-index="7" stop-index="46">
+ <aggregation-projection type="COUNT" alias="orders_count" inner-expression="(order_id)" start-index="7" stop-index="21" />
+ <column-projection name="user_id" start-index="40" stop-index="46" />
+ </projections>
+ <group-by>
+ <column-item name="user_id" start-index="70" stop-index="76" />
+ </group-by>
+ <having start-index="78" stop-index="100">
+ <expr>
+ <binary-operation-expression start-index="85" stop-index="100">
+ <left>
+ <column name="orders_count" start-index="85" stop-index="96" />
+ </left>
+ <operator>></operator>
+ <right>
+ <literal-expression start-index="100" stop-index="100" value="0"/>
+ </right>
+ </binary-operation-expression>
+ </expr>
+ </having>
+ </select>
+
+ <select sql-case-id="select_group_by_with_having_and_window">
+ <from>
+ <simple-table name="t_order" start-index="53" stop-index="59" />
+ </from>
+ <projections start-index="7" stop-index="46">
+ <aggregation-projection type="COUNT" alias="orders_count" inner-expression="(order_id)" start-index="7" stop-index="21" />
+ <column-projection name="user_id" start-index="40" stop-index="46" />
+ </projections>
+ <group-by>
+ <column-item name="user_id" start-index="70" stop-index="76" />
+ </group-by>
+ <having start-index="78" stop-index="100">
+ <expr>
+ <binary-operation-expression start-index="85" stop-index="100">
+ <left>
+ <column name="orders_count" start-index="85" stop-index="96" />
+ </left>
+ <operator>></operator>
+ <right>
+ <literal-expression start-index="100" stop-index="100" value="0" />
+ </right>
+ </binary-operation-expression>
+ </expr>
+ </having>
+ <window start-index="102" stop-index="135" />
+ </select>
</sql-parser-test-cases>
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dml/select-group-by.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dml/select-group-by.xml
index 1fc22ee..382835d 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dml/select-group-by.xml
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/sql/supported/dml/select-group-by.xml
@@ -30,4 +30,6 @@
<sql-case id="select_group_by_with_date_function" value="SELECT date_format(creation_date, '%y-%m-%d') as creation_date, count(*) as c_number FROM `t_order_item` WHERE order_id in (?, ?) GROUP BY date_format(creation_date, '%y-%m-%d')" db-types="MySQL"/>
<sql-case id="select_group_by_with_keyword_alias" value="SELECT SUM(order_id) AS orders_sum, user_id as `key` FROM t_order GROUP BY `key`" db-types="MySQL" />
<sql-case id="select_group_by_with_count_without_column_name" value="SELECT COUNT(order_id) AS orders_count, user_id FROM t_order GROUP BY 2 ORDER BY 2" db-types="MySQL,Oracle,SQLServer,PostgreSQL" />
+ <sql-case id="select_group_by_with_having" value="SELECT COUNT(order_id) AS orders_count, user_id FROM t_order GROUP BY user_id HAVING orders_count > 0" />
+ <sql-case id="select_group_by_with_having_and_window" value="SELECT COUNT(order_id) AS orders_count, user_id FROM t_order GROUP BY user_id HAVING orders_count > 0 WINDOW w AS (PARTITION BY user_id)" db-types="MySQL,PostgreSQL" />
</sql-cases>