You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2017/02/15 05:32:46 UTC

[2/4] calcite git commit: [CALCITE-1635] Add MinRowCount metadata

[CALCITE-1635] Add MinRowCount metadata


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/3b78d1d7
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/3b78d1d7
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/3b78d1d7

Branch: refs/heads/master
Commit: 3b78d1d74e33977a93005b65b0b209844e783fd1
Parents: d8c4d73
Author: Julian Hyde <jh...@apache.org>
Authored: Fri Jan 20 12:34:31 2017 -0800
Committer: Julian Hyde <jh...@apache.org>
Committed: Tue Feb 14 14:17:30 2017 -0800

----------------------------------------------------------------------
 .../calcite/rel/metadata/BuiltInMetadata.java   |  23 +++
 .../metadata/DefaultRelMetadataProvider.java    |   1 +
 .../calcite/rel/metadata/RelMdMaxRowCount.java  |   3 +
 .../calcite/rel/metadata/RelMdMinRowCount.java  | 161 +++++++++++++++++++
 .../calcite/rel/metadata/RelMetadataQuery.java  |  22 +++
 .../org/apache/calcite/util/BuiltInMethod.java  |   2 +
 .../apache/calcite/test/RelMetadataTest.java    | 132 +++++++--------
 7 files changed, 270 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/3b78d1d7/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java b/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java
index 0ff0515..eb1ff79 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/BuiltInMetadata.java
@@ -206,6 +206,29 @@ public abstract class BuiltInMetadata {
     }
   }
 
+  /** Metadata about the minimum number of rows returned by a relational
+   * expression. */
+  public interface MinRowCount extends Metadata {
+    MetadataDef<MinRowCount> DEF = MetadataDef.of(MinRowCount.class,
+        MinRowCount.Handler.class, BuiltInMethod.MIN_ROW_COUNT.method);
+
+    /**
+     * Estimates the minimum number of rows which will be returned by a
+     * relational expression.
+     *
+     * <p>The default implementation for this query returns 0,
+     * but metadata providers can override this with their own cost models.
+     *
+     * @return lower bound on the number of rows returned
+     */
+    Double getMinRowCount();
+
+    /** Handler API. */
+    interface Handler extends MetadataHandler<MinRowCount> {
+      Double getMinRowCount(RelNode r, RelMetadataQuery mq);
+    }
+  }
+
   /** Metadata about the number of distinct rows returned by a set of columns
    * in a relational expression. */
   public interface DistinctRowCount extends Metadata {

http://git-wip-us.apache.org/repos/asf/calcite/blob/3b78d1d7/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java b/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java
index 2abd5b5..bc04c4e 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/DefaultRelMetadataProvider.java
@@ -45,6 +45,7 @@ public class DefaultRelMetadataProvider extends ChainedRelMetadataProvider {
             RelMdColumnOrigins.SOURCE,
             RelMdRowCount.SOURCE,
             RelMdMaxRowCount.SOURCE,
+            RelMdMinRowCount.SOURCE,
             RelMdUniqueKeys.SOURCE,
             RelMdColumnUniqueness.SOURCE,
             RelMdPopulationSize.SOURCE,

http://git-wip-us.apache.org/repos/asf/calcite/blob/3b78d1d7/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java
index dd811a7..967960d 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMaxRowCount.java
@@ -80,6 +80,9 @@ public class RelMdMaxRowCount
   }
 
   public Double getMaxRowCount(Filter rel, RelMetadataQuery mq) {
+    if (rel.getCondition().isAlwaysFalse()) {
+      return 0D;
+    }
     return mq.getMaxRowCount(rel.getInput());
   }
 

http://git-wip-us.apache.org/repos/asf/calcite/blob/3b78d1d7/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMinRowCount.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMinRowCount.java b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMinRowCount.java
new file mode 100644
index 0000000..e34bedc
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdMinRowCount.java
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.metadata;
+
+import org.apache.calcite.adapter.enumerable.EnumerableLimit;
+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.core.Filter;
+import org.apache.calcite.rel.core.Intersect;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.Minus;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.core.TableScan;
+import org.apache.calcite.rel.core.Union;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.util.Bug;
+import org.apache.calcite.util.BuiltInMethod;
+import org.apache.calcite.util.Util;
+
+/**
+ * RelMdMinRowCount supplies a default implementation of
+ * {@link RelMetadataQuery#getMinRowCount} for the standard logical algebra.
+ */
+public class RelMdMinRowCount
+    implements MetadataHandler<BuiltInMetadata.MinRowCount> {
+  public static final RelMetadataProvider SOURCE =
+      ReflectiveRelMetadataProvider.reflectiveSource(
+          BuiltInMethod.MIN_ROW_COUNT.method, new RelMdMinRowCount());
+
+  //~ Methods ----------------------------------------------------------------
+
+  public MetadataDef<BuiltInMetadata.MinRowCount> getDef() {
+    return BuiltInMetadata.MinRowCount.DEF;
+  }
+
+  public Double getMinRowCount(Union rel, RelMetadataQuery mq) {
+    double rowCount = 0.0;
+    for (RelNode input : rel.getInputs()) {
+      Double partialRowCount = mq.getMinRowCount(input);
+      if (partialRowCount != null) {
+        rowCount += partialRowCount;
+      }
+    }
+    return rowCount;
+  }
+
+  public Double getMinRowCount(Intersect rel, RelMetadataQuery mq) {
+    return 0d; // no lower bound
+  }
+
+  public Double getMinRowCount(Minus rel, RelMetadataQuery mq) {
+    return 0d; // no lower bound
+  }
+
+  public Double getMinRowCount(Filter rel, RelMetadataQuery mq) {
+    return 0d; // no lower bound
+  }
+
+  public Double getMinRowCount(Project rel, RelMetadataQuery mq) {
+    return mq.getMinRowCount(rel.getInput());
+  }
+
+  public Double getMinRowCount(Sort rel, RelMetadataQuery mq) {
+    Double rowCount = mq.getMinRowCount(rel.getInput());
+    if (rowCount == null) {
+      rowCount = 0D;
+    }
+    final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
+    rowCount = Math.max(rowCount - offset, 0D);
+
+    if (rel.fetch != null) {
+      final int limit = RexLiteral.intValue(rel.fetch);
+      if (limit < rowCount) {
+        return (double) limit;
+      }
+    }
+    return rowCount;
+  }
+
+  public Double getMinRowCount(EnumerableLimit rel, RelMetadataQuery mq) {
+    Double rowCount = mq.getMinRowCount(rel.getInput());
+    if (rowCount == null) {
+      rowCount = 0D;
+    }
+    final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
+    rowCount = Math.max(rowCount - offset, 0D);
+
+    if (rel.fetch != null) {
+      final int limit = RexLiteral.intValue(rel.fetch);
+      if (limit < rowCount) {
+        return (double) limit;
+      }
+    }
+    return rowCount;
+  }
+
+  public Double getMinRowCount(Aggregate rel, RelMetadataQuery mq) {
+    if (rel.getGroupSet().isEmpty()) {
+      // Aggregate with no GROUP BY always returns 1 row (even on empty table).
+      return 1D;
+    }
+    final Double rowCount = mq.getMinRowCount(rel.getInput());
+    if (rowCount != null && rowCount >= 1D) {
+      return (double) rel.getGroupSets().size();
+    }
+    return 0D;
+  }
+
+  public Double getMinRowCount(Join rel, RelMetadataQuery mq) {
+    return 0D;
+  }
+
+  public Double getMinRowCount(TableScan rel, RelMetadataQuery mq) {
+    return 0D;
+  }
+
+  public Double getMinRowCount(Values values, RelMetadataQuery mq) {
+    // For Values, the minimum row count is the actual row count.
+    return (double) values.getTuples().size();
+  }
+
+  public Double getMinRowCount(RelSubset rel, RelMetadataQuery mq) {
+    // FIXME This is a short-term fix for [CALCITE-1018]. A complete
+    // solution will come with [CALCITE-1048].
+    Util.discard(Bug.CALCITE_1048_FIXED);
+    for (RelNode node : rel.getRels()) {
+      if (node instanceof Sort) {
+        Sort sort = (Sort) node;
+        if (sort.fetch != null) {
+          return (double) RexLiteral.intValue(sort.fetch);
+        }
+      }
+    }
+
+    return 0D;
+  }
+
+  // Catch-all rule when none of the others apply.
+  public Double getMinRowCount(RelNode rel, RelMetadataQuery mq) {
+    return null;
+  }
+}
+
+// End RelMdMinRowCount.java

http://git-wip-us.apache.org/repos/asf/calcite/blob/3b78d1d7/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java b/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java
index 4e07612..85f747c 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMetadataQuery.java
@@ -90,6 +90,7 @@ public class RelMetadataQuery {
   private BuiltInMetadata.Distribution.Handler distributionHandler;
   private BuiltInMetadata.ExplainVisibility.Handler explainVisibilityHandler;
   private BuiltInMetadata.MaxRowCount.Handler maxRowCountHandler;
+  private BuiltInMetadata.MinRowCount.Handler minRowCountHandler;
   private BuiltInMetadata.Memory.Handler memoryHandler;
   private BuiltInMetadata.NonCumulativeCost.Handler nonCumulativeCostHandler;
   private BuiltInMetadata.Parallelism.Handler parallelismHandler;
@@ -119,6 +120,7 @@ public class RelMetadataQuery {
     this.distributionHandler = prototype.distributionHandler;
     this.explainVisibilityHandler = prototype.explainVisibilityHandler;
     this.maxRowCountHandler = prototype.maxRowCountHandler;
+    this.minRowCountHandler = prototype.minRowCountHandler;
     this.memoryHandler = prototype.memoryHandler;
     this.nonCumulativeCostHandler = prototype.nonCumulativeCostHandler;
     this.parallelismHandler = prototype.parallelismHandler;
@@ -166,6 +168,7 @@ public class RelMetadataQuery {
     this.distributionHandler = initialHandler(BuiltInMetadata.Distribution.Handler.class);
     this.explainVisibilityHandler = initialHandler(BuiltInMetadata.ExplainVisibility.Handler.class);
     this.maxRowCountHandler = initialHandler(BuiltInMetadata.MaxRowCount.Handler.class);
+    this.minRowCountHandler = initialHandler(BuiltInMetadata.MinRowCount.Handler.class);
     this.memoryHandler = initialHandler(BuiltInMetadata.Memory.Handler.class);
     this.nonCumulativeCostHandler = initialHandler(BuiltInMetadata.NonCumulativeCost.Handler.class);
     this.parallelismHandler = initialHandler(BuiltInMetadata.Parallelism.Handler.class);
@@ -227,6 +230,25 @@ public class RelMetadataQuery {
 
   /**
    * Returns the
+   * {@link BuiltInMetadata.MinRowCount#getMinRowCount()}
+   * statistic.
+   *
+   * @param rel the relational expression
+   * @return max row count
+   */
+  public Double getMinRowCount(RelNode rel) {
+    for (;;) {
+      try {
+        return minRowCountHandler.getMinRowCount(rel, this);
+      } catch (JaninoRelMetadataProvider.NoHandler e) {
+        minRowCountHandler =
+            revise(e.relClass, BuiltInMetadata.MinRowCount.DEF);
+      }
+    }
+  }
+
+  /**
+   * Returns the
    * {@link BuiltInMetadata.CumulativeCost#getCumulativeCost()}
    * statistic.
    *

http://git-wip-us.apache.org/repos/asf/calcite/blob/3b78d1d7/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java b/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
index 39526a3..535ea49 100644
--- a/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
+++ b/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
@@ -52,6 +52,7 @@ import org.apache.calcite.rel.metadata.BuiltInMetadata.Distribution;
 import org.apache.calcite.rel.metadata.BuiltInMetadata.ExplainVisibility;
 import org.apache.calcite.rel.metadata.BuiltInMetadata.MaxRowCount;
 import org.apache.calcite.rel.metadata.BuiltInMetadata.Memory;
+import org.apache.calcite.rel.metadata.BuiltInMetadata.MinRowCount;
 import org.apache.calcite.rel.metadata.BuiltInMetadata.NonCumulativeCost;
 import org.apache.calcite.rel.metadata.BuiltInMetadata.Parallelism;
 import org.apache.calcite.rel.metadata.BuiltInMetadata.PercentageOriginalRows;
@@ -360,6 +361,7 @@ public enum BuiltInMethod {
   DISTRIBUTION(Distribution.class, "distribution"),
   ROW_COUNT(RowCount.class, "getRowCount"),
   MAX_ROW_COUNT(MaxRowCount.class, "getMaxRowCount"),
+  MIN_ROW_COUNT(MinRowCount.class, "getMinRowCount"),
   DISTINCT_ROW_COUNT(DistinctRowCount.class, "getDistinctRowCount",
       ImmutableBitSet.class, RexNode.class),
   PERCENTAGE_ORIGINAL_ROWS(PercentageOriginalRows.class,

http://git-wip-us.apache.org/repos/asf/calcite/blob/3b78d1d7/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
----------------------------------------------------------------------
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 9bded46..f83361f 100644
--- a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
@@ -442,229 +442,213 @@ public class RelMetadataTest extends SqlToRelTestBase {
         false);
   }
 
-  private void checkRowCount(
-      String sql,
-      double expected) {
-    RelNode rel = convertSql(sql);
-    final RelMetadataQuery mq = RelMetadataQuery.instance();
-    Double result = mq.getRowCount(rel);
-    assertThat(result, notNullValue());
-    assertEquals(expected, result, 0d);
-  }
-
-  private void checkMaxRowCount(
-      String sql,
-      double expected) {
+  private void checkRowCount(String sql, double expected, double expectedMin,
+      double expectedMax) {
     RelNode rel = convertSql(sql);
     final RelMetadataQuery mq = RelMetadataQuery.instance();
-    Double result = mq.getMaxRowCount(rel);
+    final Double result = mq.getRowCount(rel);
     assertThat(result, notNullValue());
     assertEquals(expected, result, 0d);
+    final Double max = mq.getMaxRowCount(rel);
+    assertThat(max, notNullValue());
+    assertEquals(expectedMax, max, 0d);
+    final Double min = mq.getMinRowCount(rel);
+    assertThat(max, notNullValue());
+    assertEquals(expectedMin, min, 0d);
   }
 
   @Test public void testRowCountEmp() {
     final String sql = "select * from emp";
-    checkRowCount(sql, EMP_SIZE);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, EMP_SIZE, 0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountDept() {
     final String sql = "select * from dept";
-    checkRowCount(sql, DEPT_SIZE);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, DEPT_SIZE, 0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountValues() {
     final String sql = "select * from (values (1), (2)) as t(c)";
-    checkRowCount(sql, 2);
-    checkMaxRowCount(sql, 2);
+    checkRowCount(sql, 2, 2, 2);
   }
 
   @Test public void testRowCountCartesian() {
     final String sql = "select * from emp,dept";
-    checkRowCount(sql, EMP_SIZE * DEPT_SIZE);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, EMP_SIZE * DEPT_SIZE, 0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountJoin() {
     final String sql = "select * from emp\n"
         + "inner join dept on emp.deptno = dept.deptno";
-    checkRowCount(sql, EMP_SIZE * DEPT_SIZE * DEFAULT_EQUAL_SELECTIVITY);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, EMP_SIZE * DEPT_SIZE * DEFAULT_EQUAL_SELECTIVITY,
+        0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountJoinFinite() {
     final String sql = "select * from (select * from emp limit 14) as emp\n"
         + "inner join (select * from dept limit 4) as dept\n"
         + "on emp.deptno = dept.deptno";
-    checkRowCount(sql, EMP_SIZE * DEPT_SIZE * DEFAULT_EQUAL_SELECTIVITY);
-    checkMaxRowCount(sql, 56D); // 4 * 14
+    checkRowCount(sql, EMP_SIZE * DEPT_SIZE * DEFAULT_EQUAL_SELECTIVITY,
+        0D, 56D); // 4 * 14
   }
 
   @Test public void testRowCountJoinEmptyFinite() {
     final String sql = "select * from (select * from emp limit 0) as emp\n"
         + "inner join (select * from dept limit 4) as dept\n"
         + "on emp.deptno = dept.deptno";
-    checkRowCount(sql, 1D); // 0, rounded up to row count's minimum 1
-    checkMaxRowCount(sql, 0D); // 0 * 4
+    checkRowCount(sql, 1D, // 0, rounded up to row count's minimum 1
+        0D, 0D); // 0 * 4
   }
 
   @Test public void testRowCountLeftJoinEmptyFinite() {
     final String sql = "select * from (select * from emp limit 0) as emp\n"
         + "left join (select * from dept limit 4) as dept\n"
         + "on emp.deptno = dept.deptno";
-    checkRowCount(sql, 1D); // 0, rounded up to row count's minimum 1
-    checkMaxRowCount(sql, 0D); // 0 * 4
+    checkRowCount(sql, 1D, // 0, rounded up to row count's minimum 1
+        0D, 0D); // 0 * 4
   }
 
   @Test public void testRowCountRightJoinEmptyFinite() {
     final String sql = "select * from (select * from emp limit 0) as emp\n"
         + "right join (select * from dept limit 4) as dept\n"
         + "on emp.deptno = dept.deptno";
-    checkRowCount(sql, 1D); // 0, rounded up to row count's minimum 1
-    checkMaxRowCount(sql, 4D); // 1 * 4
+    checkRowCount(sql, 1D, // 0, rounded up to row count's minimum 1
+        0D, 4D); // 1 * 4
   }
 
   @Test public void testRowCountJoinFiniteEmpty() {
     final String sql = "select * from (select * from emp limit 7) as emp\n"
         + "inner join (select * from dept limit 0) as dept\n"
         + "on emp.deptno = dept.deptno";
-    checkRowCount(sql, 1D); // 0, rounded up to row count's minimum 1
-    checkMaxRowCount(sql, 0D); // 7 * 0
+    checkRowCount(sql, 1D, // 0, rounded up to row count's minimum 1
+        0D, 0D); // 7 * 0
   }
 
   @Test public void testRowCountJoinEmptyEmpty() {
     final String sql = "select * from (select * from emp limit 0) as emp\n"
         + "inner join (select * from dept limit 0) as dept\n"
         + "on emp.deptno = dept.deptno";
-    checkRowCount(sql, 1D); // 0, rounded up to row count's minimum 1
-    checkMaxRowCount(sql, 0D); // 0 * 0
+    checkRowCount(sql, 1D, // 0, rounded up to row count's minimum 1
+        0D, 0D); // 0 * 0
   }
 
   @Test public void testRowCountUnion() {
     final String sql = "select ename from emp\n"
         + "union all\n"
         + "select name from dept";
-    checkRowCount(sql, EMP_SIZE + DEPT_SIZE);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, EMP_SIZE + DEPT_SIZE, 0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountUnionOnFinite() {
     final String sql = "select ename from (select * from emp limit 100)\n"
         + "union all\n"
         + "select name from (select * from dept limit 40)";
-    checkRowCount(sql, EMP_SIZE + DEPT_SIZE);
-    checkMaxRowCount(sql, 140D);
+    checkRowCount(sql, EMP_SIZE + DEPT_SIZE, 0D, 140D);
   }
 
   @Test public void testRowCountIntersectOnFinite() {
     final String sql = "select ename from (select * from emp limit 100)\n"
         + "intersect\n"
         + "select name from (select * from dept limit 40)";
-    checkRowCount(sql, Math.min(EMP_SIZE, DEPT_SIZE));
-    checkMaxRowCount(sql, 40D);
+    checkRowCount(sql, Math.min(EMP_SIZE, DEPT_SIZE), 0D, 40D);
   }
 
   @Test public void testRowCountMinusOnFinite() {
     final String sql = "select ename from (select * from emp limit 100)\n"
         + "except\n"
         + "select name from (select * from dept limit 40)";
-    checkRowCount(sql, 4D);
-    checkMaxRowCount(sql, 100D);
+    checkRowCount(sql, 4D, 0D, 100D);
   }
 
   @Test public void testRowCountFilter() {
     final String sql = "select * from emp where ename='Mathilda'";
-    checkRowCount(sql, EMP_SIZE * DEFAULT_EQUAL_SELECTIVITY);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, EMP_SIZE * DEFAULT_EQUAL_SELECTIVITY,
+        0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountFilterOnFinite() {
     final String sql = "select * from (select * from emp limit 10)\n"
         + "where ename='Mathilda'";
-    checkRowCount(sql, 10D * DEFAULT_EQUAL_SELECTIVITY);
-    checkMaxRowCount(sql, 10D);
+    checkRowCount(sql, 10D * DEFAULT_EQUAL_SELECTIVITY, 0D, 10D);
+  }
+
+  @Test public void testRowCountFilterFalse() {
+    final String sql = "select * from (values 'a', 'b') as t(x) where false";
+    checkRowCount(sql, 1D, 0D, 0D);
   }
 
   @Test public void testRowCountSort() {
     final String sql = "select * from emp order by ename";
-    checkRowCount(sql, EMP_SIZE);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, EMP_SIZE, 0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountSortHighLimit() {
     final String sql = "select * from emp order by ename limit 123456";
-    checkRowCount(sql, EMP_SIZE);
-    checkMaxRowCount(sql, 123456D);
+    checkRowCount(sql, EMP_SIZE, 0D, 123456D);
   }
 
   @Test public void testRowCountSortHighOffset() {
     final String sql = "select * from emp order by ename offset 123456";
-    checkRowCount(sql, 1D);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, 1D, 0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountSortHighOffsetLimit() {
     final String sql = "select * from emp order by ename limit 5 offset 123456";
-    checkRowCount(sql, 1D);
-    checkMaxRowCount(sql, 5D);
+    checkRowCount(sql, 1D, 0D, 5D);
   }
 
   @Test public void testRowCountSortLimit() {
     final String sql = "select * from emp order by ename limit 10";
-    checkRowCount(sql, 10d);
-    checkMaxRowCount(sql, 10d);
+    checkRowCount(sql, 10d, 0D, 10d);
   }
 
   @Test public void testRowCountSortLimit0() {
     final String sql = "select * from emp order by ename limit 10";
-    checkRowCount(sql, 10d);
-    checkMaxRowCount(sql, 10d);
+    checkRowCount(sql, 10d, 0D, 10d);
   }
 
   @Test public void testRowCountSortLimitOffset() {
     final String sql = "select * from emp order by ename limit 10 offset 5";
-    checkRowCount(sql, 9D); // 14 - 5
-    checkMaxRowCount(sql, 10d);
+    checkRowCount(sql, 9D /* 14 - 5 */, 0D, 10d);
   }
 
   @Test public void testRowCountSortLimitOffsetOnFinite() {
     final String sql = "select * from (select * from emp limit 12)\n"
         + "order by ename limit 20 offset 5";
-    checkRowCount(sql, 7d);
-    checkMaxRowCount(sql, 7d);
+    checkRowCount(sql, 7d, 0D, 7d);
   }
 
   @Test public void testRowCountAggregate() {
     final String sql = "select deptno from emp group by deptno";
-    checkRowCount(sql, 1.4D);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, 1.4D, 0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountAggregateGroupingSets() {
     final String sql = "select deptno from emp\n"
         + "group by grouping sets ((deptno), (ename, deptno))";
-    checkRowCount(sql, 2.8D); // EMP_SIZE / 10 * 2
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, 2.8D, // EMP_SIZE / 10 * 2
+        0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountAggregateGroupingSetsOneEmpty() {
     final String sql = "select deptno from emp\n"
         + "group by grouping sets ((deptno), ())";
-    checkRowCount(sql, 2.8D);
-    checkMaxRowCount(sql, Double.POSITIVE_INFINITY);
+    checkRowCount(sql, 2.8D, 0D, Double.POSITIVE_INFINITY);
   }
 
   @Test public void testRowCountAggregateEmptyKey() {
     final String sql = "select count(*) from emp";
-    checkRowCount(sql, 1D);
-    checkMaxRowCount(sql, 1D);
+    checkRowCount(sql, 1D, 1D, 1D);
+  }
+
+  @Test public void testRowCountFilterAggregateEmptyKey() {
+    final String sql = "select count(*) from emp where 1 = 0";
+    checkRowCount(sql, 1D, 1D, 1D);
   }
 
   @Test public void testRowCountAggregateEmptyKeyOnEmptyTable() {
     final String sql = "select count(*) from (select * from emp limit 0)";
-    checkRowCount(sql, 1D);
-    checkMaxRowCount(sql, 1D);
+    checkRowCount(sql, 1D, 1D, 1D);
   }
 
   private void checkFilterSelectivity(