chunweilei commented on a change in pull request #2355:
URL: https://github.com/apache/calcite/pull/2355#discussion_r589132188



##########
File path: 
core/src/main/java/org/apache/calcite/rel/metadata/RelMdDistinctRowCount.java
##########
@@ -227,6 +227,21 @@ public Double getDistinctRowCount(Values rel, 
RelMetadataQuery mq,
         return 1D;
       }
     }
+
+    // try to remove const columns from the group keys, as they do not
+    // affect the distinct row count
+    ImmutableBitSet nonConstCols = rel.getNonConstColumns(groupKey);
+    if (nonConstCols.cardinality() == 0) {
+      // all columns are constants, the distinct row count should be 1
+      return 1D;
+    }
+
+    if (nonConstCols.cardinality() < groupKey.cardinality()) {
+      // some const columns can be removed, call the method recursively
+      // with the trimmed columns
+      return getDistinctRowCount(rel, mq, nonConstCols, predicate);
+    }

Review comment:
       What would it return without your change?

##########
File path: core/src/main/java/org/apache/calcite/rel/core/Project.java
##########
@@ -442,6 +443,19 @@ public boolean isMapping() {
     return true;
   }
 
+  /**
+   * Given some columns, gets the non-constant ones.
+   */
+  public ImmutableBitSet getNonConstColumns(ImmutableBitSet columns) {
+    ImmutableBitSet.Builder nonConstCols = ImmutableBitSet.builder();
+    for (int col : columns) {
+      if (!RexUtil.isLiteral(getProjects().get(col), true)) {
+        nonConstCols.set(col);
+      }
+    }

Review comment:
       Would it better to put this method to RexUtils for being reused?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to