Manya0407 commented on code in PR #6746:
URL: https://github.com/apache/hive/pull/6746#discussion_r4025157503
##########
ql/src/test/org/apache/hadoop/hive/ql/optimizer/calcite/stats/TestFilterSelectivityEstimator.java:
##########
@@ -1202,4 +1210,184 @@ private static long timestampMillis(String timestamp) {
private static long timestamp(String timestamp) {
return timestampMillis(timestamp) / 1000;
}
+
+ private static final int INTEGER_FIELD_INDEX = 6; // f_integer
+ private static final int DATE_FIELD_INDEX = 9; // f_date
+
+ private void setupMinMaxNoHistogram(float min, float max) {
+ setupMinMaxNoHistogram(min, max, 0);
+ }
+
+ private void setupMinMaxNoHistogram(float min, float max, long numNulls) {
+ stats = new ColStatistics();
+ stats.setHistogram(null);
+ stats.setRange(min, max);
+ stats.setNumNulls(numNulls);
+ currentInputRef = REX_BUILDER.makeInputRef(scan, INTEGER_FIELD_INDEX);
+ doReturn(Collections.singletonList(stats)).when(tableMock)
+ .getColStat(Collections.singletonList(INTEGER_FIELD_INDEX));
+ }
+
+ private RelNode createScanWithPlanner(HiveConf conf) {
+ RelOptPlanner planner = CalcitePlanner.createPlanner(conf);
+ RelOptCluster cluster = RelOptCluster.create(planner, REX_BUILDER);
+ RelBuilder relBuilder = HiveRelFactories.HIVE_BUILDER.create(cluster,
schemaMock);
+ HiveTableScan tableScan =
+ new HiveTableScan(cluster, cluster.traitSetOf(HiveRelNode.CONVENTION),
tableMock, "table", null, false, false);
+ return relBuilder.push(tableScan).build();
+ }
+
+ @Test
+ public void testComparisonMinMaxNoHistogram() {
+ setupMinMaxNoHistogram(0, 100);
+ RexNode int50 = REX_BUILDER.makeLiteral(50,
TYPE_FACTORY.createSqlType(INTEGER), true);
+ RexNode filter =
REX_BUILDER.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL, currentInputRef,
int50);
+ FilterSelectivityEstimator estimator = new
FilterSelectivityEstimator(scan, mq);
+ Assert.assertEquals(0.5, estimator.estimateSelectivity(filter), DELTA);
+ }
+
+ @Test
+ public void testComparisonMinMaxNoHistogramNoRange() {
+ stats = new ColStatistics();
+ stats.setHistogram(null);
+ currentInputRef = REX_BUILDER.makeInputRef(scan, INTEGER_FIELD_INDEX);
+ doReturn(Collections.singletonList(stats)).when(tableMock)
+ .getColStat(Collections.singletonList(INTEGER_FIELD_INDEX));
+ RexNode filter = REX_BUILDER.makeCall(SqlStdOperatorTable.LESS_THAN,
currentInputRef, int3);
+ FilterSelectivityEstimator estimator = new
FilterSelectivityEstimator(scan, mq);
+ Assert.assertEquals(0.3333333333333333,
estimator.estimateSelectivity(filter), DELTA);
+ }
+
+ @Test
+ public void testComparisonDateMinMaxNoHistogram() {
+ long minDays = LocalDate.parse("2020-11-01").toEpochDay();
+ long maxDays = LocalDate.parse("2020-11-07").toEpochDay();
+ stats = new ColStatistics();
+ stats.setHistogram(null);
+ stats.setRange(minDays, maxDays);
+ currentInputRef = REX_BUILDER.makeInputRef(scan, DATE_FIELD_INDEX);
+ doReturn(Collections.singletonList(stats)).when(tableMock)
+ .getColStat(Collections.singletonList(DATE_FIELD_INDEX));
+ RexNode filter =
REX_BUILDER.makeCall(SqlStdOperatorTable.LESS_THAN_OR_EQUAL, currentInputRef,
+ literalDate("2020-11-04"));
+ FilterSelectivityEstimator estimator = new
FilterSelectivityEstimator(scan, mq);
+ Assert.assertEquals(0.5, estimator.estimateSelectivity(filter), DELTA);
Review Comment:
Thanks ā same situation as the integer case.
The test currently expects 0.5 because the implementation uses continuous
uniform width between min/max dates (3 day-units out of 6 ā 3/6), consistent
with EvaluateComparatorWithRange in the annotation path.
Your 4/7 uses inclusive discrete day counting (4 days out of 7 in the range).
I believe discrete counting is more natural for DATE filters from a user
perspective. Iām happy to update the expected value (and the estimator for DATE
if we align on that model) once confirmed we want discrete semantics in CBO
and how that should match the annotation path.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]