Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete projection assertion #4015

Merged
merged 25 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
196e1e6
remove useless findSQLSegment in SQLStatementAssert
terrymanu Jan 17, 2020
8d6a9c8
assert projections's attribute
terrymanu Jan 17, 2020
cde1abc
distinctRow to distinct-row for xml
terrymanu Jan 17, 2020
bcfc8d0
fix assert text
terrymanu Jan 17, 2020
6b5cb0e
remove XmlElementWrapper
terrymanu Jan 17, 2020
ff17d68
refactor ExpectedProjections's field name
terrymanu Jan 17, 2020
d4a39c3
rename ExpectedBaseProjection to AbstractExpectedProjection
terrymanu Jan 17, 2020
32f7a7b
refactor ExpectedProjections.findExpectedProjections()
terrymanu Jan 18, 2020
0df390e
add getStartIndex and getStopIndex for ExpectedProjection interface
terrymanu Jan 18, 2020
e300e3c
add sort for projections
terrymanu Jan 18, 2020
d18d45f
add sort for projections
terrymanu Jan 18, 2020
06b86c4
remove useless getter for ExpectedProjections
terrymanu Jan 18, 2020
00281be
refactor ProjectionAssert.assertProjection
terrymanu Jan 18, 2020
2743f74
add start index and stop index's assert for assertProjection
terrymanu Jan 18, 2020
dfcb759
use int instead of Integer in ExpectedProjection
terrymanu Jan 18, 2020
b516b2e
use int instead of Integer in ExpectedProjections
terrymanu Jan 18, 2020
f1bfebb
refactor ExpectedTableSegment
terrymanu Jan 18, 2020
6ddc590
add assertShorthandProjection
terrymanu Jan 18, 2020
117cbcf
add assertShorthandProjection
terrymanu Jan 18, 2020
ffa24f2
add assertColumnProjection
terrymanu Jan 18, 2020
2bbd24b
add assertAggregationProjection
terrymanu Jan 18, 2020
5464b86
add assertAggregationDistinctProjection
terrymanu Jan 18, 2020
a5a9d37
add assertExpressionProjection
terrymanu Jan 18, 2020
a6d3305
add assertTopProjection
terrymanu Jan 18, 2020
4b53f23
remove ExpectedProjection.getText() and getParameterMarkerText()
terrymanu Jan 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading