liyafan82 commented on a change in pull request #2330:
URL: https://github.com/apache/calcite/pull/2330#discussion_r561508574
##########
File path: core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
##########
@@ -2857,6 +2857,108 @@ private void checkSarg(String message, Sarg sarg,
is(2), is("Sarg[[3..8], [10..20]]"));
}
+ /** Tests {@link Sarg#numDistinctVals(RelDataType)}. */
+ @Test void testSargNdv() {
+ RelDataType nonNullableInt =
typeFactory.createSqlType(SqlTypeName.INTEGER);
+ RelDataType nullableInt =
typeFactory.createTypeWithNullability(nonNullableInt, true);
+
+ RelDataType nonNullableFloat =
typeFactory.createSqlType(SqlTypeName.FLOAT);
+ RelDataType nullableFloat =
typeFactory.createTypeWithNullability(nonNullableFloat, true);
+
+ // point: 10 (non-nullable)
+ Sarg sarg = Sarg.of(false,
+ ImmutableRangeSet.of(Range.closed(10, 10)));
+ assertThat(sarg.numDistinctVals(nonNullableInt), is(1.0));
Review comment:
Thank you for the good suggestion.
I have revised the code accordingly.
##########
File path: core/src/main/java/org/apache/calcite/util/RangeSets.java
##########
@@ -105,6 +110,45 @@ private RangeSets() {}
return 0;
}
+ /**
+ * Estimates the number of distinct values for a range (if possible).
+ * A null is returned if the number of distinct values is infinity or
unknown.
+ */
+ public static <C extends Comparable<C>> @Nullable Double numDistinctVals(
+ Range<C> range, RelDataType type) {
+ if (RangeSets.isPoint(range)) {
+ return 1.0;
+ }
+ if (!range.hasLowerBound() || !range.hasUpperBound()) {
+ // infinity range.
+ return null;
+ }
+ C lower = range.lowerEndpoint();
+ C upper = range.upperEndpoint();
+
+ if (lower instanceof Number && upper instanceof Number) {
+ Number lowerNum = (Number) lower;
+ Number upperNum = (Number) upper;
+
+ boolean discreteType = type.getSqlTypeName() ==
SqlTypeName.BOOLEAN.BOOLEAN
Review comment:
Nice catch. Thank you.
----------------------------------------------------------------
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]