konstantinb commented on code in PR #6505:
URL: https://github.com/apache/hive/pull/6505#discussion_r4701681239


##########
ql/src/test/org/apache/hadoop/hive/ql/optimizer/stats/annotation/TestStatsRulesProcFactory.java:
##########
@@ -709,10 +847,58 @@ public void 
testComputeAggregateColumnMinMaxWithKnownNumNulls() throws SemanticE
         cs, conf, agg, "bigint", parentStats);
 
     // With known numNulls=20, valuesCount = 100 - 20 = 80
-    assertNotNull("Range should be set", cs.getRange());
+    assertNotNull(cs.getRange(), "Range should be set");
     assertEquals(0L, ((Number) cs.getRange().minValue).longValue());
-    assertEquals("COUNT max should be 80 (numRows - numNulls)",
-        80L, ((Number) cs.getRange().maxValue).longValue());
+    assertEquals(80L, ((Number) cs.getRange().maxValue).longValue(),
+        "COUNT max should be 80 (numRows - numNulls)");
+  }
+
+  /**
+   * HIVE-29625: COUNT(DISTINCT col) uses parentCS.getCountDistint() as the 
max range.
+   * When NDV is unknown (-1) the new guard short-circuits before building a 
Range with
+   * negative maxValue. When NDV is known, Range is set to [0, NDV].
+   */
+  @ParameterizedTest(name = "{0}")
+  @MethodSource("computeAggregateColumnMinMaxDistinctCases")
+  void testComputeAggregateColumnMinMaxDistinctByNDV(
+      String name, long parentNDV, Long expectedMax) throws SemanticException {
+    ColStatistics cs = new ColStatistics("_col0", "bigint");
+    HiveConf conf = new HiveConf();
+
+    ColStatistics parentColStats = new ColStatistics("val", "int");
+    parentColStats.setNumNulls(0);
+    parentColStats.setCountDistint(parentNDV);
+    parentColStats.setRange(1, 100);
+
+    Statistics parentStats = new Statistics(100, 400, 400, 400);
+    parentStats.addToColumnStats(Collections.singletonList(parentColStats));
+
+    ExprNodeColumnDesc colExpr = new ExprNodeColumnDesc(
+        TypeInfoFactory.intTypeInfo, "val", "t", false);
+    AggregationDesc agg = new AggregationDesc();
+    agg.setGenericUDAFName("count");
+    agg.setParameters(Collections.singletonList(colExpr));
+    agg.setDistinct(true);
+    agg.setMode(GenericUDAFEvaluator.Mode.COMPLETE);
+
+    StatsRulesProcFactory.GroupByStatsRule.computeAggregateColumnMinMax(
+        cs, conf, agg, "bigint", parentStats);
+
+    if (expectedMax == null) {
+      assertNull(cs.getRange(), "Range should NOT be set when DISTINCT NDV is 
unknown");
+    } else {
+      assertNotNull(cs.getRange(), "Range should be set when DISTINCT NDV is 
known");
+      assertEquals(0L, ((Number) cs.getRange().minValue).longValue());
+      assertEquals(expectedMax.longValue(), ((Number) 
cs.getRange().maxValue).longValue(),

Review Comment:
   Applied in 
[ecb61df](https://github.com/apache/hive/pull/6505/commits/ecb61df3ac) together 
with the line above.
   



-- 
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]

Reply via email to