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


##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/stats/HiveRelMdDistinctRowCount.java:
##########
@@ -61,6 +63,12 @@ public Double getDistinctRowCount(HiveTableScan htRel, 
RelMetadataQuery mq, Immu
     List<ColStatistics> colStats = htRel.getColStat(projIndxLst);
     Double noDistinctRows = 1.0;
     for (ColStatistics cStat : colStats) {
+      // unknown (< 0) or zero NDV yields 0; null (Calcite's
+      // "unknown") would need matching guards in
+      // HiveRelMdRowCount's PK-FK analysis

Review Comment:
   Done in 
[45c29a0](https://github.com/apache/hive/pull/6505/commits/45c29a03e3): 
`HiveRelMdDistinctRowCount` now returns null for an unknown NDV. That was this 
PR's original design 
([bed032d](https://github.com/apache/hive/pull/6505/commits/bed032d4d8)), 
reverted in 
[f5b19e9](https://github.com/apache/hive/pull/6505/commits/f5b19e9c38) as too 
intrusive. Null alone broke planning: `explain cbo joincost` with a filter on 
an unknown-NDV column hit an NPE in Calcite's `RelMdUtil.estimateFilteredRows`, 
and joins on RELY PK/FK constraints failed. So each reader got a guard, all 
following one rule: an unknown NDV never lowers an estimate below what the 
previous head of this PR estimated, and never discards a known value.
   - PK-FK row count: unknown is the existing "not computed" -1; an unknown 
ratio means every FK row matches.
   - Selectivity returned to Calcite: a predicate the statistics can't estimate 
keeps every row; unknown join keys are skipped, and the known ones still bound 
the join.
   - A filter above an anti join that depends on an unknown NDV keeps every 
row. That filter is inverted, so the fallback values would otherwise turn into 
near-zero estimates.
   - `FilterSelectivityEstimator`: NOT and SEARCH propagate the unknown instead 
of unboxing it.
   
   No existing golden changed (229 related qtests and the TPC-DS plan suites 
re-run). Two new qtests, `cbo_ndv_unknown_estimates.q` and 
`cbo_ndv_unknown_pkfk.q`, print each estimate next to the actual row count, and 
unit tests cover every changed line and branch.
   



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