From 3f28158cecd248ad95e9fb87ba77679ed6cf80f4 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 6 Jun 2019 02:07:54 +0800 Subject: [PATCH] for #2441, add RowNumberValueSegment --- .../RowNumberDecoratorMergedResult.java | 6 +- .../TopAndRowNumberDecoratorMergedResult.java | 4 +- .../core/merge/dql/DQLMergeEngineTest.java | 34 +++++------ .../LimitDecoratorMergedResultTest.java | 12 ++-- .../RowNumberDecoratorMergedResultTest.java | 14 +++-- ...AndRowNumberDecoratorMergedResultTest.java | 20 +++---- .../impl/dml/select/LimitExtractor.java | 18 +++--- .../item/impl/TopSelectItemExtractor.java | 12 ++-- .../parse/filler/common/dml/LimitFiller.java | 8 +-- .../filler/common/dml/SelectItemFiller.java | 6 +- .../ShardingRowNumberPredicateFiller.java | 32 +++++----- .../NumberLiteralPaginationValueSegment.java | 35 +++++++++++ .../dml/pagination/PaginationSegment.java | 59 +++++++++++++++++++ .../pagination/PaginationValueSegment.java | 39 ++++++++++++ ...ParameterMarkerPaginationValueSegment.java | 35 +++++++++++ .../pagination/limit/LimitValueSegment.java | 33 +++++++++++ .../limit/NumberLiteralLimitValueSegment.java | 37 ++++++++++++ .../ParameterMarkerLimitValueSegment.java | 37 ++++++++++++ .../NumberLiteralRowNumberValueSegment.java | 37 ++++++++++++ .../ParameterMarkerRowNumberValueSegment.java | 37 ++++++++++++ .../rownum/RowNumberValueSegment.java | 32 ++++++++++ .../{limit => pagination/top}/TopSegment.java | 3 +- .../sql/statement/dml/SelectStatement.java | 4 +- .../filler-rule-definition.xml | 2 +- .../integrate/asserts/SQLStatementAssert.java | 8 +-- ...LimitAssert.java => PaginationAssert.java} | 36 +++++------ .../token/generator/OffsetTokenGenerator.java | 15 ++--- .../generator/RowCountTokenGenerator.java | 15 ++--- .../core/rewrite/token/pojo/OffsetToken.java | 2 +- .../rewrite/token/pojo/RowCountToken.java | 2 +- .../rewriter/ShardingSQLRewriterTest.java | 43 +++++++------- .../core/route/limit/Limit.java | 28 +++++---- .../core/route/limit/LimitValue.java | 4 +- .../router/sharding/ParsingSQLRouter.java | 4 +- .../core/route/DatabaseTest.java | 34 +++++------ 35 files changed, 567 insertions(+), 180 deletions(-) create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/NumberLiteralPaginationValueSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/PaginationSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/PaginationValueSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/ParameterMarkerPaginationValueSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/LimitValueSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/NumberLiteralLimitValueSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/ParameterMarkerLimitValueSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/NumberLiteralRowNumberValueSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/ParameterMarkerRowNumberValueSegment.java create mode 100644 sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/RowNumberValueSegment.java rename sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/{limit => pagination/top}/TopSegment.java (88%) rename sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/limit/{LimitAssert.java => PaginationAssert.java} (52%) diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResult.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResult.java index 4b84d50c5c..d43bed48e4 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResult.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResult.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.route.limit.Limit; import java.sql.SQLException; /** - * Decorator merged result for rownum pagination. + * Decorator merged result for row number pagination. * * @author zhangliang */ @@ -47,7 +47,7 @@ public final class RowNumberDecoratorMergedResult extends DecoratorMergedResult if (null == limit.getOffset()) { end = 0; } else { - end = limit.getOffset().getLimitValueSegment().isBoundOpened() ? limit.getOffsetValue() - 1 : limit.getOffsetValue(); + end = limit.getOffset().getPaginationValueSegment().isBoundOpened() ? limit.getOffsetValue() - 1 : limit.getOffsetValue(); } for (int i = 0; i < end; i++) { if (!getMergedResult().next()) { @@ -66,7 +66,7 @@ public final class RowNumberDecoratorMergedResult extends DecoratorMergedResult if (limit.getRowCountValue() < 0) { return getMergedResult().next(); } - if (limit.getRowCount().getLimitValueSegment().isBoundOpened()) { + if (limit.getRowCount().getPaginationValueSegment().isBoundOpened()) { return rowNumber++ <= limit.getRowCountValue() && getMergedResult().next(); } return rowNumber++ < limit.getRowCountValue() && getMergedResult().next(); diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResult.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResult.java index 020cd33324..733d3b50c5 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResult.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResult.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.route.limit.Limit; import java.sql.SQLException; /** - * Decorator merged result for top and rownum pagination. + * Decorator merged result for top and row number pagination. * * @author zhangliang */ @@ -47,7 +47,7 @@ public final class TopAndRowNumberDecoratorMergedResult extends DecoratorMergedR if (null == limit.getOffset()) { end = 0; } else { - end = limit.getOffset().getLimitValueSegment().isBoundOpened() ? limit.getOffsetValue() - 1 : limit.getOffsetValue(); + end = limit.getOffset().getPaginationValueSegment().isBoundOpened() ? limit.getOffsetValue() - 1 : limit.getOffsetValue(); } for (int i = 0; i < end; i++) { if (!getMergedResult().next()) { diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngineTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngineTest.java index e7119c12a3..01ca3e4d02 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngineTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngineTest.java @@ -33,8 +33,8 @@ import org.apache.shardingsphere.core.merge.dql.pagination.RowNumberDecoratorMer import org.apache.shardingsphere.core.merge.dql.pagination.TopAndRowNumberDecoratorMergedResult; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; import org.apache.shardingsphere.core.parse.sql.context.selectitem.AggregationSelectItem; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.route.limit.Limit; @@ -91,14 +91,14 @@ public final class DQLMergeEngineTest { @Test public void assertBuildIteratorStreamMergedResultWithLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, singleQueryResult); assertThat(mergeEngine.merge(), instanceOf(IteratorStreamMergedResult.class)); } @Test public void assertBuildIteratorStreamMergedResultWithMySQLLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(LimitDecoratorMergedResult.class)); @@ -107,7 +107,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildIteratorStreamMergedResultWithOracleLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(RowNumberDecoratorMergedResult.class)); @@ -116,7 +116,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildIteratorStreamMergedResultWithSQLServerLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class)); @@ -132,7 +132,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildOrderByStreamMergedResultWithMySQLLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); @@ -142,7 +142,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildOrderByStreamMergedResultWithOracleLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); @@ -152,7 +152,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildOrderByStreamMergedResultWithSQLServerLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); @@ -170,7 +170,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByStreamMergedResultWithMySQLLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults); @@ -181,7 +181,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByStreamMergedResultWithOracleLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); @@ -192,7 +192,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByStreamMergedResultWithSQLServerLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); @@ -210,7 +210,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithMySQLLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); @@ -220,7 +220,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithOracleLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); selectStatement.getOrderByItems().add(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); @@ -231,7 +231,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithSQLServerLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)); selectStatement.getGroupByItems().add(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); @@ -249,7 +249,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithMySQLLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.absent())); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); @@ -259,7 +259,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithOracleLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.absent())); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); @@ -269,7 +269,7 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithSQLServerLimit() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); selectStatement.getItems().add(new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.absent())); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResultTest.java index 5a9f462eea..6127eb7905 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResultTest.java @@ -23,8 +23,8 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.route.limit.Limit; @@ -66,12 +66,12 @@ public final class LimitDecoratorMergedResultTest { } SelectStatement selectStatement = new SelectStatement(); routeResult = new SQLRouteResult(selectStatement); - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); } @Test public void assertNextForSkipAll() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE), null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertFalse(actual.next()); @@ -79,7 +79,7 @@ public final class LimitDecoratorMergedResultTest { @Test public void assertNextWithoutRowCount() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2), null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); for (int i = 0; i < 6; i++) { @@ -90,7 +90,7 @@ public final class LimitDecoratorMergedResultTest { @Test public void assertNextWithRowCount() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 2, false)), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2), new NumberLiteralLimitValueSegment(0, 0, 2)), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.MySQL, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java index 6f5cb06374..1caf528601 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java @@ -23,8 +23,8 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.route.limit.Limit; @@ -69,7 +69,7 @@ public final class RowNumberDecoratorMergedResultTest { @Test public void assertNextForSkipAll() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertFalse(actual.next()); @@ -77,7 +77,7 @@ public final class RowNumberDecoratorMergedResultTest { @Test public void assertNextWithoutOffsetWithoutRowCount() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); for (int i = 0; i < 8; i++) { @@ -88,7 +88,8 @@ public final class RowNumberDecoratorMergedResultTest { @Test public void assertNextForRowCountBoundOpenedFalse() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4, false)), Collections.emptyList())); + routeResult.setLimit(new Limit( + new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, false)), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); @@ -98,7 +99,8 @@ public final class RowNumberDecoratorMergedResultTest { @Test public void assertNextForRowCountBoundOpenedTrue() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4, true)), Collections.emptyList())); + routeResult.setLimit(new Limit( + new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, true)), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.Oracle, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java index 0546286d90..077f9b38e1 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java @@ -23,8 +23,9 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.route.limit.Limit; @@ -49,8 +50,6 @@ public final class TopAndRowNumberDecoratorMergedResultTest { private List queryResults; - private SelectStatement selectStatement; - private SQLRouteResult routeResult; @Before @@ -66,13 +65,12 @@ public final class TopAndRowNumberDecoratorMergedResultTest { for (ResultSet each : resultSets) { queryResults.add(new TestQueryResult(each)); } - selectStatement = new SelectStatement(); - routeResult = new SQLRouteResult(selectStatement); + routeResult = new SQLRouteResult(new SelectStatement()); } @Test public void assertNextForSkipAll() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertFalse(actual.next()); @@ -80,7 +78,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextWithoutOffsetWithRowCount() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, null, new NumberLiteralLimitValueSegment(0, 0, 5, false)), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, null, new NumberLiteralLimitValueSegment(0, 0, 5)), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); for (int i = 0; i < 5; i++) { @@ -91,7 +89,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextWithOffsetWithoutRowCount() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), null), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, true), null), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); for (int i = 0; i < 7; i++) { @@ -102,7 +100,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextWithOffsetBoundOpenedFalse() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, false), new NumberLiteralLimitValueSegment(0, 0, 4, false)), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, false), new NumberLiteralLimitValueSegment(0, 0, 4)), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); @@ -112,7 +110,7 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextWithOffsetBoundOpenedTrue() throws SQLException { - routeResult.setLimit(new Limit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4, false)), Collections.emptyList())); + routeResult.setLimit(new Limit(new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4)), Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseType.SQLServer, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/extractor/impl/dml/select/LimitExtractor.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/extractor/impl/dml/select/LimitExtractor.java index 95d26b2d8f..52b4891d4b 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/extractor/impl/dml/select/LimitExtractor.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/extractor/impl/dml/select/LimitExtractor.java @@ -25,10 +25,10 @@ import org.apache.shardingsphere.core.parse.extractor.impl.common.expression.imp import org.apache.shardingsphere.core.parse.extractor.util.ExtractorUtils; import org.apache.shardingsphere.core.parse.extractor.util.RuleName; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment; import org.apache.shardingsphere.core.util.NumberUtil; import java.util.Map; @@ -45,11 +45,11 @@ public final class LimitExtractor implements OptionalSQLSegmentExtractor { private final ParameterMarkerExpressionExtractor parameterMarkerExpressionExtractor = new ParameterMarkerExpressionExtractor(); @Override - public Optional extract(final ParserRuleContext ancestorNode, final Map parameterMarkerIndexes) { + public Optional extract(final ParserRuleContext ancestorNode, final Map parameterMarkerIndexes) { Optional limitNode = ExtractorUtils.findFirstChildNode(ancestorNode, RuleName.LIMIT_CLAUSE); return limitNode.isPresent() - ? Optional.of(new LimitSegment(limitNode.get().getStart().getStartIndex(), limitNode.get().getStop().getStopIndex(), - extractOffset(limitNode.get(), parameterMarkerIndexes).orNull(), extractRowCount(limitNode.get(), parameterMarkerIndexes).orNull())) : Optional.absent(); + ? Optional.of(new PaginationSegment(limitNode.get().getStart().getStartIndex(), limitNode.get().getStop().getStopIndex(), + extractOffset(limitNode.get(), parameterMarkerIndexes).orNull(), extractRowCount(limitNode.get(), parameterMarkerIndexes).orNull())) : Optional.absent(); } private Optional extractOffset(final ParserRuleContext limitNode, final Map parameterMarkerIndexes) { @@ -66,11 +66,11 @@ public final class LimitExtractor implements OptionalSQLSegmentExtractor { Optional parameterMarkerExpression = parameterMarkerExpressionExtractor.extract(limitValueNode, parameterMarkerIndexes); if (parameterMarkerExpression.isPresent()) { return new ParameterMarkerLimitValueSegment( - limitValueNode.getStart().getStartIndex(), limitValueNode.getStop().getStopIndex(), parameterMarkerExpression.get().getParameterMarkerIndex(), false); + limitValueNode.getStart().getStartIndex(), limitValueNode.getStop().getStopIndex(), parameterMarkerExpression.get().getParameterMarkerIndex()); } Optional numberLiteralsNode = ExtractorUtils.findFirstChildNode(limitValueNode, RuleName.NUMBER_LITERALS); Preconditions.checkState(numberLiteralsNode.isPresent()); return new NumberLiteralLimitValueSegment( - limitValueNode.getStart().getStartIndex(), limitValueNode.getStop().getStopIndex(), NumberUtil.getExactlyNumber(numberLiteralsNode.get().getText(), 10).intValue(), false); + limitValueNode.getStart().getStartIndex(), limitValueNode.getStop().getStopIndex(), NumberUtil.getExactlyNumber(numberLiteralsNode.get().getText(), 10).intValue()); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/extractor/impl/dml/select/item/impl/TopSelectItemExtractor.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/extractor/impl/dml/select/item/impl/TopSelectItemExtractor.java index faf3187897..02fa415d2f 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/extractor/impl/dml/select/item/impl/TopSelectItemExtractor.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/extractor/impl/dml/select/item/impl/TopSelectItemExtractor.java @@ -27,10 +27,10 @@ import org.apache.shardingsphere.core.parse.extractor.util.RuleName; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.TopSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.top.TopSegment; import java.util.Map; @@ -62,11 +62,11 @@ public final class TopSelectItemExtractor implements OptionalSQLSegmentExtractor private Optional createLimitValueSegment(final ExpressionSegment topExpr) { if (topExpr instanceof ParameterMarkerExpressionSegment) { return Optional.of( - new ParameterMarkerLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((ParameterMarkerExpressionSegment) topExpr).getParameterMarkerIndex(), false)); + new ParameterMarkerLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((ParameterMarkerExpressionSegment) topExpr).getParameterMarkerIndex())); } if (topExpr instanceof LiteralExpressionSegment && ((LiteralExpressionSegment) topExpr).getLiterals() instanceof Number) { return Optional.of( - new NumberLiteralLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((Number) ((LiteralExpressionSegment) topExpr).getLiterals()).intValue(), false)); + new NumberLiteralLimitValueSegment(topExpr.getStartIndex(), topExpr.getStopIndex(), ((Number) ((LiteralExpressionSegment) topExpr).getLiterals()).intValue())); } return Optional.absent(); } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/filler/common/dml/LimitFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/filler/common/dml/LimitFiller.java index a78021c593..3169046161 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/filler/common/dml/LimitFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/filler/common/dml/LimitFiller.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.parse.filler.common.dml; import org.apache.shardingsphere.core.parse.filler.api.SQLSegmentFiller; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; @@ -27,10 +27,10 @@ import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; * * @author duhongjun */ -public final class LimitFiller implements SQLSegmentFiller { +public final class LimitFiller implements SQLSegmentFiller { @Override - public void fill(final LimitSegment sqlSegment, final SQLStatement sqlStatement) { - ((SelectStatement) sqlStatement).setLimit(sqlSegment); + public void fill(final PaginationSegment sqlSegment, final SQLStatement sqlStatement) { + ((SelectStatement) sqlStatement).setPagination(sqlSegment); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/filler/common/dml/SelectItemFiller.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/filler/common/dml/SelectItemFiller.java index 6ca6d17917..33e7441a03 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/filler/common/dml/SelectItemFiller.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/filler/common/dml/SelectItemFiller.java @@ -32,8 +32,8 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ColumnSelectIte import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ExpressionSelectItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ShorthandSelectItemSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.TopSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.top.TopSegment; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; @@ -109,7 +109,7 @@ public final class SelectItemFiller implements SQLSegmentFiller rowNumberPredicates) { - LimitValueSegment rowCount = null; - LimitValueSegment offset = null; + RowNumberValueSegment rowCount = null; + RowNumberValueSegment offset = null; for (PredicateSegment each : rowNumberPredicates) { ExpressionSegment expression = ((PredicateCompareRightValue) each.getRightValue()).getExpression(); switch (((PredicateCompareRightValue) each.getRightValue()).getOperator()) { case ">": - offset = createLimitValueSegment(expression, false); + offset = createRowNumberValueSegment(expression, false); break; case ">=": - offset = createLimitValueSegment(expression, true); + offset = createRowNumberValueSegment(expression, true); break; case "<": - rowCount = createLimitValueSegment(expression, false); + rowCount = createRowNumberValueSegment(expression, false); break; case "<=": - rowCount = createLimitValueSegment(expression, true); + rowCount = createRowNumberValueSegment(expression, true); break; default: break; } } - selectStatement.setLimit(new LimitSegment(-1, -1, offset, rowCount)); + selectStatement.setPagination(new PaginationSegment(-1, -1, offset, rowCount)); } - private LimitValueSegment createLimitValueSegment(final ExpressionSegment expression, final boolean boundOpened) { - if (expression instanceof LiteralExpressionSegment) { - return new NumberLiteralLimitValueSegment(expression.getStartIndex(), expression.getStopIndex(), (Integer) ((LiteralExpressionSegment) expression).getLiterals(), boundOpened); - } - return new ParameterMarkerLimitValueSegment(expression.getStartIndex(), expression.getStopIndex(), ((ParameterMarkerExpressionSegment) expression).getParameterMarkerIndex(), boundOpened); + private RowNumberValueSegment createRowNumberValueSegment(final ExpressionSegment expression, final boolean boundOpened) { + return expression instanceof LiteralExpressionSegment + ? new NumberLiteralRowNumberValueSegment(expression.getStartIndex(), expression.getStopIndex(), (int) ((LiteralExpressionSegment) expression).getLiterals(), boundOpened) + : new ParameterMarkerRowNumberValueSegment( + expression.getStartIndex(), expression.getStopIndex(), ((ParameterMarkerExpressionSegment) expression).getParameterMarkerIndex(), boundOpened); } } diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/NumberLiteralPaginationValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/NumberLiteralPaginationValueSegment.java new file mode 100644 index 0000000000..f0309af7db --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/NumberLiteralPaginationValueSegment.java @@ -0,0 +1,35 @@ +/* + * 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.core.parse.sql.segment.dml.pagination; + +import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment; + +/** + * Pagination value segment for number literal. + * + * @author zhangliang + */ +public interface NumberLiteralPaginationValueSegment extends SQLSegment { + + /** + * Get value. + * + * @return value + */ + int getValue(); +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/PaginationSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/PaginationSegment.java new file mode 100644 index 0000000000..2bb40fa2f1 --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/PaginationSegment.java @@ -0,0 +1,59 @@ +/* + * 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.core.parse.sql.segment.dml.pagination; + +import com.google.common.base.Optional; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment; + +/** + * Pagination segment. + * + * @author duhongjun + */ +@RequiredArgsConstructor +@Getter +public final class PaginationSegment implements SQLSegment { + + private final int startIndex; + + private final int stopIndex; + + private final PaginationValueSegment offset; + + private final PaginationValueSegment rowCount; + + /** + * Get offset. + * + * @return offset + */ + public Optional getOffset() { + return Optional.fromNullable(offset); + } + + /** + * Get row count. + * + * @return row count + */ + public Optional getRowCount() { + return Optional.fromNullable(rowCount); + } +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/PaginationValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/PaginationValueSegment.java new file mode 100644 index 0000000000..a4a3925d67 --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/PaginationValueSegment.java @@ -0,0 +1,39 @@ +/* + * 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.core.parse.sql.segment.dml.pagination; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment; + +/** + * Pagination value segment. + * + * @author zhangliang + * @author panjuan + */ +@RequiredArgsConstructor +@Getter +public abstract class PaginationValueSegment implements SQLSegment { + + private final int startIndex; + + private final int stopIndex; + + private final boolean boundOpened; +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/ParameterMarkerPaginationValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/ParameterMarkerPaginationValueSegment.java new file mode 100644 index 0000000000..703921a481 --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/ParameterMarkerPaginationValueSegment.java @@ -0,0 +1,35 @@ +/* + * 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.core.parse.sql.segment.dml.pagination; + +import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment; + +/** + * Pagination value segment for parameter marker. + * + * @author zhangliang + */ +public interface ParameterMarkerPaginationValueSegment extends SQLSegment { + + /** + * Get parameter index. + * + * @return parameter index + */ + int getParameterIndex(); +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/LimitValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/LimitValueSegment.java new file mode 100644 index 0000000000..54cd805994 --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/LimitValueSegment.java @@ -0,0 +1,33 @@ +/* + * 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.core.parse.sql.segment.dml.pagination.limit; + +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment; + +/** + * Limit value segment. + * + * @author zhangliang + * @author panjuan + */ +public abstract class LimitValueSegment extends PaginationValueSegment { + + public LimitValueSegment(final int startIndex, final int stopIndex) { + super(startIndex, stopIndex, false); + } +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/NumberLiteralLimitValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/NumberLiteralLimitValueSegment.java new file mode 100644 index 0000000000..1600149707 --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/NumberLiteralLimitValueSegment.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit; + +import lombok.Getter; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; + +/** + * Limit value segment for number literal. + * + * @author zhangliang + */ +@Getter +public final class NumberLiteralLimitValueSegment extends LimitValueSegment implements NumberLiteralPaginationValueSegment { + + private final int value; + + public NumberLiteralLimitValueSegment(final int startIndex, final int stopIndex, final int value) { + super(startIndex, stopIndex); + this.value = value; + } +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/ParameterMarkerLimitValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/ParameterMarkerLimitValueSegment.java new file mode 100644 index 0000000000..08cb25149d --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/limit/ParameterMarkerLimitValueSegment.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit; + +import lombok.Getter; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment; + +/** + * Limit value segment for parameter marker. + * + * @author zhangliang + */ +@Getter +public final class ParameterMarkerLimitValueSegment extends LimitValueSegment implements ParameterMarkerPaginationValueSegment { + + private final int parameterIndex; + + public ParameterMarkerLimitValueSegment(final int startIndex, final int stopIndex, final int parameterIndex) { + super(startIndex, stopIndex); + this.parameterIndex = parameterIndex; + } +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/NumberLiteralRowNumberValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/NumberLiteralRowNumberValueSegment.java new file mode 100644 index 0000000000..7ebf15dbaa --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/NumberLiteralRowNumberValueSegment.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum; + +import lombok.Getter; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; + +/** + * Row number value segment for number literal. + * + * @author zhangliang + */ +@Getter +public final class NumberLiteralRowNumberValueSegment extends RowNumberValueSegment implements NumberLiteralPaginationValueSegment { + + private final int value; + + public NumberLiteralRowNumberValueSegment(final int startIndex, final int stopIndex, final int value, final boolean boundOpened) { + super(startIndex, stopIndex, boundOpened); + this.value = value; + } +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/ParameterMarkerRowNumberValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/ParameterMarkerRowNumberValueSegment.java new file mode 100644 index 0000000000..5d0b1bdec7 --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/ParameterMarkerRowNumberValueSegment.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum; + +import lombok.Getter; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment; + +/** + * Row number value segment for parameter marker. + * + * @author zhangliang + */ +@Getter +public final class ParameterMarkerRowNumberValueSegment extends RowNumberValueSegment implements ParameterMarkerPaginationValueSegment { + + private final int parameterIndex; + + public ParameterMarkerRowNumberValueSegment(final int startIndex, final int stopIndex, final int parameterIndex, final boolean boundOpened) { + super(startIndex, stopIndex, boundOpened); + this.parameterIndex = parameterIndex; + } +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/RowNumberValueSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/RowNumberValueSegment.java new file mode 100644 index 0000000000..6c3f59c82c --- /dev/null +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/rownum/RowNumberValueSegment.java @@ -0,0 +1,32 @@ +/* + * 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.core.parse.sql.segment.dml.pagination.rownum; + +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment; + +/** + * Row number value segment. + * + * @author zhangliang + */ +public abstract class RowNumberValueSegment extends PaginationValueSegment { + + public RowNumberValueSegment(final int startIndex, final int stopIndex, final boolean boundOpened) { + super(startIndex, stopIndex, boundOpened); + } +} diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/limit/TopSegment.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/top/TopSegment.java similarity index 88% rename from sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/limit/TopSegment.java rename to sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/top/TopSegment.java index 8f7b248088..2bd1445158 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/limit/TopSegment.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/segment/dml/pagination/top/TopSegment.java @@ -15,11 +15,12 @@ * limitations under the License. */ -package org.apache.shardingsphere.core.parse.sql.segment.dml.limit; +package org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.top; import lombok.Getter; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitValueSegment; /** * Top segment. diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/statement/dml/SelectStatement.java b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/statement/dml/SelectStatement.java index d60dfcb8cf..a4544b193d 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/statement/dml/SelectStatement.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/java/org/apache/shardingsphere/core/parse/sql/statement/dml/SelectStatement.java @@ -29,12 +29,12 @@ import org.apache.shardingsphere.core.parse.sql.context.selectitem.DistinctSelec import org.apache.shardingsphere.core.parse.sql.context.selectitem.SelectItem; import org.apache.shardingsphere.core.parse.sql.context.selectitem.StarSelectItem; import org.apache.shardingsphere.core.parse.sql.context.table.Table; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ColumnOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ExpressionOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.OrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.TextOrderByItemSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; import org.apache.shardingsphere.core.parse.util.SQLUtil; import java.util.Collection; @@ -71,7 +71,7 @@ public final class SelectStatement extends DQLStatement { private int groupByLastIndex; - private LimitSegment limit; + private PaginationSegment pagination; private SelectStatement parentStatement; diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/resources/META-INF/parsing-rule-definition/filler-rule-definition.xml b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/resources/META-INF/parsing-rule-definition/filler-rule-definition.xml index ab4d88791b..3f955f2ddc 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/resources/META-INF/parsing-rule-definition/filler-rule-definition.xml +++ b/sharding-core/sharding-core-parse/sharding-core-parse-common/src/main/resources/META-INF/parsing-rule-definition/filler-rule-definition.xml @@ -29,7 +29,7 @@ - + diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/SQLStatementAssert.java b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/SQLStatementAssert.java index a8cd06834d..f8caa72b0b 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/SQLStatementAssert.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/SQLStatementAssert.java @@ -21,7 +21,7 @@ import org.apache.shardingsphere.core.parse.integrate.asserts.condition.Conditio import org.apache.shardingsphere.core.parse.integrate.asserts.groupby.GroupByAssert; import org.apache.shardingsphere.core.parse.integrate.asserts.index.IndexAssert; import org.apache.shardingsphere.core.parse.integrate.asserts.item.ItemAssert; -import org.apache.shardingsphere.core.parse.integrate.asserts.limit.LimitAssert; +import org.apache.shardingsphere.core.parse.integrate.asserts.limit.PaginationAssert; import org.apache.shardingsphere.core.parse.integrate.asserts.meta.TableMetaDataAssert; import org.apache.shardingsphere.core.parse.integrate.asserts.orderby.OrderByAssert; import org.apache.shardingsphere.core.parse.integrate.asserts.table.AlterTableAssert; @@ -61,7 +61,7 @@ public final class SQLStatementAssert { private final OrderByAssert orderByAssert; - private final LimitAssert limitAssert; + private final PaginationAssert paginationAssert; private final TableMetaDataAssert metaAssert; @@ -84,7 +84,7 @@ public final class SQLStatementAssert { itemAssert = new ItemAssert(assertMessage); groupByAssert = new GroupByAssert(assertMessage); orderByAssert = new OrderByAssert(assertMessage); - limitAssert = new LimitAssert(sqlCaseType, assertMessage); + paginationAssert = new PaginationAssert(sqlCaseType, assertMessage); metaAssert = new TableMetaDataAssert(assertMessage); alterTableAssert = new AlterTableAssert(assertMessage); this.databaseType = databaseType; @@ -118,7 +118,7 @@ public final class SQLStatementAssert { itemAssert.assertItems(actual.getItems(), expected.getSelectItems()); groupByAssert.assertGroupByItems(actual.getGroupByItems(), expected.getGroupByColumns()); orderByAssert.assertOrderByItems(actual.getOrderByItems(), expected.getOrderByColumns()); - limitAssert.assertLimit(actual.getLimit(), expected.getLimit()); + paginationAssert.assertPagination(actual.getPagination(), expected.getLimit()); } private void assertCreateTableStatement(final CreateTableStatement actual) { diff --git a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/limit/LimitAssert.java b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/limit/PaginationAssert.java similarity index 52% rename from sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/limit/LimitAssert.java rename to sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/limit/PaginationAssert.java index 013843b389..5b3093a702 100644 --- a/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/limit/LimitAssert.java +++ b/sharding-core/sharding-core-parse/sharding-core-parse-test/src/test/java/org/apache/shardingsphere/core/parse/integrate/asserts/limit/PaginationAssert.java @@ -20,9 +20,9 @@ package org.apache.shardingsphere.core.parse.integrate.asserts.limit; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.parse.integrate.asserts.SQLStatementAssertMessage; import org.apache.shardingsphere.core.parse.integrate.jaxb.limit.ExpectedLimit; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment; import org.apache.shardingsphere.test.sql.SQLCaseType; import static org.hamcrest.CoreMatchers.is; @@ -30,45 +30,45 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; /** - * Limit assert. + * Pagination assert. * * @author zhangliang */ @RequiredArgsConstructor -public final class LimitAssert { +public final class PaginationAssert { private final SQLCaseType sqlCaseType; private final SQLStatementAssertMessage assertMessage; /** - * Assert limit. + * Assert pagination. * - * @param actual actual limit - * @param expected expected limit + * @param actual actual pagination + * @param expected expected pagination */ - public void assertLimit(final LimitSegment actual, final ExpectedLimit expected) { + public void assertPagination(final PaginationSegment actual, final ExpectedLimit expected) { if (null == actual) { - assertNull(assertMessage.getFullAssertMessage("Limit should not exist: "), expected); + assertNull(assertMessage.getFullAssertMessage("Pagination should not exist: "), expected); return; } if (SQLCaseType.Placeholder == sqlCaseType) { if (actual.getOffset().isPresent()) { - assertThat(assertMessage.getFullAssertMessage("Limit offset index assertion error: "), - ((ParameterMarkerLimitValueSegment) actual.getOffset().get()).getParameterIndex(), is(expected.getOffsetParameterIndex())); + assertThat(assertMessage.getFullAssertMessage("Pagination offset index assertion error: "), + ((ParameterMarkerPaginationValueSegment) actual.getOffset().get()).getParameterIndex(), is(expected.getOffsetParameterIndex())); } if (actual.getRowCount().isPresent()) { - assertThat(assertMessage.getFullAssertMessage("Limit row count index assertion error: "), - ((ParameterMarkerLimitValueSegment) actual.getRowCount().get()).getParameterIndex(), is(expected.getRowCountParameterIndex())); + assertThat(assertMessage.getFullAssertMessage("Pagination row count index assertion error: "), + ((ParameterMarkerPaginationValueSegment) actual.getRowCount().get()).getParameterIndex(), is(expected.getRowCountParameterIndex())); } } else { if (actual.getOffset().isPresent()) { - assertThat(assertMessage.getFullAssertMessage("Limit offset value assertion error: "), - ((NumberLiteralLimitValueSegment) actual.getOffset().get()).getValue(), is(expected.getOffset())); + assertThat(assertMessage.getFullAssertMessage("Pagination offset value assertion error: "), + ((NumberLiteralPaginationValueSegment) actual.getOffset().get()).getValue(), is(expected.getOffset())); } if (actual.getRowCount().isPresent()) { - assertThat(assertMessage.getFullAssertMessage("Limit row count value assertion error: "), - ((NumberLiteralLimitValueSegment) actual.getRowCount().get()).getValue(), is(expected.getRowCount())); + assertThat(assertMessage.getFullAssertMessage("Pagination row count value assertion error: "), + ((NumberLiteralPaginationValueSegment) actual.getRowCount().get()).getValue(), is(expected.getRowCount())); } } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OffsetTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OffsetTokenGenerator.java index 05afdb8e64..c6e7de301a 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OffsetTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OffsetTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.rewrite.token.pojo.OffsetToken; @@ -37,17 +37,18 @@ public final class OffsetTokenGenerator implements OptionalSQLTokenGenerator offsetSegment = getLiteralOffsetSegment((SelectStatement) sqlStatement); + Optional offsetSegment = getLiteralOffsetSegment((SelectStatement) sqlStatement); return offsetSegment.isPresent() - ? Optional.of(new OffsetToken(offsetSegment.get().getStartIndex(), offsetSegment.get().getStopIndex(), ((NumberLiteralLimitValueSegment) offsetSegment.get()).getValue())) + ? Optional.of(new OffsetToken(offsetSegment.get().getStartIndex(), offsetSegment.get().getStopIndex(), ((NumberLiteralPaginationValueSegment) offsetSegment.get()).getValue())) : Optional.absent(); } - private Optional getLiteralOffsetSegment(final SelectStatement selectStatement) { - return isLiteralOffset(selectStatement) ? selectStatement.getLimit().getOffset() : Optional.absent(); + private Optional getLiteralOffsetSegment(final SelectStatement selectStatement) { + return isLiteralOffset(selectStatement) ? selectStatement.getPagination().getOffset() : Optional.absent(); } private boolean isLiteralOffset(final SelectStatement selectStatement) { - return null != selectStatement.getLimit() && selectStatement.getLimit().getOffset().isPresent() && selectStatement.getLimit().getOffset().get() instanceof NumberLiteralLimitValueSegment; + return null != selectStatement.getPagination() + && selectStatement.getPagination().getOffset().isPresent() && selectStatement.getPagination().getOffset().get() instanceof NumberLiteralPaginationValueSegment; } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RowCountTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RowCountTokenGenerator.java index 1e8266df5d..e9af7345ba 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RowCountTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RowCountTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.rewrite.token.pojo.RowCountToken; @@ -37,17 +37,18 @@ public final class RowCountTokenGenerator implements OptionalSQLTokenGenerator rowCountSegment = getLiteralRowCountSegment((SelectStatement) sqlStatement); + Optional rowCountSegment = getLiteralRowCountSegment((SelectStatement) sqlStatement); return rowCountSegment.isPresent() - ? Optional.of(new RowCountToken(rowCountSegment.get().getStartIndex(), rowCountSegment.get().getStopIndex(), ((NumberLiteralLimitValueSegment) rowCountSegment.get()).getValue())) + ? Optional.of(new RowCountToken(rowCountSegment.get().getStartIndex(), rowCountSegment.get().getStopIndex(), ((NumberLiteralPaginationValueSegment) rowCountSegment.get()).getValue())) : Optional.absent(); } - private Optional getLiteralRowCountSegment(final SelectStatement selectStatement) { - return isLiteralRowCount(selectStatement) ? selectStatement.getLimit().getRowCount() : Optional.absent(); + private Optional getLiteralRowCountSegment(final SelectStatement selectStatement) { + return isLiteralRowCount(selectStatement) ? selectStatement.getPagination().getRowCount() : Optional.absent(); } private boolean isLiteralRowCount(final SelectStatement selectStatement) { - return null != selectStatement.getLimit() && selectStatement.getLimit().getRowCount().isPresent() && selectStatement.getLimit().getRowCount().get() instanceof NumberLiteralLimitValueSegment; + return null != selectStatement.getPagination() && selectStatement.getPagination().getRowCount().isPresent() + && selectStatement.getPagination().getRowCount().get() instanceof NumberLiteralPaginationValueSegment; } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/pojo/OffsetToken.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/pojo/OffsetToken.java index bed732dc71..67e952e9cc 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/pojo/OffsetToken.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/pojo/OffsetToken.java @@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.rewrite.token.pojo; import lombok.Getter; /** - * Offset token for limit. + * Offset token for pagination. * * @author zhangliang * @author panjuan diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/pojo/RowCountToken.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/pojo/RowCountToken.java index c07f92207d..d08cdcc9ed 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/pojo/RowCountToken.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/pojo/RowCountToken.java @@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.rewrite.token.pojo; import lombok.Getter; /** - * Row count token for limit. + * Row count token for pagination. * * @author zhangliang * @author panjuan diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriterTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriterTest.java index 212bc96a5d..5a686c3ba8 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriterTest.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriterTest.java @@ -46,9 +46,10 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegme import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.AggregationDistinctSelectItemSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ColumnOrderByItemSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateSegment; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.DeleteStatement; @@ -346,8 +347,8 @@ public final class ShardingSQLRewriterTest { @Test public void assertRewriteForLimit() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2, true), new NumberLiteralLimitValueSegment(36, 36, 2, false)); - selectStatement.setLimit(limitSegment); + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2)); + selectStatement.setPagination(limitSegment); selectStatement.getSqlSegments().add(new TableSegment(17, 23, "table_x")); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(new Limit(limitSegment, Collections.emptyList())); @@ -358,9 +359,9 @@ public final class ShardingSQLRewriterTest { } @Test - public void assertRewriteForRowNum() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(119, 119, 2, true), new NumberLiteralLimitValueSegment(98, 98, 4, false)); - selectStatement.setLimit(limitSegment); + public void assertRewriteForRowNumber() { + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false)); + selectStatement.setPagination(limitSegment); selectStatement.getSqlSegments().add(new TableSegment(68, 74, "table_x")); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(new Limit(limitSegment, Collections.emptyList())); @@ -373,8 +374,8 @@ public final class ShardingSQLRewriterTest { @Test public void assertRewriteForTopAndRowNumber() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4, false)); - selectStatement.setLimit(limitSegment); + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4)); + selectStatement.setPagination(limitSegment); selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x")); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(new Limit(limitSegment, Collections.emptyList())); @@ -387,8 +388,8 @@ public final class ShardingSQLRewriterTest { @Test public void assertRewriteForLimitForMemoryGroupBy() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2, true), new NumberLiteralLimitValueSegment(36, 36, 2, false)); - selectStatement.setLimit(limitSegment); + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2)); + selectStatement.setPagination(limitSegment); ColumnSegment columnSegment = new ColumnSegment(0, 0, "id"); columnSegment.setOwner(new TableSegment(0, 0, "x")); selectStatement.getOrderByItems().add(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC)); @@ -404,8 +405,8 @@ public final class ShardingSQLRewriterTest { @Test public void assertRewriteForRowNumForMemoryGroupBy() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(119, 119, 2, true), new NumberLiteralLimitValueSegment(98, 98, 4, false)); - selectStatement.setLimit(limitSegment); + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false)); + selectStatement.setPagination(limitSegment); selectStatement.getSqlSegments().add(new TableSegment(68, 74, "table_x")); ColumnSegment columnSegment = new ColumnSegment(0, 0, "id"); columnSegment.setOwner(new TableSegment(0, 0, "x")); @@ -422,8 +423,8 @@ public final class ShardingSQLRewriterTest { @Test public void assertRewriteForTopAndRowNumberForMemoryGroupBy() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4, false)); - selectStatement.setLimit(limitSegment); + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, false), new NumberLiteralLimitValueSegment(26, 26, 4)); + selectStatement.setPagination(limitSegment); selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x")); ColumnSegment columnSegment = new ColumnSegment(0, 0, "id"); columnSegment.setOwner(new TableSegment(0, 0, "x")); @@ -440,8 +441,8 @@ public final class ShardingSQLRewriterTest { @Test public void assertRewriteForLimitForNotRewriteLimit() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2, true), new NumberLiteralLimitValueSegment(36, 36, 2, false)); - selectStatement.setLimit(limitSegment); + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2)); + selectStatement.setPagination(limitSegment); selectStatement.getSqlSegments().add(new TableSegment(17, 23, "table_x")); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(new Limit(limitSegment, Collections.emptyList())); @@ -455,8 +456,8 @@ public final class ShardingSQLRewriterTest { @Test public void assertRewriteForRowNumForNotRewriteLimit() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(119, 119, 2, true), new NumberLiteralLimitValueSegment(98, 98, 4, false)); - selectStatement.setLimit(limitSegment); + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false)); + selectStatement.setPagination(limitSegment); selectStatement.getSqlSegments().add(new TableSegment(68, 74, "table_x")); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(new Limit(limitSegment, Collections.emptyList())); @@ -471,8 +472,8 @@ public final class ShardingSQLRewriterTest { @Test public void assertRewriteForTopAndRowNumberForNotRewriteLimit() { - LimitSegment limitSegment = new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4, false)); - selectStatement.setLimit(limitSegment); + PaginationSegment limitSegment = new PaginationSegment(0, 0, new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralLimitValueSegment(26, 26, 4)); + selectStatement.setPagination(limitSegment); selectStatement.getSqlSegments().add(new TableSegment(85, 91, "table_x")); routeResult = new SQLRouteResult(selectStatement); routeResult.setLimit(new Limit(limitSegment, Collections.emptyList())); diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/Limit.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/Limit.java index 97cb03616d..833b6ce153 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/Limit.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/Limit.java @@ -18,10 +18,11 @@ package org.apache.shardingsphere.core.route.limit; import lombok.Getter; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.ParameterMarkerLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment; import java.util.HashMap; import java.util.List; @@ -41,15 +42,16 @@ public final class Limit { private final LimitValue rowCount; - public Limit(final LimitSegment limitSegment, final List parameters) { + public Limit(final PaginationSegment limitSegment, final List parameters) { offset = limitSegment.getOffset().isPresent() ? createLimitValue(limitSegment.getOffset().get(), parameters) : null; rowCount = limitSegment.getRowCount().isPresent() ? createLimitValue(limitSegment.getRowCount().get(), parameters) : null; } - private LimitValue createLimitValue(final LimitValueSegment limitValueSegment, final List parameters) { - int segmentValue = limitValueSegment instanceof ParameterMarkerLimitValueSegment - ? (int) parameters.get(((ParameterMarkerLimitValueSegment) limitValueSegment).getParameterIndex()) : ((NumberLiteralLimitValueSegment) limitValueSegment).getValue(); - return new LimitValue(limitValueSegment, segmentValue); + private LimitValue createLimitValue(final PaginationValueSegment paginationValueSegment, final List parameters) { + int segmentValue = paginationValueSegment instanceof ParameterMarkerPaginationValueSegment + ? (int) parameters.get(((ParameterMarkerPaginationValueSegment) paginationValueSegment).getParameterIndex()) + : ((NumberLiteralPaginationValueSegment) paginationValueSegment).getValue(); + return new LimitValue(paginationValueSegment, segmentValue); } /** @@ -79,11 +81,11 @@ public final class Limit { */ public Map getRevisedParameters(final boolean isFetchAll, final String databaseType) { Map result = new HashMap<>(2, 1); - if (null != offset && offset.getLimitValueSegment() instanceof ParameterMarkerLimitValueSegment) { - result.put(((ParameterMarkerLimitValueSegment) offset.getLimitValueSegment()).getParameterIndex(), 0); + if (null != offset && offset.getPaginationValueSegment() instanceof ParameterMarkerLimitValueSegment) { + result.put(((ParameterMarkerLimitValueSegment) offset.getPaginationValueSegment()).getParameterIndex(), 0); } - if (null != rowCount && rowCount.getLimitValueSegment() instanceof ParameterMarkerLimitValueSegment) { - result.put(((ParameterMarkerLimitValueSegment) rowCount.getLimitValueSegment()).getParameterIndex(), getRewriteRowCount(isFetchAll, databaseType)); + if (null != rowCount && rowCount.getPaginationValueSegment() instanceof ParameterMarkerLimitValueSegment) { + result.put(((ParameterMarkerLimitValueSegment) rowCount.getPaginationValueSegment()).getParameterIndex(), getRewriteRowCount(isFetchAll, databaseType)); } return result; } diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/LimitValue.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/LimitValue.java index 35cb716402..72ec486c08 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/LimitValue.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/LimitValue.java @@ -19,7 +19,7 @@ package org.apache.shardingsphere.core.route.limit; import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment; /** * Limit value. @@ -30,7 +30,7 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.LimitValueSegm @Getter public final class LimitValue { - private final LimitValueSegment limitValueSegment; + private final PaginationValueSegment paginationValueSegment; private final int value; } diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ParsingSQLRouter.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ParsingSQLRouter.java index 8fc66ae8af..43def99201 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ParsingSQLRouter.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ParsingSQLRouter.java @@ -103,8 +103,8 @@ public final class ParsingSQLRouter implements ShardingRouter { mergeShardingValues(optimizeResult.getShardingConditions()); } RoutingResult routingResult = RoutingEngineFactory.newInstance(shardingRule, shardingMetaData.getDataSource(), sqlStatement, optimizeResult).route(); - if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit() && !routingResult.isSingleRouting()) { - result.setLimit(new Limit(((SelectStatement) sqlStatement).getLimit(), parameters)); + if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getPagination() && !routingResult.isSingleRouting()) { + result.setLimit(new Limit(((SelectStatement) sqlStatement).getPagination(), parameters)); } if (needMerge) { Preconditions.checkState(1 == routingResult.getRoutingUnits().size(), "Must have one sharding with subquery."); diff --git a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java index af039a92f2..f2a1059e3d 100644 --- a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java +++ b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java @@ -27,7 +27,7 @@ import org.apache.shardingsphere.core.constant.DatabaseType; import org.apache.shardingsphere.core.metadata.ShardingMetaData; import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData; import org.apache.shardingsphere.core.parse.cache.ParsingResultCache; -import org.apache.shardingsphere.core.parse.sql.segment.dml.limit.NumberLiteralLimitValueSegment; +import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.fixture.HintShardingAlgorithmFixture; import org.apache.shardingsphere.core.rule.ShardingRule; @@ -89,17 +89,17 @@ public final class DatabaseTest { String originSQL = "select user_id from tbl_pagination limit 0,5"; SQLRouteResult actual = new StatementRoutingEngine(shardingRule, mock(ShardingMetaData.class), DatabaseType.MySQL, new ParsingResultCache()).route(originSQL); SelectStatement selectStatement = (SelectStatement) actual.getSqlStatement(); - assertTrue(selectStatement.getLimit().getOffset().isPresent()); - assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getOffset().get()).getValue(), is(0)); - assertTrue(selectStatement.getLimit().getRowCount().isPresent()); - assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getRowCount().get()).getValue(), is(5)); + assertTrue(selectStatement.getPagination().getOffset().isPresent()); + assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getOffset().get()).getValue(), is(0)); + assertTrue(selectStatement.getPagination().getRowCount().isPresent()); + assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getRowCount().get()).getValue(), is(5)); originSQL = "select user_id from tbl_pagination limit 5,5"; actual = new StatementRoutingEngine(shardingRule, mock(ShardingMetaData.class), DatabaseType.MySQL, new ParsingResultCache()).route(originSQL); selectStatement = (SelectStatement) actual.getSqlStatement(); - assertTrue(selectStatement.getLimit().getOffset().isPresent()); - assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getOffset().get()).getValue(), is(5)); - assertTrue(selectStatement.getLimit().getRowCount().isPresent()); - assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getRowCount().get()).getValue(), is(5)); + assertTrue(selectStatement.getPagination().getOffset().isPresent()); + assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getOffset().get()).getValue(), is(5)); + assertTrue(selectStatement.getPagination().getRowCount().isPresent()); + assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getRowCount().get()).getValue(), is(5)); } @Test @@ -119,18 +119,18 @@ public final class DatabaseTest { when(shardingMetaData.getTable()).thenReturn(mock(ShardingTableMetaData.class)); SQLRouteResult actual = new PreparedStatementRoutingEngine(originSQL, rule, shardingMetaData, DatabaseType.MySQL, new ParsingResultCache()).route(Lists.newArrayList(13, 173)); SelectStatement selectStatement = (SelectStatement) actual.getSqlStatement(); - assertTrue(selectStatement.getLimit().getOffset().isPresent()); - assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getOffset().get()).getValue(), is(5)); - assertTrue(selectStatement.getLimit().getRowCount().isPresent()); - assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getRowCount().get()).getValue(), is(10)); + assertTrue(selectStatement.getPagination().getOffset().isPresent()); + assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getOffset().get()).getValue(), is(5)); + assertTrue(selectStatement.getPagination().getRowCount().isPresent()); + assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getRowCount().get()).getValue(), is(10)); assertThat(actual.getRoutingResult().getRoutingUnits().size(), is(1)); originSQL = "select city_id from t_user where city_id in (?,?) limit 5,10"; actual = new PreparedStatementRoutingEngine(originSQL, rule, shardingMetaData, DatabaseType.MySQL, new ParsingResultCache()).route(Lists.newArrayList(89, 84)); selectStatement = (SelectStatement) actual.getSqlStatement(); - assertTrue(selectStatement.getLimit().getOffset().isPresent()); - assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getOffset().get()).getValue(), is(5)); - assertTrue(selectStatement.getLimit().getRowCount().isPresent()); - assertThat(((NumberLiteralLimitValueSegment) selectStatement.getLimit().getRowCount().get()).getValue(), is(10)); + assertTrue(selectStatement.getPagination().getOffset().isPresent()); + assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getOffset().get()).getValue(), is(5)); + assertTrue(selectStatement.getPagination().getRowCount().isPresent()); + assertThat(((NumberLiteralLimitValueSegment) selectStatement.getPagination().getRowCount().get()).getValue(), is(10)); assertThat(actual.getRoutingResult().getRoutingUnits().size(), is(2)); } -- GitLab