diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java index 478dbb41354..98f966b7118 100644 --- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java +++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java @@ -2679,6 +2679,8 @@ private boolean alreadyUnique(List aggCallList, List extraNodes) { final RelMetadataQuery mq = peek().getCluster().getMetadataQuery(); if (aggCallList.isEmpty() && groupSet.isEmpty()) { + // We can't remove "GROUP BY ()" if there's a chance the rel could be + // empty. if (RelMdUtil.isRelDefinitelyEmpty(mq, peek())) { return false; }