diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java index f0422c0a54449a..8a6331bbe6e7e3 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java @@ -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; @@ -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; @@ -134,11 +136,27 @@ private boolean getValueCaseSensitiveFromTables(final QueryResult queryResult, f } return false; } - - private List getMemoryResultSetRows(final SelectStatementContext selectStatementContext, + + private List getMemoryResultSetRows(final SelectStatementContext selectStatementContext, final Map dataMap, final List valueCaseSensitive) { + if (dataMap.isEmpty()) { + Object[] data = generateReturnData(selectStatementContext); + return Collections.singletonList(new MemoryQueryResultRow(data)); + } + List 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; + } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java index d1328924a6ba52..2f176f58508235 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java @@ -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()); } diff --git a/shardingsphere-infra/shardingsphere-infra-merge/src/main/java/org/apache/shardingsphere/infra/merge/result/impl/memory/MemoryQueryResultRow.java b/shardingsphere-infra/shardingsphere-infra-merge/src/main/java/org/apache/shardingsphere/infra/merge/result/impl/memory/MemoryQueryResultRow.java index 0ad134add0d4da..8e80bdd2c6cbcd 100644 --- a/shardingsphere-infra/shardingsphere-infra-merge/src/main/java/org/apache/shardingsphere/infra/merge/result/impl/memory/MemoryQueryResultRow.java +++ b/shardingsphere-infra/shardingsphere-infra-merge/src/main/java/org/apache/shardingsphere/infra/merge/result/impl/memory/MemoryQueryResultRow.java @@ -18,6 +18,7 @@ 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; @@ -25,6 +26,7 @@ /** * Memory query result row. */ +@RequiredArgsConstructor public final class MemoryQueryResultRow { private final Object[] data;