liyafan82 commented on a change in pull request #2330:
URL: https://github.com/apache/calcite/pull/2330#discussion_r562487548
##########
File path: core/src/main/java/org/apache/calcite/rex/RexUtil.java
##########
@@ -2620,6 +2621,103 @@ private RexNode or(Iterable<? extends RexNode> nodes) {
return Util.transform(list, Object::toString);
}
+ /** Estimate the number of distinct values for the specified columns (if
possible),
+ * given the condition. */
+ public static @Nullable Double estimateColumnsNdv(
+ ImmutableBitSet columns, @Nullable RexNode condition) {
+ if (condition == null) {
+ return null;
+ }
+ if (condition.isAlwaysFalse()) {
+ return 0.0;
+ }
+ double ndv = 1.0;
+ List<RexNode> conditions = RelOptUtil.conjunctions(condition);
+ for (int col : columns) {
+ Double colNdv = estimateColumnNdv(col, conditions);
+ if (colNdv == null) {
+ // if one column's ndv cannot be estimated, we cannot
+ // estimate the ndv for the column set.
+ return null;
+ }
+ // ndv's should be multiplied.
+ // for example, NDV(x) <= a, and NDV(y) <= b;
+ // then we have NDV(x, y) <= a * b;
+ ndv *= colNdv;
+ }
+ return ndv;
+ }
+
+ /** Estimate the number of distinct values for a single column (if possible),
+ * given the condition. */
+ private static @Nullable Double estimateColumnNdv(int colIdx, List<RexNode>
conditions) {
+ Double ndv = null;
+ for (RexNode condition : conditions) {
+ Double singleNdv = estimateColumnNdvSingleCondition(colIdx, condition);
+ if (singleNdv != null) {
+ if (ndv == null) {
Review comment:
Accepted. Thanks for the suggestion.
----------------------------------------------------------------
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]