Skip to content

Commit 11800bf

Browse files
committed
HIVE-29556: more explicit and "future-proof" NDV sssignment for const columns
1 parent 297cab5 commit 11800bf

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2115,7 +2115,9 @@ private static List<Long> extractNDVGroupingColumns(List<ColStatistics> colStats
21152115
for (ColStatistics cs : colStats) {
21162116
if (cs != null) {
21172117
long ndv = cs.getCountDistint();
2118-
if (cs.getNumNulls() > 0 && (ndv > 0 || cs.isConst())) {
2118+
if (cs.isConst()) {
2119+
ndv = 1;
2120+
} else if (ndv > 0 && cs.getNumNulls() > 0) {
21192121
ndv = StatsUtils.safeAdd(ndv, 1);
21202122
}
21212123
ndvValues.add(ndv);

ql/src/test/org/apache/hadoop/hive/ql/stats/TestStatsUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.junit.jupiter.api.Test;
4949
import org.junit.jupiter.params.ParameterizedTest;
5050
import org.junit.jupiter.params.provider.Arguments;
51+
import org.junit.jupiter.params.provider.CsvSource;
5152
import org.junit.jupiter.params.provider.MethodSource;
5253

5354
import com.google.common.collect.Sets;
@@ -625,4 +626,28 @@ void testColStatisticsIsConstSetterAndClone() {
625626
assertEquals(true, clone.isConst(), "Clone should be independent of source after mutation");
626627
}
627628

629+
@ParameterizedTest(name = "isConst=true => NDV contribution = 1 (countDistint={0}, numNulls={1})")
630+
@CsvSource({"1, 0", "0, 10", "1, 10", "0, 0"})
631+
void testComputeNDVGroupingColumnsConstColumnContributesOne(long countDistint, long numNulls) {
632+
ColStatistics cs = new ColStatistics("c", "int");
633+
cs.setCountDistint(countDistint);
634+
cs.setNumNulls(numNulls);
635+
cs.setConst(true);
636+
637+
long ndv = StatsUtils.computeNDVGroupingColumns(Collections.singletonList(cs), new Statistics(), false);
638+
639+
assertEquals(1L, ndv);
640+
}
641+
642+
@Test
643+
void testComputeNDVGroupingColumnsNonConstWithNullsAddsOne() {
644+
ColStatistics cs = new ColStatistics("c", "int");
645+
cs.setCountDistint(7);
646+
cs.setNumNulls(3);
647+
648+
long ndv = StatsUtils.computeNDVGroupingColumns(Collections.singletonList(cs), new Statistics(), false);
649+
650+
assertEquals(8L, ndv);
651+
}
652+
628653
}

0 commit comments

Comments
 (0)