diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngine.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngine.java index 98b6b49d71e00..555985197aaee 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngine.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngine.java @@ -75,7 +75,7 @@ private List getRealQueryResults(final List queryResul if (!selectStatement.getAggregationDistinctSelectItems().isEmpty()) { result = getDividedQueryResults(new AggregationDistinctQueryResult(queryResults, selectStatement.getAggregationDistinctSelectItems())); } - if (selectStatement.getDistinctSelectItem().isPresent()) { + if (isNeedProcessDistinctSelectItem()) { result = getDividedQueryResults(new DistinctQueryResult(queryResults, new ArrayList<>(selectStatement.getDistinctSelectItem().get().getDistinctColumnLabels()))); } return result.isEmpty() ? queryResults : result; @@ -91,6 +91,10 @@ public QueryResult apply(final DistinctQueryResult input) { }); } + private boolean isNeedProcessDistinctSelectItem() { + return selectStatement.getDistinctSelectItem().isPresent() && selectStatement.getGroupByItems().isEmpty(); + } + private Map getColumnLabelIndexMap(final QueryResult queryResult) throws SQLException { Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); for (int i = 1; i <= queryResult.getColumnCount(); i++) {