Skip to content

Commit

Permalink
Merge 4b53f23 into 789bd51
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 19, 2020
2 parents 789bd51 + 4b53f23 commit 8aec00c
Show file tree
Hide file tree
Showing 30 changed files with 743 additions and 999 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.ParameterMarkerRowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.RowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopProjectionSegment;

import java.util.Map;

Expand All @@ -44,7 +44,7 @@ public final class TopProjectionExtractor implements OptionalSQLSegmentExtractor
private final ExpressionExtractor expressionExtractor = new ExpressionExtractor();

@Override
public Optional<TopSegment> extract(final ParserRuleContext expressionNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
public Optional<TopProjectionSegment> extract(final ParserRuleContext expressionNode, final Map<ParserRuleContext, Integer> parameterMarkerIndexes) {
Optional<ParserRuleContext> topNode = ExtractorUtils.findFirstChildNode(expressionNode, RuleName.TOP);
if (!topNode.isPresent()) {
return Optional.absent();
Expand All @@ -55,8 +55,8 @@ public Optional<TopSegment> extract(final ParserRuleContext expressionNode, fina
Optional<RowNumberValueSegment> rowNumberValueSegment = createRowNumberValueSegment(topExpr.get());
Preconditions.checkState(rowNumberValueSegment.isPresent());
ParserRuleContext rowNumberAliasNode = ExtractorUtils.getFirstChildNode(topNode.get().getParent(), RuleName.ALIAS);
return Optional.of(
new TopSegment(topNode.get().getStart().getStartIndex(), topNode.get().getStop().getStopIndex(), topNode.get().getText(), rowNumberValueSegment.get(), rowNumberAliasNode.getText()));
return Optional.of(new TopProjectionSegment(topNode.get().getStart().getStartIndex(), topNode.get().getStop().getStopIndex(),
topNode.get().getText(), rowNumberValueSegment.get(), rowNumberAliasNode.getText()));
}

private Optional<RowNumberValueSegment> createRowNumberValueSegment(final ExpressionSegment topExpr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.RowNumberValueSegment;

/**
* Top segment.
* Top projection segment.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public final class TopSegment implements ProjectionSegment {
public final class TopProjectionSegment implements ProjectionSegment {

private final int startIndex;

Expand All @@ -39,5 +39,5 @@ public final class TopSegment implements ProjectionSegment {

private final RowNumberValueSegment top;

private final String rowNumberAlias;
private final String alias;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.shardingsphere.sql.parser.relation.segment.select.projection.ProjectionsContext;
import org.apache.shardingsphere.sql.parser.relation.segment.select.pagination.PaginationContext;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
Expand Down Expand Up @@ -49,11 +49,11 @@ public PaginationContext createPaginationContext(final SelectStatement selectSta
if (limitSegment.isPresent()) {
return new LimitPaginationContextEngine().createPaginationContext(limitSegment.get(), parameters);
}
Optional<TopSegment> topSegment = selectStatement.findSQLSegment(TopSegment.class);
Optional<TopProjectionSegment> topProjectionSegment = selectStatement.findSQLSegment(TopProjectionSegment.class);
Optional<WhereSegment> whereSegment = selectStatement.findSQLSegment(WhereSegment.class);
if (topSegment.isPresent()) {
if (topProjectionSegment.isPresent()) {
return new TopPaginationContextEngine().createPaginationContext(
topSegment.get(), whereSegment.isPresent() ? whereSegment.get().getAndPredicates() : Collections.<AndPredicate>emptyList(), parameters);
topProjectionSegment.get(), whereSegment.isPresent() ? whereSegment.get().getAndPredicates() : Collections.<AndPredicate>emptyList(), parameters);
}
if (whereSegment.isPresent()) {
return new RowNumberPaginationContextEngine().createPaginationContext(whereSegment.get().getAndPredicates(), projectionsContext, parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.ParameterMarkerRowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.RowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.value.PredicateCompareRightValue;
Expand All @@ -44,15 +44,15 @@ public final class TopPaginationContextEngine {
/**
* Create pagination context.
*
* @param topSegment top segment
* @param topProjectionSegment top projection segment
* @param andPredicates and predicates
* @param parameters SQL parameters
* @return pagination context
*/
public PaginationContext createPaginationContext(final TopSegment topSegment, final Collection<AndPredicate> andPredicates, final List<Object> parameters) {
Optional<PredicateSegment> rowNumberPredicate = getRowNumberPredicate(andPredicates, topSegment.getRowNumberAlias());
public PaginationContext createPaginationContext(final TopProjectionSegment topProjectionSegment, final Collection<AndPredicate> andPredicates, final List<Object> parameters) {
Optional<PredicateSegment> rowNumberPredicate = getRowNumberPredicate(andPredicates, topProjectionSegment.getAlias());
Optional<PaginationValueSegment> offset = rowNumberPredicate.isPresent() ? createOffsetWithRowNumber(rowNumberPredicate.get()) : Optional.<PaginationValueSegment>absent();
PaginationValueSegment rowCount = topSegment.getTop();
PaginationValueSegment rowCount = topProjectionSegment.getTop();
return new PaginationContext(offset.orNull(), rowCount, parameters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.shardingsphere.sql.parser.relation.segment.select.projection.ProjectionsContext;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.limit.LimitSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.WhereSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
import org.junit.Test;
Expand Down Expand Up @@ -53,7 +53,7 @@ public void assertCreatePaginationContextWhenLimitSegmentIsPresent() {
public void assertCreatePaginationContextWhenLimitSegmentAbsentAndTopSegmentPresent() {
SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.findSQLSegment(LimitSegment.class)).thenReturn(Optional.<LimitSegment>absent());
when(selectStatement.findSQLSegment(TopSegment.class)).thenReturn(Optional.of(new TopSegment(0, 10, "text", null, "rowNumberAlias")));
when(selectStatement.findSQLSegment(TopProjectionSegment.class)).thenReturn(Optional.of(new TopProjectionSegment(0, 10, "text", null, "rowNumberAlias")));
when(selectStatement.findSQLSegment(WhereSegment.class)).thenReturn(Optional.<WhereSegment>absent());
PaginationContext paginationContext = new PaginationContextEngine().createPaginationContext(selectStatement, null, Collections.emptyList());
assertFalse(paginationContext.getOffsetSegment().isPresent());
Expand All @@ -64,7 +64,7 @@ public void assertCreatePaginationContextWhenLimitSegmentAbsentAndTopSegmentPres
public void assertCreatePaginationContextWhenLimitSegmentTopSegmentAbsentAndWhereSegmentPresent() {
SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.findSQLSegment(LimitSegment.class)).thenReturn(Optional.<LimitSegment>absent());
when(selectStatement.findSQLSegment(TopSegment.class)).thenReturn(Optional.<TopSegment>absent());
when(selectStatement.findSQLSegment(TopProjectionSegment.class)).thenReturn(Optional.<TopProjectionSegment>absent());
when(selectStatement.findSQLSegment(WhereSegment.class)).thenReturn(Optional.of(new WhereSegment(0, 10, 10)));
ProjectionsContext projectionsContext = mock(ProjectionsContext.class);
when(projectionsContext.findAlias(anyString())).thenReturn(Optional.<String>absent());
Expand All @@ -77,7 +77,7 @@ public void assertCreatePaginationContextWhenLimitSegmentTopSegmentAbsentAndWher
public void assertCreatePaginationContextWhenResultIsPaginationContext() {
SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.findSQLSegment(LimitSegment.class)).thenReturn(Optional.<LimitSegment>absent());
when(selectStatement.findSQLSegment(TopSegment.class)).thenReturn(Optional.<TopSegment>absent());
when(selectStatement.findSQLSegment(TopProjectionSegment.class)).thenReturn(Optional.<TopProjectionSegment>absent());
when(selectStatement.findSQLSegment(WhereSegment.class)).thenReturn(Optional.<WhereSegment>absent());
assertThat(new PaginationContextEngine().createPaginationContext(selectStatement, mock(ProjectionsContext.class), Collections.emptyList()), instanceOf(PaginationContext.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.PaginationValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.rownum.ParameterMarkerRowNumberValueSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.top.TopProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.AndPredicate;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.value.PredicateCompareRightValue;
Expand Down Expand Up @@ -57,10 +57,10 @@ public void setUp() {

@Test
public void assertCreatePaginationContextWhenRowNumberPredicateNotPresent() {
TopSegment topSegment = new TopSegment(0, 10, "text", null, "rowNumberAlias");
TopProjectionSegment topProjectionSegment = new TopProjectionSegment(0, 10, "text", null, "rowNumberAlias");
Collection<AndPredicate> andPredicates = Collections.emptyList();
List<Object> parameters = Collections.emptyList();
PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(topSegment, andPredicates, parameters);
PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(topProjectionSegment, andPredicates, parameters);
assertFalse(paginationContext.getOffsetSegment().isPresent());
assertFalse(paginationContext.getRowCountSegment().isPresent());
}
Expand All @@ -85,7 +85,7 @@ public void assertCreatePaginationContextWhenPredicateInRightValue() {
Collection<AndPredicate> andPredicates = Collections.singleton(andPredicate);
ProjectionsContext projectionsContext = mock(ProjectionsContext.class);
when(projectionsContext.findAlias(anyString())).thenReturn(Optional.of("predicateRowNumberAlias"));
PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopSegment(0, 10, "text", null, name), andPredicates, Collections.emptyList());
PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopProjectionSegment(0, 10, "text", null, name), andPredicates, Collections.emptyList());
assertFalse(paginationContext.getOffsetSegment().isPresent());
assertFalse(paginationContext.getRowCountSegment().isPresent());
}
Expand All @@ -103,7 +103,8 @@ public void assertCreatePaginationContextWhenParameterMarkerRowNumberValueSegmen
Collection<AndPredicate> andPredicates = Collections.singleton(andPredicate);
ProjectionsContext projectionsContext = mock(ProjectionsContext.class);
when(projectionsContext.findAlias(anyString())).thenReturn(Optional.of("predicateRowNumberAlias"));
PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopSegment(0, 10, "text", null, name), andPredicates, Collections.<Object>singletonList(1));
PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(
new TopProjectionSegment(0, 10, "text", null, name), andPredicates, Collections.<Object>singletonList(1));
assertTrue(paginationContext.getOffsetSegment().isPresent());
PaginationValueSegment paginationValueSegment = paginationContext.getOffsetSegment().get();
assertThat(paginationValueSegment, instanceOf(ParameterMarkerRowNumberValueSegment.class));
Expand All @@ -126,7 +127,7 @@ private void assertCreatePaginationContextWhenRowNumberPredicatePresentAndWithGi
Collection<AndPredicate> andPredicates = Collections.singleton(andPredicate);
ProjectionsContext projectionsContext = mock(ProjectionsContext.class);
when(projectionsContext.findAlias(anyString())).thenReturn(Optional.of("predicateRowNumberAlias"));
PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopSegment(0, 10, "text", null, name), andPredicates, Collections.emptyList());
PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopProjectionSegment(0, 10, "text", null, name), andPredicates, Collections.emptyList());
assertTrue(paginationContext.getOffsetSegment().isPresent());
PaginationValueSegment paginationValueSegment = paginationContext.getOffsetSegment().get();
assertThat(paginationValueSegment, instanceOf(NumberLiteralRowNumberValueSegment.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public static void assertIs(final SQLStatementAssertMessage assertMessage, final
}

private static void assertSelectStatement(final SQLStatementAssertMessage assertMessage, final SelectStatement actual, final ParserResult expected, final SQLCaseType sqlCaseType) {
ProjectionAssert.assertIs(assertMessage, actual.getProjections(), expected.getProjections(), sqlCaseType);
Optional<GroupBySegment> groupBySegment = actual.findSQLSegment(GroupBySegment.class);
ProjectionAssert.assertIs(assertMessage, actual.getProjections(), expected.getProjections());
Optional<GroupBySegment> groupBySegment = actual.getGroupBy();
if (groupBySegment.isPresent()) {
GroupByAssert.assertIs(assertMessage, groupBySegment.get().getGroupByItems(), expected.getGroupByColumns());
}
Optional<OrderBySegment> orderBySegment = actual.findSQLSegment(OrderBySegment.class);
Optional<OrderBySegment> orderBySegment = actual.getOrderBy();
if (orderBySegment.isPresent()) {
OrderByAssert.assertIs(assertMessage, orderBySegment.get().getOrderByItems(), expected.getOrderByColumns());
}
Expand All @@ -94,7 +94,7 @@ private static void assertSelectStatement(final SQLStatementAssertMessage assert
PaginationAssert.assertOffset(assertMessage, limitSegment.get().getOffset().orNull(), expected.getOffset(), sqlCaseType);
PaginationAssert.assertRowCount(assertMessage, limitSegment.get().getRowCount().orNull(), expected.getRowCount(), sqlCaseType);
}
Optional<WhereSegment> whereSegment = actual.findSQLSegment(WhereSegment.class);
Optional<WhereSegment> whereSegment = actual.getWhere();
if (whereSegment.isPresent() && null != expected.getWhereSegment()) {
PredicateAssert.assertIs(assertMessage, whereSegment.get(), expected.getWhereSegment(), sqlCaseType);
}
Expand Down

0 comments on commit 8aec00c

Please sign in to comment.