This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch ignite-21580
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit 9854ead1a7d24567c400bcdc49935a625327a4ab
Author: amashenkov <[email protected]>
AuthorDate: Mon Apr 1 11:05:04 2024 +0300

    fixup! Fix aggregate row count estimation.
---
 .../engine/metadata/IgniteMdDistinctRowCount.java  | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git 
a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/metadata/IgniteMdDistinctRowCount.java
 
b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/metadata/IgniteMdDistinctRowCount.java
index 6b1cce64c2..ea69593144 100644
--- 
a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/metadata/IgniteMdDistinctRowCount.java
+++ 
b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/metadata/IgniteMdDistinctRowCount.java
@@ -20,6 +20,7 @@ package org.apache.ignite.internal.sql.engine.metadata;
 import org.apache.calcite.plan.Convention;
 import org.apache.calcite.plan.volcano.RelSubset;
 import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
 import org.apache.calcite.rel.metadata.CyclicMetadataException;
 import org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider;
 import org.apache.calcite.rel.metadata.RelMdDistinctRowCount;
@@ -29,6 +30,7 @@ import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.util.BuiltInMethod;
 import org.apache.calcite.util.ImmutableBitSet;
 import org.apache.calcite.util.NumberUtil;
+import org.checkerframework.checker.nullness.qual.Nullable;
 
 /**
  * IgniteMdDistinctRowCount.
@@ -40,6 +42,26 @@ public class IgniteMdDistinctRowCount extends 
RelMdDistinctRowCount {
             ReflectiveRelMetadataProvider.reflectiveSource(
                     BuiltInMethod.DISTINCT_ROW_COUNT.method, new 
IgniteMdDistinctRowCount());
 
+    @Override
+    public @Nullable Double getDistinctRowCount(Aggregate rel, 
RelMetadataQuery mq, ImmutableBitSet groupKey, @Nullable RexNode predicate) {
+        // Estimation of the groups count is not available.
+        // Use heuristic estimation for result rows count.
+        int groupCount = groupKey.cardinality();
+        if (groupCount == 0) {
+            return 1.0;
+        } else {
+            Double groupsCnt = mq.getDistinctRowCount(rel.getInput(), 
groupKey, predicate);
+
+            if (groupsCnt != null) {
+                return groupsCnt;
+            }
+
+            double rowCount = mq.getRowCount(rel.getInput());
+            rowCount *= 1.0 - Math.pow(.75, groupCount);
+            return rowCount;
+        }
+    }
+
     /** {@inheritDoc} */
     @Override
     public Double getDistinctRowCount(

Reply via email to