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

asolimando pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 1efc2bdd77 [CALCITE-7365] RelMdRowCount ignores estimateRowCount() 
overrides in SingleRel's subclasses
1efc2bdd77 is described below

commit 1efc2bdd77f663ba28c37a7a331c0df5d1c4de83
Author: Alessandro Solimando <[email protected]>
AuthorDate: Fri Jan 9 19:44:28 2026 +0100

    [CALCITE-7365] RelMdRowCount ignores estimateRowCount() overrides in 
SingleRel's subclasses
    
    Change the SingleRel handler to delegate to estimateRowCount() so that
    custom SingleRel subclasses can provide accurate row count estimates
---
 .../apache/calcite/rel/metadata/RelMdRowCount.java |  5 ++--
 .../org/apache/calcite/test/RelMetadataTest.java   | 29 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

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 f853fd647a..e83f4c1da9 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
@@ -191,9 +191,10 @@ public Double getRowCount(Calc rel, RelMetadataQuery mq) {
     return sampleRate * inputRowCount;
   }
 
-  // Covers Converter, Interpreter
+  // Covers Converter, Interpreter, and custom SingleRel subclasses
+  // Delegates to estimateRowCount() to allow subclasses to provide custom 
estimates
   public @Nullable Double getRowCount(SingleRel rel, RelMetadataQuery mq) {
-    return mq.getRowCount(rel.getInput());
+    return rel.estimateRowCount(mq);
   }
 
   public @Nullable Double getRowCount(Join rel, RelMetadataQuery mq) {
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 acb04f1762..c5bdfcedd2 100644
--- a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
@@ -1433,6 +1433,18 @@ void testColumnOriginsUnion() {
         .assertThatRowCount(is(EMP_SIZE * 0.2), is(0D), 
is(Double.POSITIVE_INFINITY));
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7365";>[CALCITE-7365]
+   * RelMdRowCount ignores estimateRowCount() overrides in SingleRel's 
subclasses</a>. */
+  @Test void testRowCountCustomSingleRel() {
+    final RelNode scan = sql("select * from emp").toRel();
+    final ExpandingRel expanding =
+        new ExpandingRel(scan.getCluster(), scan.getTraitSet(), scan);
+    final RelMetadataQuery mq = scan.getCluster().getMetadataQuery();
+    final double rowCount = mq.getRowCount(expanding);
+    assertThat(rowCount, is(140D));
+  }
+
   @Test void testRowCountAggregate() {
     final String sql = "select deptno from emp group by deptno";
     sql(sql).assertThatRowCount(is(1.4D), is(0D), 
is(Double.POSITIVE_INFINITY));
@@ -5409,6 +5421,23 @@ private static class DummyRelNode extends SingleRel {
     }
   }
 
+  /**
+   * A custom SingleRel that expands row count by a factor of 10.
+   * Used to test that estimateRowCount() overrides are respected
+   * by the metadata system.
+   */
+  private static class ExpandingRel extends SingleRel {
+    private static final double EXPANSION_FACTOR = 10.0;
+
+    ExpandingRel(RelOptCluster cluster, RelTraitSet traits, RelNode input) {
+      super(cluster, traits, input);
+    }
+
+    @Override public double estimateRowCount(RelMetadataQuery mq) {
+      return mq.getRowCount(input) * EXPANSION_FACTOR;
+    }
+  }
+
   /** Mock catalog reader for registering a table with composite keys. */
   private static class CompositeKeysCatalogReader
       extends MockCatalogReaderSimple {

Reply via email to