Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

Check warning on line 135 in ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import 'com.google.common.collect.Sets'.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ47SLmkg1LROzm67ZCn&open=AZ47SLmkg1LROzm67ZCn&pullRequest=6496

Check warning on line 135 in ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unused import - com.google.common.collect.Sets.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ47SLmkg1LROzm67ZCo&open=AZ47SLmkg1LROzm67ZCo&pullRequest=6496
import com.google.common.math.LongMath;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Longs;
Expand Down Expand Up @@ -239,22 +239,20 @@
return aggregateStat.getNumRows();
}

private static void estimateStatsForMissingCols(List<String> neededColumns, List<ColStatistics> columnStats,
static List<ColStatistics> estimateStatsForMissingCols(
List<String> neededColumns, List<ColStatistics> existingColStats,
HiveConf conf, long nr, List<ColumnInfo> schema) {

Set<String> neededCols = new HashSet<>(neededColumns);
Set<String> colsWithStats = new HashSet<>();

for (ColStatistics cstats : columnStats) {
colsWithStats.add(cstats.getColumnName());
Set<String> missingCols = new HashSet<>(neededColumns);
List<ColStatistics> neededColStats = new ArrayList<>(neededColumns.size());
for (ColStatistics cs : existingColStats) {
if (missingCols.remove(cs.getColumnName())) {
neededColStats.add(cs);
}
}

List<String> missingColStats = new ArrayList<>(Sets.difference(neededCols, colsWithStats));

if (!missingColStats.isEmpty()) {
columnStats.addAll(
estimateStats(schema, missingColStats, conf, nr));
if (!missingCols.isEmpty()) {
neededColStats.addAll(estimateStats(schema, new ArrayList<>(missingCols), conf, nr));
}
return neededColStats;
}

public static Statistics collectStatistics(HiveConf conf, PrunedPartitionList partList,
Expand Down Expand Up @@ -300,7 +298,7 @@
if (needColStats && !metaTable) {
colStats = getTableColumnStats(table, neededColumns, colStatsCache, fetchColStats);
if (estimateStats) {
estimateStatsForMissingCols(neededColumns, colStats, conf, nr, schema);
colStats = estimateStatsForMissingCols(neededColumns, colStats, conf, nr, schema);
}
// we should have stats for all columns (estimated or actual)
if (neededColumns.size() == colStats.size()) {
Expand Down Expand Up @@ -386,7 +384,7 @@
boolean statsRetrieved = aggrStats != null &&
aggrStats.getColStats() != null && aggrStats.getColStatsSize() != 0;
if (neededColumns.isEmpty() || (!neededColsToRetrieve.isEmpty() && !statsRetrieved)) {
estimateStatsForMissingCols(neededColsToRetrieve, columnStats, conf, nr, schema);
columnStats = estimateStatsForMissingCols(neededColsToRetrieve, columnStats, conf, nr, schema);
// There are some partitions with no state (or we didn't fetch any state).
// Update the stats with empty list to reflect that in the
// state/initialize structures.
Expand Down
14 changes: 14 additions & 0 deletions ql/src/test/org/apache/hadoop/hive/ql/stats/TestStatsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
import org.apache.hadoop.hive.metastore.api.LongColumnStatsData;
import org.apache.hadoop.hive.metastore.api.Timestamp;
import org.apache.hadoop.hive.metastore.api.TimestampColumnStatsData;
import org.apache.hadoop.hive.ql.exec.ColumnInfo;
import org.apache.hadoop.hive.ql.plan.ColStatistics;
import org.apache.hadoop.hive.ql.plan.ColStatistics.Range;
import org.apache.hadoop.hive.ql.plan.Statistics;
import org.apache.hadoop.hive.serde.serdeConstants;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -565,4 +567,16 @@ void testGetColStatisticsTimestampType() {
assertEquals(1700000000L, range.maxValue.longValue(), "maxValue mismatch for TIMESTAMP");
}

@Test
void testEstimateStatsForMissingColsHandlesEmptyList() {
HiveConf conf = new HiveConf();

ColumnInfo columnInfoA = new ColumnInfo("a", TypeInfoFactory.intTypeInfo, "t", false);

List<ColStatistics> allColumnStats = StatsUtils.estimateStatsForMissingCols(
List.of("a"), Collections.emptyList(), conf, 0, List.of(columnInfoA));

assertEquals(1, allColumnStats.size());
}

}
Loading