liyafan82 commented on a change in pull request #2330:
URL: https://github.com/apache/calcite/pull/2330#discussion_r562487344
##########
File path:
core/src/main/java/org/apache/calcite/rel/metadata/RelMdDistinctRowCount.java
##########
@@ -146,13 +155,17 @@ protected RelMdDistinctRowCount() {}
predicate,
rel.getCondition());
- return mq.getDistinctRowCount(rel.getInput(), groupKey, unionPreds);
+ Double ndvUpperBound = RexUtil.estimateColumnsNdv(groupKey, unionPreds);
+ return NumberUtil.min(
+ mq.getDistinctRowCount(rel.getInput(), groupKey, unionPreds),
ndvUpperBound);
Review comment:
Thanks for your comments.
Here we are using min, because the estimated NDV is an upper bound of the
actual NDV. In other words, the real NDV should never exceed the estimated NDV.
For example, given expression `x = 1`, the estimated NDV is 1. However, the
real NDV can also be 0, when the underlying data do not contain a row with `x`
equal to 1.
For this code, if the value of `mq.getDistinctRowCount(rel.getInput(),
groupKey, unionPreds)` is smaller than 1, we just use that value. On the other
hand, if the value of `mq.getDistinctRowCount(rel.getInput(), groupKey,
unionPreds)` is greater than 1, we use the NDV 1, because it is an upper bound.
This is why we are using min here.
----------------------------------------------------------------
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]