Skip to content

Commit

Permalink
[CALCITE-6474] Aggregate with constant key can get a RowCount greater…
Browse files Browse the repository at this point in the history
… than its MaxRowCount
  • Loading branch information
rubenada committed Jul 17, 2024
1 parent c3f39b2 commit a27a8de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ && allGroupKeysAreConstant(rel, predicateList)) {
return rowCount * rel.getGroupSets().size();
}

private static boolean allGroupKeysAreConstant(Aggregate aggregate,
static boolean allGroupKeysAreConstant(Aggregate aggregate,
RelOptPredicateList predicateList) {
final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
for (int key : aggregate.getGroupSet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.calcite.rel.metadata;

import org.apache.calcite.adapter.enumerable.EnumerableLimit;
import org.apache.calcite.plan.RelOptPredicateList;
import org.apache.calcite.plan.volcano.RelSubset;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.SingleRel;
Expand Down Expand Up @@ -191,6 +192,17 @@ public Double getRowCount(Aggregate rel, RelMetadataQuery mq) {
// Aggregate with no GROUP BY always returns 1 row (even on empty table).
return 1D;
}

// Aggregate with constant GROUP BY always returns 1 row
if (rel.getGroupType() == Aggregate.Group.SIMPLE) {
final RelOptPredicateList predicateList =
mq.getPulledUpPredicates(rel.getInput());
if (!RelOptPredicateList.isEmpty(predicateList)
&& RelMdMaxRowCount.allGroupKeysAreConstant(rel, predicateList)) {
return 1D;
}
}

// rowCount is the cardinality of the group by columns
Double distinctRowCount =
mq.getDistinctRowCount(rel.getInput(), groupKey, null);
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,23 @@ final RelMetadataFixture sql(String sql) {
sql(sql).assertThatRowCount(is(1D), is(0D), is(1D));
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6474">[CALCITE-6474]
* Aggregate with constant key can get a RowCount greater than its MaxRowCount </a>. */
@Test void testRowCountAggregateConstantKeysOnBigInput() {
final String sql = ""
+ "select distinct deptno from ("
+ "select deptno from emp e1 union all "
+ "select deptno from emp e2 union all "
+ "select deptno from emp e3 union all "
+ "select deptno from emp e4 union all "
+ "select deptno from emp e5 union all "
+ "select deptno from emp e6 union all "
+ "select deptno from emp e7"
+ ") where deptno=4";
sql(sql).assertThatRowCount(is(1D), is(0D), is(1D));
}

@Test void testRowCountFilterAggregateEmptyKey() {
final String sql = "select count(*) from emp where 1 = 0";
sql(sql).assertThatRowCount(is(1D), is(1D), is(1D));
Expand Down

0 comments on commit a27a8de

Please sign in to comment.