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

Rename SelectStatementContext#isSameGroupByAndOrderByItems to isGroupByStartsWithOrderByItems(#25414) #25680

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private NullsOrderType createDefaultNullsOrderType(final SelectStatement selectS

private MergedResult getGroupByMergedResult(final List<QueryResult> queryResults, final SelectStatementContext selectStatementContext,
final Map<String, Integer> columnLabelIndexMap, final ShardingSphereSchema schema) throws SQLException {
return selectStatementContext.isSameGroupByAndOrderByItems()
return selectStatementContext.isGroupByStartsWithOrderByItems()
? new GroupByStreamMergedResult(columnLabelIndexMap, queryResults, selectStatementContext, schema)
: new GroupByMemoryMergedResult(queryResults, selectStatementContext, schema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ void assertGenerateSQLToken() {
SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
when(selectStatementContext.getPaginationContext()).thenReturn(paginationContext);
when(selectStatementContext.getGroupByContext().getItems().isEmpty()).thenReturn(Boolean.FALSE);
when(selectStatementContext.isSameGroupByAndOrderByItems()).thenReturn(Boolean.FALSE);
when(selectStatementContext.isGroupByStartsWithOrderByItems()).thenReturn(Boolean.FALSE);
RowCountTokenGenerator rowCountTokenGenerator = new RowCountTokenGenerator();
RowCountToken rowCountToken = rowCountTokenGenerator.generateSQLToken(selectStatementContext);
assertThat(rowCountToken.toString(), is(String.valueOf(Integer.MAX_VALUE)));
when(selectStatementContext.isSameGroupByAndOrderByItems()).thenReturn(Boolean.TRUE);
when(selectStatementContext.isGroupByStartsWithOrderByItems()).thenReturn(Boolean.TRUE);
rowCountToken = rowCountTokenGenerator.generateSQLToken(selectStatementContext);
assertThat(rowCountToken.toString(), is(String.valueOf(testOffsetSegmentValue + testRowCountSegmentValue)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,6 @@ public long getRevisedRowCount(final SelectStatementContext selectStatementConte

private boolean isMaxRowCount(final SelectStatementContext selectStatementContext) {
return (!selectStatementContext.getGroupByContext().getItems().isEmpty()
|| !selectStatementContext.getProjectionsContext().getAggregationProjections().isEmpty()) && !selectStatementContext.isSameGroupByAndOrderByItems();
|| !selectStatementContext.getProjectionsContext().getAggregationProjections().isEmpty()) && !selectStatementContext.isGroupByStartsWithOrderByItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -290,12 +292,24 @@ private String getOrderItemText(final TextOrderByItemSegment orderByItemSegment)
}

/**
* Judge group by and order by sequence is same or not.
* Judge group is start with order or not.
*
* @return group by and order by sequence is same or not
* @return group is start with order or not
*/
public boolean isSameGroupByAndOrderByItems() {
return !groupByContext.getItems().isEmpty() && groupByContext.getItems().equals(orderByContext.getItems());
public boolean isGroupByStartsWithOrderByItems() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @yangsen00, can you add e2e test for this logic change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course.but Can you give a Can you give an example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (groupByContext.getItems().isEmpty()) {
return false;
}
Iterator<OrderByItem> orderByIterator = orderByContext.getItems().iterator();
Iterator<OrderByItem> groupByIterator = groupByContext.getItems().iterator();
while (groupByIterator.hasNext() && orderByIterator.hasNext()) {
OrderByItem groupItem = groupByIterator.next();
OrderByItem orderItem = orderByIterator.next();
if (!Objects.equals(groupItem, orderItem)) {
return false;
}
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private void assertIsSameGroupByAndOrderByItems(final SelectStatement selectStat
selectStatement.setGroupBy(new GroupBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, NullsOrderType.LAST))));
selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, NullsOrderType.LAST))));
SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
assertTrue(selectStatementContext.isSameGroupByAndOrderByItems());
assertTrue(selectStatementContext.isGroupByStartsWithOrderByItems());
}

private SelectStatementContext createSelectStatementContext(final SelectStatement selectStatement) {
Expand Down Expand Up @@ -286,7 +286,7 @@ void assertIsNotSameGroupByAndOrderByItemsWhenEmptyGroupByForSQLServer() {
private void assertIsNotSameGroupByAndOrderByItemsWhenEmptyGroupBy(final SelectStatement selectStatement) {
selectStatement.setProjections(new ProjectionsSegment(0, 0));
SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
assertFalse(selectStatementContext.isSameGroupByAndOrderByItems());
assertFalse(selectStatementContext.isGroupByStartsWithOrderByItems());
}

@Test
Expand Down Expand Up @@ -319,7 +319,7 @@ private void assertIsNotSameGroupByAndOrderByItemsWhenDifferentGroupByAndOrderBy
selectStatement.setGroupBy(new GroupBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, NullsOrderType.LAST))));
selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, NullsOrderType.LAST))));
SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
assertFalse(selectStatementContext.isSameGroupByAndOrderByItems());
assertFalse(selectStatementContext.isGroupByStartsWithOrderByItems());
}

@Test
Expand Down