Skip to content

Commit

Permalink
fix count return null (apache#6118)
Browse files Browse the repository at this point in the history
* fix count return null

* fix checkstyle

* fix unit test

* polish checkstyle

* polish code style

* fix in getMemoryResultSetRows

* fix code style

* extract method

* fix codestyle
  • Loading branch information
kimmking authored Jun 19, 2020
1 parent cd9a11b commit f9d0903
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.shardingsphere.sql.parser.binder.segment.select.projection.impl.AggregationProjection;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.sql.parser.sql.constant.AggregationType;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.infra.executor.sql.QueryResult;
import org.apache.shardingsphere.infra.merge.result.impl.memory.MemoryMergedResult;
Expand All @@ -39,6 +40,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -134,11 +136,27 @@ private boolean getValueCaseSensitiveFromTables(final QueryResult queryResult, f
}
return false;
}
private List<MemoryQueryResultRow> getMemoryResultSetRows(final SelectStatementContext selectStatementContext,

private List<MemoryQueryResultRow> getMemoryResultSetRows(final SelectStatementContext selectStatementContext,
final Map<GroupByValue, MemoryQueryResultRow> dataMap, final List<Boolean> valueCaseSensitive) {
if (dataMap.isEmpty()) {
Object[] data = generateReturnData(selectStatementContext);
return Collections.singletonList(new MemoryQueryResultRow(data));
}

List<MemoryQueryResultRow> result = new ArrayList<>(dataMap.values());
result.sort(new GroupByRowComparator(selectStatementContext, valueCaseSensitive));
return result;
}

private Object[] generateReturnData(final SelectStatementContext selectStatementContext) {
List projections = new LinkedList(selectStatementContext.getProjectionsContext().getProjections());
Object[] data = new Object[projections.size()];
for (int i = 0; i < projections.size(); i++) {
if (projections.get(i) instanceof AggregationProjection && AggregationType.COUNT == ((AggregationProjection) projections.get(i)).getType()) {
data[i] = 0;
}
}
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public final class GroupByMemoryMergedResultTest {
public void assertNextForResultSetsAllEmpty() throws SQLException {
ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL"));
MergedResult actual = resultMerger.merge(Arrays.asList(createQueryResult(), createQueryResult(), createQueryResult()), createSelectStatementContext(), null);
assertTrue(actual.next());
assertFalse(actual.next());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
package org.apache.shardingsphere.infra.merge.result.impl.memory;

import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.executor.sql.QueryResult;

import java.sql.SQLException;

/**
* Memory query result row.
*/
@RequiredArgsConstructor
public final class MemoryQueryResultRow {

private final Object[] data;
Expand Down

0 comments on commit f9d0903

Please sign in to comment.