This is an automated email from the ASF dual-hosted git repository. jhyde pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/calcite.git
commit 1ce516dcb660caa40ac806617f37cae9532bd0aa Author: Jing Zhang <[email protected]> AuthorDate: Thu Mar 17 21:45:00 2022 +0800 [CALCITE-5050] Metadata (RelMdRowCount) should reflect the fact that an Aggregate with no GROUP BY always returns 1 row Close apache/calcite#2746 --- .../apache/calcite/rel/metadata/RelMdRowCount.java | 5 +++- .../org/apache/calcite/test/RelMetadataTest.java | 29 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdRowCount.java b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdRowCount.java index 6b10195..3de9314 100644 --- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdRowCount.java +++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdRowCount.java @@ -199,7 +199,10 @@ public class RelMdRowCount public Double getRowCount(Aggregate rel, RelMetadataQuery mq) { ImmutableBitSet groupKey = rel.getGroupSet(); - + if (groupKey.isEmpty()) { + // Aggregate with no GROUP BY always returns 1 row (even on empty table). + return 1D; + } // rowCount is the cardinality of the group by columns Double distinctRowCount = mq.getDistinctRowCount(rel.getInput(), groupKey, null); diff --git a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java index c7498da..038547c 100644 --- a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java +++ b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java @@ -674,6 +674,35 @@ public class RelMetadataTest { sql(sql).assertThatRowCount(is(1D), is(1D), is(1D)); } + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-5050">[CALCITE-5050] + * Aggregate with no GROUP BY always returns 1 row. </a>. */ + @Test void testRowCountAggregateEmptyGroupKey() { + fixture() + .withRelFn(b -> + b.scan("EMP") + .aggregate( + b.groupKey(), + b.count(false, "C")) + .build()) + .assertThatRowCount(is(1D), is(1D), is(1D)); + } + + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-5050">[CALCITE-5050] + * Aggregate with no GROUP BY always returns 1 row (even on empty table). </a>. */ + @Test void testRowCountAggregateEmptyGroupKeyWithEmptyTable() { + fixture() + .withRelFn(b -> + b.scan("EMP") + .filter(b.literal(false)) + .aggregate( + b.groupKey(), + b.count(false, "C")) + .build()) + .assertThatRowCount(is(1D), is(1D), is(1D)); + } + @Test void testRowCountAggregateConstantKey() { final String sql = "select count(*) from emp where deptno=2 and ename='emp1' " + "group by deptno, ename";
