konstantinb commented on code in PR #6505: URL: https://github.com/apache/hive/pull/6505#discussion_r4777565420
########## ql/src/java/org/apache/hadoop/hive/ql/optimizer/stats/annotation/StatsRulesProcFactory.java: ########## Review Comment: Applied in [425cbda](https://github.com/apache/hive/pull/6505/commits/425cbda808): the early return now applies only to COUNT and SUM, the branches that use `valuesCount`; MIN and MAX keep the column's range. In `groupby_distinct_minmax_ndv_unknown.q` the filter above `MAX(DISTINCT)` goes from 250 estimated rows to 1 (0 actual). The test needs `hive.map.groupby.sorted=true` with vectorization off, because with the default plan the reducer never finds the DISTINCT argument's stats: the argument is named `KEY.<key>:<n>._colX` ([SemanticAnalyzer](https://github.com/apache/hive/blob/f0ca1616e7c7e9a23c6edab00f7e5b9bc5474c49/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java#L5525-L5531)), while the reduce sink names its stats `KEY.<key>` ([StatsRulesProcFactory](https://github.com/apache/hive/blob/f0ca1616e7c7e9a23c6edab00f7e5b9bc5474c49/ql/src/java/org/apache/hadoop/hive/ql/optimizer/stats/annotation/StatsRulesProcFactory.java#L2935-L2941)). That gap is on master too; I'll file a follow-up. ########## ql/src/java/org/apache/hadoop/hive/ql/optimizer/ReduceSinkMapJoinProc.java: ########## Review Comment: Applied your fallback in [03b3387](https://github.com/apache/hive/pull/6505/commits/03b33876ea): a verified-zero NDV now contributes 1 to the product, so an all-NULL key column no longer zeroes the estimate. Any overestimate stays capped at the row count, so I didn't add the `getNullSafes` distinction. In `mapjoin_key_count_null_key.q` the estimated key count goes from 1 to 31 (12 actual). The test needs CBO off: with CBO on, the keys are plain columns, and their stats never resolve here because the reduce sink renames them to `KEY.*`, so the count falls back to the row count. That's the same naming gap as in the DISTINCT case. ########## 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: Agreed, applied in [645a0e1](https://github.com/apache/hive/pull/6505/commits/645a0e1c5d). It was worse than unsafe: `addWithExpDecay` multiplies, so one zero input made the NDV of `concat_ws(',', a, b)` 0 regardless of `b`. Master computed the same 0, but treated it as unknown. In `ndv_function_zero_input.q` the group-by estimate goes from 1 row to 125 (309 actual). ########## 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: Agreed that a partial aggregate can't back a verified zero: the flag is set when only some of the scanned partitions have stats, so the 0 covers those partitions only. Rather than checking it in this helper alone, [66d6f2c](https://github.com/apache/hive/pull/6505/commits/66d6f2c375) handles it where the flag is set (`StatsUtils.collectStatistics`): a partial-aggregate NDV of 0 becomes unknown. That also covers the IN, join, group-by and map-join paths, which would otherwise still trust it, and matches master for partial stats. Here `rowsAfterEqualityFilter` then returns `numRows / 2`, the same as your version. In `filter_ndv_zero_partial_partition_stats.q` the partial-stats filters go from 1 row to 262, while complete stats keep the verified-zero estimate. -- 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]
