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


##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/stats/annotation/StatsRulesProcFactory.java:
##########


Review Comment:
   I guess the early return above degrades the estimation in this branch. Could 
you keep the behavior where `valuesCount` is unused?



##########
ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java:
##########
@@ -1783,6 +1792,10 @@ private static long getNDVFor(ExprNodeGenericFuncDesc 
engfd, long numRows, Stati
       for (String col : engfd.getCols()) {
         ColStatistics stats = parentStats.getColumnStatisticsFromColName(col);
         if (stats != null) {
+          // countDistinct < 0 means "unknown"
+          if (stats.getCountDistint() < 0) {

Review Comment:
   ```suggestion
             // countDistinct < 0 purely means "unknown".
             // A zero input NDV does not imply a zero output NDV. For example, 
concat_ws skips NULL
             // arguments and can return values from another column. Treat the 
output NDV as unknown here.
             if (stats.getCountDistint() <= 0) {
   ```
   
   Is this safer right now? Say `a` is always NULL and the NDV of `b` is 1 
million. The true NDV of `concat_ws(',', a, b)` is 1M. That of `concat(a, b)` 
is zero. The result heavily relies on the UDF implementation when all values 
are NULL. The ideal solution is to implement an estimator for every UDF similar 
to `concat_ws`. But I don't think that's a trivial effort, even for standard 
UDFs.



##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/ReduceSinkMapJoinProc.java:
##########


Review Comment:
   The final number becomes zero, and an underestimation can occur when NDV = 0 
and # of nulls != 0, especially if this is not a null-safe join. It looks like 
that `MapJoinConf#getNullSafes` could provide insightful information. If it is 
hard to use, `maxKeyCount *= max(cs.getCountDistint(), 1);` might prevent the 
degradation



##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/stats/annotation/StatsRulesProcFactory.java:
##########
@@ -1381,6 +1388,17 @@ private long evaluateChildExpr(Statistics stats, 
ExprNodeDesc child,
       return numRows / 2;
     }
 
+    private static long rowsAfterEqualityFilter(long numRows, long dvs, 
boolean nullSafeEqualsNull) {
+      if (dvs < 0) {
+        return numRows / 2;
+      }
+      if (dvs == 0) {

Review Comment:
   ```
   private static long rowsAfterEqualityFilter(long numRows, ColStatistics cs, 
boolean nullSafeEqualsNull) {
     long dvs = cs.getCountDistint();
     if (dvs < 0) {
       return numRows / 2;
     }
     if (dvs == 0) {
       if (cs.isPartialAggregate()) {
         return numRows / 2;
       }
       return nullSafeEqualsNull ? numRows : 0;
     }
     return Math.round((double) numRows / dvs);
   }
   ```
   
   Checking the implementation, there is a case where `cs.isPartialAggregate` 
is flagged. In this case, the col stats are partially reliable. This might 
cause a catastrophic underestimation also with `nullSafeEqualsNull` flagged. I 
guess `cs.isPartialAggregate()` needs to be checked.



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